Skip to content

Commit bff5283

Browse files
committed
test(material/core): combine shared tests
Since we only have one module, we don't need separate shared tests anymore.
1 parent 33ba7d4 commit bff5283

File tree

5 files changed

+192
-234
lines changed

5 files changed

+192
-234
lines changed

src/material/core/testing/BUILD.bazel

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ filegroup(
1919
)
2020

2121
ng_test_library(
22-
name = "harness_tests_lib",
23-
srcs = [
24-
"optgroup-shared.spec.ts",
25-
"option-shared.spec.ts",
26-
],
22+
name = "unit_tests_lib",
23+
srcs = glob(["**/*.spec.ts"]),
2724
deps = [
2825
":testing",
2926
"//src/cdk/testing",
@@ -32,22 +29,6 @@ ng_test_library(
3229
],
3330
)
3431

35-
ng_test_library(
36-
name = "unit_tests_lib",
37-
srcs = glob(
38-
["**/*.spec.ts"],
39-
exclude = [
40-
"option-shared.spec.ts",
41-
"optgroup-shared.spec.ts",
42-
],
43-
),
44-
deps = [
45-
":harness_tests_lib",
46-
":testing",
47-
"//src/material/core",
48-
],
49-
)
50-
5132
ng_web_test_suite(
5233
name = "unit_tests",
5334
deps = [":unit_tests_lib"],
Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,77 @@
1+
import {Component} from '@angular/core';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
3+
import {HarnessLoader, parallel} from '@angular/cdk/testing';
4+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
15
import {MatOptionModule} from '@angular/material/core';
2-
import {runHarnessTests} from './optgroup-shared.spec';
36
import {MatOptgroupHarness} from './optgroup-harness';
47

5-
describe('MDC-based MatOptgroupHarness', () => {
6-
runHarnessTests(MatOptionModule, MatOptgroupHarness);
8+
describe('MatOptgroupHarness', () => {
9+
let fixture: ComponentFixture<OptgroupHarnessTest>;
10+
let loader: HarnessLoader;
11+
12+
beforeEach(async () => {
13+
await TestBed.configureTestingModule({
14+
imports: [MatOptionModule],
15+
declarations: [OptgroupHarnessTest],
16+
}).compileComponents();
17+
18+
fixture = TestBed.createComponent(OptgroupHarnessTest);
19+
fixture.detectChanges();
20+
loader = TestbedHarnessEnvironment.loader(fixture);
21+
});
22+
23+
it('should load all option group harnesses', async () => {
24+
const groups = await loader.getAllHarnesses(MatOptgroupHarness);
25+
expect(groups.length).toBe(2);
26+
});
27+
28+
it('should filter groups based on their text', async () => {
29+
const groups = await loader.getAllHarnesses(
30+
MatOptgroupHarness.with({
31+
labelText: 'Disabled group',
32+
}),
33+
);
34+
35+
expect(groups.length).toBe(1);
36+
});
37+
38+
it('should filter group label text by a pattern', async () => {
39+
const groups = await loader.getAllHarnesses(MatOptgroupHarness.with({labelText: /group/}));
40+
expect(groups.length).toBe(2);
41+
});
42+
43+
it('should get the group label text', async () => {
44+
const groups = await loader.getAllHarnesses(MatOptgroupHarness);
45+
const texts = await parallel(() => groups.map(group => group.getLabelText()));
46+
expect(texts).toEqual(['Plain group', 'Disabled group']);
47+
});
48+
49+
it('should get the group disabled state', async () => {
50+
const groups = await loader.getAllHarnesses(MatOptgroupHarness);
51+
const disabledStates = await parallel(() => groups.map(group => group.isDisabled()));
52+
expect(disabledStates).toEqual([false, true]);
53+
});
54+
55+
it('should get the options inside the groups', async () => {
56+
const group = await loader.getHarness(MatOptgroupHarness);
57+
const optionTexts = await group.getOptions().then(async options => {
58+
return await parallel(() => options.map(option => option.getText()));
59+
});
60+
61+
expect(optionTexts).toEqual(['Option 1', 'Option 2']);
62+
});
763
});
64+
65+
@Component({
66+
template: `
67+
<mat-optgroup label="Plain group">
68+
<mat-option>Option 1</mat-option>
69+
<mat-option>Option 2</mat-option>
70+
</mat-optgroup>
71+
72+
<mat-optgroup label="Disabled group" disabled>
73+
<mat-option>Disabled option 1</mat-option>
74+
</mat-optgroup>
75+
`,
76+
})
77+
class OptgroupHarnessTest {}

src/material/core/testing/optgroup-shared.spec.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,120 @@
1-
import {MatOptionModule, MatOption} from '@angular/material/core';
2-
import {runHarnessTests} from './option-shared.spec';
1+
import {Component, ViewChildren, QueryList} from '@angular/core';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
3+
import {HarnessLoader, parallel} from '@angular/cdk/testing';
4+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
5+
import {
6+
MatOption,
7+
MatOptionModule,
8+
MAT_OPTION_PARENT_COMPONENT,
9+
MatOptionParentComponent,
10+
} from '@angular/material/core';
311
import {MatOptionHarness} from './option-harness';
412

5-
describe('MDC-based MatOptionHarness', () => {
6-
runHarnessTests(MatOptionModule, MatOptionHarness, MatOption);
13+
describe('MatOptionHarness', () => {
14+
let fixture: ComponentFixture<OptionHarnessTest>;
15+
let loader: HarnessLoader;
16+
17+
beforeEach(async () => {
18+
await TestBed.configureTestingModule({
19+
imports: [MatOptionModule],
20+
declarations: [OptionHarnessTest],
21+
}).compileComponents();
22+
23+
fixture = TestBed.createComponent(OptionHarnessTest);
24+
fixture.detectChanges();
25+
loader = TestbedHarnessEnvironment.loader(fixture);
26+
});
27+
28+
it('should load all option harnesses', async () => {
29+
const options = await loader.getAllHarnesses(MatOptionHarness);
30+
expect(options.length).toBe(2);
31+
});
32+
33+
it('should filter options by text', async () => {
34+
const options = await loader.getAllHarnesses(MatOptionHarness.with({text: 'Disabled option'}));
35+
expect(options.length).toBe(1);
36+
});
37+
38+
it('should filter options text by a pattern', async () => {
39+
const options = await loader.getAllHarnesses(MatOptionHarness.with({text: /option/}));
40+
expect(options.length).toBe(2);
41+
});
42+
43+
it('should filter options text by its selected state', async () => {
44+
const options = await loader.getAllHarnesses(MatOptionHarness);
45+
let selectedOptions = await loader.getAllHarnesses(MatOptionHarness.with({isSelected: true}));
46+
47+
expect(options.length).toBe(2);
48+
expect(selectedOptions.length).toBe(0);
49+
50+
await options[0].click();
51+
52+
selectedOptions = await loader.getAllHarnesses(MatOptionHarness.with({isSelected: true}));
53+
54+
expect(selectedOptions.length).toBe(1);
55+
});
56+
57+
it('should get the text of options', async () => {
58+
const options = await loader.getAllHarnesses(MatOptionHarness);
59+
const texts = await parallel(() => options.map(option => option.getText()));
60+
expect(texts).toEqual(['Plain option', 'Disabled option']);
61+
});
62+
63+
it('should get whether an option is disabled', async () => {
64+
const options = await loader.getAllHarnesses(MatOptionHarness);
65+
const disabledStates = await parallel(() => options.map(option => option.isDisabled()));
66+
expect(disabledStates).toEqual([false, true]);
67+
});
68+
69+
it('should get whether an option is selected', async () => {
70+
const option = await loader.getHarness(MatOptionHarness);
71+
72+
expect(await option.isSelected()).toBe(false);
73+
74+
await option.click();
75+
76+
expect(await option.isSelected()).toBe(true);
77+
});
78+
79+
it('should get whether an option is active', async () => {
80+
const option = await loader.getHarness(MatOptionHarness);
81+
82+
expect(await option.isActive()).toBe(false);
83+
84+
// Set the option as active programmatically since
85+
// it's usually the parent component that does it.
86+
fixture.componentInstance.options.first.setActiveStyles();
87+
fixture.detectChanges();
88+
89+
expect(await option.isActive()).toBe(true);
90+
});
91+
92+
it('should get whether an option is in multi-selection mode', async () => {
93+
const option = await loader.getHarness(MatOptionHarness);
94+
95+
expect(await option.isMultiple()).toBe(false);
96+
97+
// Options take their `multiple` state from the parent component.
98+
fixture.componentInstance.multiple = true;
99+
fixture.detectChanges();
100+
101+
expect(await option.isMultiple()).toBe(true);
102+
});
7103
});
104+
105+
@Component({
106+
providers: [
107+
{
108+
provide: MAT_OPTION_PARENT_COMPONENT,
109+
useExisting: OptionHarnessTest,
110+
},
111+
],
112+
template: `
113+
<mat-option>Plain option</mat-option>
114+
<mat-option disabled>Disabled option</mat-option>
115+
`,
116+
})
117+
class OptionHarnessTest implements MatOptionParentComponent {
118+
@ViewChildren(MatOption) options: QueryList<{setActiveStyles(): void}>;
119+
multiple = false;
120+
}

0 commit comments

Comments
 (0)