Skip to content

Commit 8e2e248

Browse files
committed
handle selectors with comma
1 parent 813e3f4 commit 8e2e248

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

src/cdk-experimental/testing/component-harness.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ export class HarnessPredicate<T extends ComponentHarness> {
349349

350350
/** Gets the selector used to find candidate elements. */
351351
getSelector() {
352-
return (this._ancestor ? `${this._ancestor} ` : '') + this.harnessType.hostSelector;
352+
return this._ancestor.split(',')
353+
.map(part => `${part.trim()} ${this.harnessType.hostSelector}`.trim())
354+
.join(',');
353355
}
354356

355357
/** Adds base options common to all harness types. */

src/cdk-experimental/testing/tests/harnesses/main-component-harness.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,23 @@ export class MainComponentHarness extends ComponentHarness {
5252
readonly lastList = this.locatorFor(SubComponentHarness.with({selector: ':last-child'}));
5353
readonly specaialKey = this.locatorFor('.special-key');
5454

55-
readonly requiredParentRestrictedSubcomponent =
55+
readonly requiredAncestorRestrictedSubcomponent =
5656
this.locatorFor(SubComponentHarness.with({ancestor: '.other'}));
57-
readonly optionalParentRestrictedSubcomponent =
57+
readonly optionalAncestorRestrictedSubcomponent =
5858
this.locatorForOptional(SubComponentHarness.with({ancestor: '.other'}));
59-
readonly allParentRestrictedSubcomponent =
59+
readonly allAncestorRestrictedSubcomponent =
6060
this.locatorForAll(SubComponentHarness.with({ancestor: '.other'}));
61-
readonly requiredParentRestrictedMissingSubcomponent =
61+
readonly requiredAncestorRestrictedMissingSubcomponent =
6262
this.locatorFor(SubComponentHarness.with({ancestor: '.not-found'}));
63-
readonly optionalParentRestrictedMissingSubcomponent =
63+
readonly optionalAncestorRestrictedMissingSubcomponent =
6464
this.locatorForOptional(SubComponentHarness.with({ancestor: '.not-found'}));
65-
readonly allParentRestrictedMissingSubcomponent =
65+
readonly allAncestorRestrictedMissingSubcomponent =
6666
this.locatorForAll(SubComponentHarness.with({ancestor: '.not-found'}));
67+
readonly multipleAncestorSelectorsSubcomponent =
68+
this.locatorForAll(SubComponentHarness.with({ancestor: '.other, .subcomponents'}));
69+
readonly directAncestorSelectorSubcomponent =
70+
this.locatorForAll(SubComponentHarness.with({ancestor: '.other >'}));
71+
6772

6873
private _testTools = this.locatorFor(SubComponentHarness);
6974

src/cdk-experimental/testing/tests/protractor.e2e.spec.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ describe('ProtractorHarnessEnvironment', () => {
271271
expect(await button.matchesSelector('button:disabled')).toBe(false);
272272
});
273273

274-
it('should load required harness with parent selector restriction', async () => {
275-
const subcomp = await harness.requiredParentRestrictedSubcomponent();
274+
it('should load required harness with ancestor selector restriction', async () => {
275+
const subcomp = await harness.requiredAncestorRestrictedSubcomponent();
276276
expect(await (await subcomp.title()).text()).toBe('List of other 1');
277277
});
278278

279-
it('should throw when failing to find required harness with parent selector restriction',
279+
it('should throw when failing to find required harness with ancestor selector restriction',
280280
async () => {
281281
try {
282-
await harness.requiredParentRestrictedMissingSubcomponent();
282+
await harness.requiredAncestorRestrictedMissingSubcomponent();
283283
fail('Expected to throw');
284284
} catch (e) {
285285
expect(e.message).toBe(
@@ -289,20 +289,20 @@ describe('ProtractorHarnessEnvironment', () => {
289289
}
290290
});
291291

292-
it('should load optional harness with parent selector restriction', async () => {
292+
it('should load optional harness with ancestor selector restriction', async () => {
293293
const [subcomp1, subcomp2] = await Promise.all([
294-
harness.optionalParentRestrictedSubcomponent(),
295-
harness.optionalParentRestrictedMissingSubcomponent()
294+
harness.optionalAncestorRestrictedSubcomponent(),
295+
harness.optionalAncestorRestrictedMissingSubcomponent()
296296
]);
297297
expect(subcomp1).not.toBeNull();
298298
expect(subcomp2).toBeNull();
299299
expect(await (await subcomp1!.title()).text()).toBe('List of other 1');
300300
});
301301

302-
it('should load all harnesses with parent selector restriction', async () => {
302+
it('should load all harnesses with ancestor selector restriction', async () => {
303303
const [subcomps1, subcomps2] = await Promise.all([
304-
harness.allParentRestrictedSubcomponent(),
305-
harness.allParentRestrictedMissingSubcomponent()
304+
harness.allAncestorRestrictedSubcomponent(),
305+
harness.allAncestorRestrictedMissingSubcomponent()
306306
]);
307307
expect(subcomps1.length).toBe(2);
308308
expect(subcomps2.length).toBe(0);
@@ -311,6 +311,16 @@ describe('ProtractorHarnessEnvironment', () => {
311311
expect(title1).toBe('List of other 1');
312312
expect(title2).toBe('List of other 2');
313313
});
314+
315+
it('should load all harnesses with multiple ancestor selector restriction', async () => {
316+
const subcomps = await harness.multipleAncestorSelectorsSubcomponent();
317+
expect(subcomps.length).toBe(4);
318+
});
319+
320+
it('should load all harnesses with direct ancestor selector restriction', async () => {
321+
const subcomps = await harness.directAncestorSelectorSubcomponent();
322+
expect(subcomps.length).toBe(2);
323+
});
314324
});
315325

316326
describe('HarnessPredicate', () => {

src/cdk-experimental/testing/tests/testbed.spec.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ describe('TestbedHarnessEnvironment', () => {
191191
expect(await specialKey.text()).toBe('alt-j');
192192
});
193193

194-
it('should load required harness with parent selector restriction', async () => {
195-
const subcomp = await harness.requiredParentRestrictedSubcomponent();
194+
it('should load required harness with ancestor selector restriction', async () => {
195+
const subcomp = await harness.requiredAncestorRestrictedSubcomponent();
196196
expect(await (await subcomp.title()).text()).toBe('List of other 1');
197197
});
198198

199-
it('should throw when failing to find required harness with parent selector restriction',
199+
it('should throw when failing to find required harness with ancestor selector restriction',
200200
async () => {
201201
try {
202-
await harness.requiredParentRestrictedMissingSubcomponent();
202+
await harness.requiredAncestorRestrictedMissingSubcomponent();
203203
fail('Expected to throw');
204204
} catch (e) {
205205
expect(e.message).toBe(
@@ -209,20 +209,20 @@ describe('TestbedHarnessEnvironment', () => {
209209
}
210210
});
211211

212-
it('should load optional harness with parent selector restriction', async () => {
212+
it('should load optional harness with ancestor selector restriction', async () => {
213213
const [subcomp1, subcomp2] = await Promise.all([
214-
harness.optionalParentRestrictedSubcomponent(),
215-
harness.optionalParentRestrictedMissingSubcomponent()
214+
harness.optionalAncestorRestrictedSubcomponent(),
215+
harness.optionalAncestorRestrictedMissingSubcomponent()
216216
]);
217217
expect(subcomp1).not.toBeNull();
218218
expect(subcomp2).toBeNull();
219219
expect(await (await subcomp1!.title()).text()).toBe('List of other 1');
220220
});
221221

222-
it('should load all harnesses with parent selector restriction', async () => {
222+
it('should load all harnesses with ancestor selector restriction', async () => {
223223
const [subcomps1, subcomps2] = await Promise.all([
224-
harness.allParentRestrictedSubcomponent(),
225-
harness.allParentRestrictedMissingSubcomponent()
224+
harness.allAncestorRestrictedSubcomponent(),
225+
harness.allAncestorRestrictedMissingSubcomponent()
226226
]);
227227
expect(subcomps1.length).toBe(2);
228228
expect(subcomps2.length).toBe(0);
@@ -231,6 +231,16 @@ describe('TestbedHarnessEnvironment', () => {
231231
expect(title1).toBe('List of other 1');
232232
expect(title2).toBe('List of other 2');
233233
});
234+
235+
it('should load all harnesses with multiple ancestor selector restriction', async () => {
236+
const subcomps = await harness.multipleAncestorSelectorsSubcomponent();
237+
expect(subcomps.length).toBe(4);
238+
});
239+
240+
it('should load all harnesses with direct ancestor selector restriction', async () => {
241+
const subcomps = await harness.directAncestorSelectorSubcomponent();
242+
expect(subcomps.length).toBe(2);
243+
});
234244
});
235245

236246
describe('TestElement', () => {

0 commit comments

Comments
 (0)