Skip to content

Commit ce5ad75

Browse files
committed
add test, refactor
1 parent cbabed5 commit ce5ad75

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
10-
import {browser, by, ElementFinder, Key} from 'protractor';
9+
import {
10+
ElementDimensions,
11+
getTextWithExcludedElements,
12+
ModifierKeys,
13+
TestElement,
14+
TestKey
15+
} from '@angular/cdk/testing';
16+
import {browser, ElementFinder, Key} from 'protractor';
1117

1218
/** Maps the `TestKey` constants to Protractor's `Key` constants. */
1319
const keyMap = {
@@ -131,9 +137,7 @@ export class ProtractorElement implements TestElement {
131137

132138
async text(options?: {excludes?: string}): Promise<string> {
133139
if (options?.excludes) {
134-
return browser.executeScript(`var clone = arguments[0].cloneNode(true);
135-
var remove = clone.querySelectorAll(arguments[1]);
136-
remove.forEach(n => n.remove()); return clone.textContent`, this.element, options.excludes);
140+
return browser.executeScript(getTextWithExcludedElements, this.element, options.excludes);
137141
} else {
138142
return this.element.getText();
139143
}

src/cdk/testing/test-element.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,13 @@ 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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
*/
88

99
import * as keyCodes from '@angular/cdk/keycodes';
10-
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
10+
import {
11+
ElementDimensions,
12+
getTextWithExcludedElements,
13+
ModifierKeys,
14+
TestElement,
15+
TestKey
16+
} from '@angular/cdk/testing';
1117
import {
1218
clearElement,
1319
dispatchMouseEvent,
@@ -129,10 +135,7 @@ export class UnitTestElement implements TestElement {
129135
async text(options?: {excludes?: string}): Promise<string> {
130136
await this._stabilize();
131137
if (options?.excludes) {
132-
const clone = this.element.cloneNode(true);
133-
const exclusions = this.element.querySelectorAll(options.excludes);
134-
exclusions.forEach(n => n.remove());
135-
return (clone.textContent || '').trim();
138+
return getTextWithExcludedElements(this.element, options.excludes);
136139
} else {
137140
return (this.element.textContent || '').trim();
138141
}

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,15 @@ export function crossEnvironmentSpecs(
412412
expect(await button.matchesSelector('button:disabled')).toBe(false);
413413
});
414414

415+
416+
it('should get correct text excluding certain selectors', async () => {
417+
const results = await harness.subcomponentAndSpecialHarnesses();
418+
const subHarnessHost = await results[0].host();
419+
420+
expect(await subHarnessHost.text({excludes: 'h2'})).toBe('ProtractorTestBedOther');
421+
expect(await subHarnessHost.text({excludes: 'li'})).toBe('List of test tools');
422+
});
423+
415424
it('should get TestElements and ComponentHarnesses', async () => {
416425
const results = await harness.subcomponentHarnessesAndElements();
417426
expect(results.length).toBe(5);

0 commit comments

Comments
 (0)