Skip to content

Commit 594fb9b

Browse files
committed
move shared function to separate file
1 parent 21876af commit 594fb9b

File tree

7 files changed

+23
-26
lines changed

7 files changed

+23
-26
lines changed

src/cdk/testing/dom-helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function getTextWithExcludedElements(element: Element, excludeSelector: string) {
2+
const clone = element.cloneNode(true) as Element;
3+
const exclusions = clone.querySelectorAll(excludeSelector);
4+
for (let i = 0; i < exclusions.length; i++) {
5+
let child = exclusions[i];
6+
child.parentNode?.removeChild(child);
7+
}
8+
return (clone.textContent || '').trim();
9+
}

src/cdk/testing/protractor/protractor-element.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import {
1010
ElementDimensions,
11-
getTextWithExcludedElements,
1211
ModifierKeys,
1312
TestElement,
1413
TestKey
1514
} from '@angular/cdk/testing';
1615
import {browser, ElementFinder, Key} from 'protractor';
16+
import {getTextWithExcludedElements} from '@angular/cdk/testing/dom-helpers';
1717

1818
/** Maps the `TestKey` constants to Protractor's `Key` constants. */
1919
const keyMap = {
@@ -135,12 +135,11 @@ export class ProtractorElement implements TestElement {
135135
return this.element.sendKeys(...keys);
136136
}
137137

138-
async text(options?: {excludes?: string}): Promise<string> {
139-
if (options?.excludes) {
140-
return browser.executeScript(getTextWithExcludedElements, this.element, options.excludes);
141-
} else {
142-
return this.element.getText();
138+
async text(options?: {exclude?: string}): Promise<string> {
139+
if (options?.exclude) {
140+
return browser.executeScript(getTextWithExcludedElements, this.element, options.excludes;
143141
}
142+
return this.element.getText();
144143
}
145144

146145
async getAttribute(name: string): Promise<string|null> {

src/cdk/testing/test-element.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export interface TestElement {
106106
* @param options Options that affect what text is included
107107
* exclude: A selector for elements whose text should be excluded from the result.
108108
*/
109-
text(options?: {excludes?: string}): Promise<string>;
109+
text(options?: {exclude?: string}): Promise<string>;
110110

111111
/** Gets the value for the given attribute from the element. */
112112
getAttribute(name: string): Promise<string | null>;
@@ -132,13 +132,3 @@ export interface TestElement {
132132
/** Checks whether the element is focused. */
133133
isFocused(): Promise<boolean>;
134134
}
135-
136-
export function getTextWithExcludedElements(element: Element, excludeSelector: string) {
137-
const clone = element.cloneNode(true) as Element;
138-
const exclusions = clone.querySelectorAll(excludeSelector);
139-
for (let i = 0; i < exclusions.length; i++) {
140-
let child = exclusions[i];
141-
child.parentNode?.removeChild(child);
142-
}
143-
return (clone.textContent || '').trim();
144-
}

src/cdk/testing/testbed/unit-test-element.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import * as keyCodes from '@angular/cdk/keycodes';
1010
import {
1111
ElementDimensions,
12-
getTextWithExcludedElements,
1312
ModifierKeys,
1413
TestElement,
1514
TestKey
@@ -23,6 +22,7 @@ import {
2322
triggerFocus,
2423
typeInElement,
2524
} from './fake-events';
25+
import {getTextWithExcludedElements} from '@angular/cdk/testing/dom-helpers';
2626

2727
/** Maps `TestKey` constants to the `keyCode` and `key` values used by native browser events. */
2828
const keyMap = {
@@ -132,13 +132,12 @@ export class UnitTestElement implements TestElement {
132132
await this._stabilize();
133133
}
134134

135-
async text(options?: {excludes?: string}): Promise<string> {
135+
async text(options?: {exclude?: string}): Promise<string> {
136136
await this._stabilize();
137-
if (options?.excludes) {
138-
return getTextWithExcludedElements(this.element, options.excludes);
139-
} else {
140-
return (this.element.textContent || '').trim();
137+
if (options?.exclude) {
138+
return getTextWithExcludedElements(this.element, options.exclude);
141139
}
140+
return (this.element.textContent || '').trim();
142141
}
143142

144143
async getAttribute(name: string): Promise<string|null> {

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export interface TestElement {
134134
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
135135
setInputValue?(value: string): Promise<void>;
136136
text(options?: {
137-
excludes?: string;
137+
exclude?: string;
138138
}): Promise<string>;
139139
}
140140

tools/public_api_guard/cdk/testing/protractor.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export declare class ProtractorElement implements TestElement {
1818
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
1919
setInputValue(value: string): Promise<void>;
2020
text(options?: {
21-
excludes?: string;
21+
exclude?: string;
2222
}): Promise<string>;
2323
}
2424

tools/public_api_guard/cdk/testing/testbed.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export declare class UnitTestElement implements TestElement {
3535
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
3636
setInputValue(value: string): Promise<void>;
3737
text(options?: {
38-
excludes?: string;
38+
exclude?: string;
3939
}): Promise<string>;
4040
}

0 commit comments

Comments
 (0)