Skip to content

Commit d1de833

Browse files
committed
refactor: make cross environment tests compatible with Vitest
Updates the cross environment harness tests to account for Vitest and exclude some that don't work with a virtual DOM.
1 parent 79139cd commit d1de833

File tree

4 files changed

+112
-112
lines changed

4 files changed

+112
-112
lines changed

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

Lines changed: 109 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {SubComponentHarness, SubComponentSpecialHarness} from './harnesses/sub-c
2828
* @param getHarnessLoaderFromEnvironment env specific closure to get HarnessLoader
2929
* @param getMainComponentHarnessFromEnvironment env specific closure to get MainComponentHarness
3030
* @param getActiveElementId env specific closure to get active element
31-
* @param skipAsyncTests skip tests that rely on Angular framework stabilization
31+
* @param isVirtualDom Whether the tests are running inside a virtual DOM implementation.
3232
*
3333
* @docs-private
3434
*/
@@ -38,7 +38,7 @@ export function crossEnvironmentSpecs(
3838
// Maybe we should introduce HarnessLoader.getActiveElement(): TestElement
3939
// then this 3rd parameter could get removed.
4040
getActiveElementId: () => Promise<string | null>,
41-
skipAsyncTests: boolean = false,
41+
isVirtualDom: boolean,
4242
) {
4343
describe('HarnessLoader', () => {
4444
let loader: HarnessLoader;
@@ -57,15 +57,11 @@ export function crossEnvironmentSpecs(
5757
});
5858

5959
it('should error after failing to find required HarnessLoader for child element', async () => {
60-
try {
61-
await loader.getChildLoader('error');
62-
fail('Expected to throw');
63-
} catch (e) {
64-
expect((e as Error).message).toBe(
65-
'Failed to find element matching one of the following queries:' +
66-
'\n(HarnessLoader for element matching selector: "error")',
67-
);
68-
}
60+
await expectRejection(
61+
() => loader.getChildLoader('error'),
62+
'Failed to find element matching one of the following queries:' +
63+
'\n(HarnessLoader for element matching selector: "error")',
64+
);
6965
});
7066

7167
it('should find all HarnessLoaders for child elements', async () => {
@@ -81,14 +77,10 @@ export function crossEnvironmentSpecs(
8177

8278
it('should throw if no matching component found for required harness', async () => {
8379
const countersLoader = await loader.getChildLoader('.counters');
84-
try {
85-
await countersLoader.getHarness(SubComponentHarness);
86-
fail('Expected to throw');
87-
} catch (e) {
88-
expect((e as Error).message).toMatch(
89-
/Failed to find element matching one of the following queries:\n\(_?SubComponentHarness with host element matching selector: "test-sub"\)/,
90-
);
91-
}
80+
await expectRejection(
81+
() => countersLoader.getHarness(SubComponentHarness),
82+
/Failed to find element matching one of the following queries:\n\(_?SubComponentHarness with host element matching selector: "test-sub"\)/,
83+
);
9284
});
9385

9486
it('should get first matching component for optional harness', async () => {
@@ -128,15 +120,11 @@ export function crossEnvironmentSpecs(
128120
});
129121

130122
it('should throw when failing to locate a required element based on CSS selector', async () => {
131-
try {
132-
await harness.errorItem();
133-
fail('Expected to throw');
134-
} catch (e) {
135-
expect((e as Error).message).toBe(
136-
'Failed to find element matching one of the following queries:' +
137-
'\n(TestElement for element matching selector: "wrong locator")',
138-
);
139-
}
123+
await expectRejection(
124+
() => harness.errorItem(),
125+
'Failed to find element matching one of the following queries:' +
126+
'\n(TestElement for element matching selector: "wrong locator")',
127+
);
140128
});
141129

142130
it('should locate an optional element based on CSS selector', async () => {
@@ -163,15 +151,11 @@ export function crossEnvironmentSpecs(
163151
});
164152

165153
it('should throw when failing to locate required sub harnesses', async () => {
166-
try {
167-
await harness.errorSubComponent();
168-
fail('Expected to throw');
169-
} catch (e) {
170-
expect((e as Error).message).toBe(
171-
'Failed to find element matching one of the following queries:' +
172-
'\n(WrongComponentHarness with host element matching selector: "wrong-selector")',
173-
);
174-
}
154+
await expectRejection(
155+
() => harness.errorSubComponent(),
156+
'Failed to find element matching one of the following queries:\n' +
157+
'(WrongComponentHarness with host element matching selector: "wrong-selector")',
158+
);
175159
});
176160

177161
it('should locate optional sub harnesses', async () => {
@@ -220,14 +204,10 @@ export function crossEnvironmentSpecs(
220204
});
221205

222206
it('should throw when failing to find required harness with ancestor selector restriction', async () => {
223-
try {
224-
await harness.requiredAncestorRestrictedMissingSubcomponent();
225-
fail('Expected to throw');
226-
} catch (e) {
227-
expect((e as Error).message).toMatch(
228-
/Failed to find element matching one of the following queries:\n\(_?SubComponentHarness with host element matching selector: "test-sub" satisfying the constraints: has ancestor matching selector ".not-found"\)/,
229-
);
230-
}
207+
await expectRejection(
208+
() => harness.requiredAncestorRestrictedMissingSubcomponent(),
209+
/Failed to find element matching one of the following queries:\n\(_?SubComponentHarness with host element matching selector: "test-sub" satisfying the constraints: has ancestor matching selector ".not-found"\)/,
210+
);
231211
});
232212

233213
it('should load optional harness with ancestor selector restriction', async () => {
@@ -280,14 +260,12 @@ export function crossEnvironmentSpecs(
280260
expect(await element.getText()).toBe('Has comma inside attribute');
281261
});
282262

283-
if (!skipAsyncTests) {
284-
it('should wait for async operation to complete', async () => {
285-
const asyncCounter = await harness.asyncCounter();
286-
expect(await asyncCounter.text()).toBe('5');
287-
await harness.increaseCounter(3);
288-
expect(await asyncCounter.text()).toBe('8');
289-
});
290-
}
263+
it('should wait for async operation to complete', async () => {
264+
const asyncCounter = await harness.asyncCounter();
265+
expect(await asyncCounter.text()).toBe('5');
266+
await harness.increaseCounter(3);
267+
expect(await asyncCounter.text()).toBe('8');
268+
});
291269
});
292270

293271
describe('HarnessPredicate', () => {
@@ -327,14 +305,10 @@ export function crossEnvironmentSpecs(
327305
});
328306

329307
it('should error if predicate does not match but a harness is required', async () => {
330-
try {
331-
await harness.requiredFourIteamToolsLists();
332-
fail('Expected to throw');
333-
} catch (e) {
334-
expect((e as Error).message).toMatch(
335-
/Failed to find element matching one of the following queries:\n\(_?SubComponentHarness with host element matching selector: "test-sub" satisfying the constraints: title = "List of test tools", item count = 4\)/,
336-
);
337-
}
308+
await expectRejection(
309+
() => harness.requiredFourIteamToolsLists(),
310+
/Failed to find element matching one of the following queries:\n\(_?SubComponentHarness with host element matching selector: "test-sub" satisfying the constraints: title = "List of test tools", item count = 4\)/,
311+
);
338312
});
339313

340314
it('should have correct description for debugging', () => {
@@ -429,12 +403,14 @@ export function crossEnvironmentSpecs(
429403
expect(await modifiersResult.text()).toBe('shift---meta');
430404
});
431405

432-
it('should be able to click the center of an element', async () => {
433-
const clickTest = await harness.clickTest();
434-
const clickTestResult = await harness.clickTestResult();
435-
await clickTest.click('center');
436-
expect(await clickTestResult.text()).toBe('50-50');
437-
});
406+
if (!isVirtualDom) {
407+
it('should be able to click the center of an element', async () => {
408+
const clickTest = await harness.clickTest();
409+
const clickTestResult = await harness.clickTestResult();
410+
await clickTest.click('center');
411+
expect(await clickTestResult.text()).toBe('50-50');
412+
});
413+
}
438414

439415
it('should be able to click the center of an element with shift meta modifiers', async () => {
440416
const clickTest = await harness.clickTest();
@@ -491,35 +467,41 @@ export function crossEnvironmentSpecs(
491467
expect(await value.text()).toBe('Number value: -123');
492468
});
493469

494-
it('should be able to retrieve dimensions', async () => {
495-
const dimensions = await (await harness.title()).getDimensions();
496-
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));
497-
});
470+
if (!isVirtualDom) {
471+
it('should be able to retrieve dimensions', async () => {
472+
const dimensions = await (await harness.title()).getDimensions();
473+
expect(dimensions.height).toBe(100);
474+
expect(dimensions.width).toBe(200);
475+
});
476+
}
498477

499-
it('should dispatch `mouseenter` and `mouseover` on hover', async () => {
500-
const box = await harness.hoverTest();
501-
let classAttr = await box.getAttribute('class');
502-
expect(classAttr).not.toContain('hovering');
503-
expect(classAttr).not.toContain('pointer-over');
504-
await box.hover();
505-
classAttr = await box.getAttribute('class');
506-
expect(classAttr).toContain('hovering');
507-
expect(classAttr).toContain('pointer-over');
508-
});
478+
// TODO(crisbeto): this _should_ work with a virtual DOM.
479+
if (!isVirtualDom) {
480+
it('should dispatch `mouseenter` and `mouseover` on hover', async () => {
481+
const box = await harness.hoverTest();
482+
let classAttr = await box.getAttribute('class');
483+
expect(classAttr).not.toContain('hovering');
484+
expect(classAttr).not.toContain('pointer-over');
485+
await box.hover();
486+
classAttr = (await box.getAttribute('class')) || '';
487+
expect(classAttr).toContain('hovering');
488+
expect(classAttr).toContain('pointer-over');
489+
});
509490

510-
it('should dispatch `mouseleave` and `mouseout` on mouseAway', async () => {
511-
const box = await harness.hoverTest();
512-
let classAttr = await box.getAttribute('class');
513-
expect(classAttr).not.toContain('hovering');
514-
await box.hover();
515-
classAttr = await box.getAttribute('class');
516-
expect(classAttr).toContain('hovering');
517-
expect(classAttr).toContain('pointer-over');
518-
await box.mouseAway();
519-
classAttr = await box.getAttribute('class');
520-
expect(classAttr).not.toContain('hovering');
521-
expect(classAttr).not.toContain('pointer-over');
522-
});
491+
it('should dispatch `mouseleave` and `mouseout` on mouseAway', async () => {
492+
const box = await harness.hoverTest();
493+
let classAttr = await box.getAttribute('class');
494+
expect(classAttr).not.toContain('hovering');
495+
await box.hover();
496+
classAttr = await box.getAttribute('class');
497+
expect(classAttr).toContain('hovering');
498+
expect(classAttr).toContain('pointer-over');
499+
await box.mouseAway();
500+
classAttr = await box.getAttribute('class');
501+
expect(classAttr).not.toContain('hovering');
502+
expect(classAttr).not.toContain('pointer-over');
503+
});
504+
}
523505

524506
it('should be able to getAttribute', async () => {
525507
const memoStr = `
@@ -559,17 +541,20 @@ export function crossEnvironmentSpecs(
559541
expect(await input.getProperty<string>('value')).toBe('hello');
560542
});
561543

562-
it('should be able to set the value of a select in single selection mode', async () => {
563-
const [select, value, changeEventCounter] = await parallel(() => [
564-
harness.singleSelect(),
565-
harness.singleSelectValue(),
566-
harness.singleSelectChangeEventCounter(),
567-
]);
544+
// TODO(crisbeto): this _should_ work with a virtual DOM.
545+
if (!isVirtualDom) {
546+
it('should be able to set the value of a select in single selection mode', async () => {
547+
const [select, value, changeEventCounter] = await parallel(() => [
548+
harness.singleSelect(),
549+
harness.singleSelectValue(),
550+
harness.singleSelectChangeEventCounter(),
551+
]);
568552

569-
await select.selectOptions(2);
570-
expect(await value.text()).toBe('Select: three');
571-
expect(await changeEventCounter.text()).toBe('Change events: 1');
572-
});
553+
await select.selectOptions(2);
554+
expect(await value.text()).toBe('Select: three');
555+
expect(await changeEventCounter.text()).toBe('Change events: 1');
556+
});
557+
}
573558

574559
it('should be able to set the value of a select in multi-selection mode', async () => {
575560
const [select, value, changeEventCounter] = await parallel(() => [
@@ -686,14 +671,10 @@ export function crossEnvironmentSpecs(
686671
});
687672

688673
it('should throw when multiple queries fail to match', async () => {
689-
try {
690-
await harness.missingElementsAndHarnesses();
691-
fail('Expected to throw.');
692-
} catch (e) {
693-
expect((e as Error).message).toMatch(
694-
/Failed to find element matching one of the following queries:\n\(TestElement for element matching selector: ".not-found"\),\n\(_?SubComponentHarness with host element matching selector: "test-sub" satisfying the constraints: title = \/not found\/\)/,
695-
);
696-
}
674+
await expectRejection(
675+
() => harness.missingElementsAndHarnesses(),
676+
/Failed to find element matching one of the following queries:\n\(TestElement for element matching selector: ".not-found"\),\n\(_?SubComponentHarness with host element matching selector: "test-sub" satisfying the constraints: title = \/not found\/\)/,
677+
);
697678
});
698679

699680
it('should check if element is focused', async () => {
@@ -737,3 +718,19 @@ export async function checkIsHarness<T extends ComponentHarness>(
737718
await finalCheck(result as T);
738719
}
739720
}
721+
722+
async function expectRejection(fn: () => Promise<unknown>, expectedError: string | RegExp) {
723+
let error = '<blank>';
724+
725+
try {
726+
await fn();
727+
} catch (e) {
728+
error = (e as Error).message;
729+
}
730+
731+
if (typeof expectedError === 'string') {
732+
expect(error).toContain(expectedError);
733+
} else {
734+
expect(error).toMatch(expectedError);
735+
}
736+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe('ProtractorHarnessEnvironment', () => {
9797
() => ProtractorHarnessEnvironment.loader(),
9898
() => ProtractorHarnessEnvironment.loader().getHarness(MainComponentHarness),
9999
async () => (await activeElement()).getAttribute('id'),
100+
false,
100101
));
101102
});
102103

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,6 @@ describe('TestbedHarnessEnvironment', () => {
189189
() => TestbedHarnessEnvironment.loader(fixture),
190190
() => TestbedHarnessEnvironment.harnessForFixture(fixture, MainComponentHarness),
191191
() => Promise.resolve(document.activeElement!.id),
192+
false,
192193
));
193194
});

src/cdk/testing/tests/webdriver.e2e.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,6 @@ describe('WebDriverHarnessEnvironment', () => {
143143
() => SeleniumWebDriverHarnessEnvironment.loader(wd),
144144
() => SeleniumWebDriverHarnessEnvironment.loader(wd).getHarness(MainComponentHarness),
145145
async () => (await activeElement()).getAttribute('id'),
146+
false,
146147
));
147148
});

0 commit comments

Comments
 (0)