Skip to content

Commit 1131d8c

Browse files
committed
add tests
1 parent 4a61a64 commit 1131d8c

File tree

8 files changed

+217
-6
lines changed

8 files changed

+217
-6
lines changed

src/cdk-experimental/testing/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package(default_visibility = ["//visibility:public"])
22

3-
load("//tools:defaults.bzl", "ng_module", "ng_web_test_suite")
3+
load("//tools:defaults.bzl", "ng_module", "ng_web_test_suite", "ts_library")
44
load("@npm_angular_bazel//:index.bzl", "protractor_web_test_suite")
55

66
ng_module(

src/cdk-experimental/testing/public-api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88

99
export * from './component-harness';
1010
export * from './harness-environment';
11-
export * from './protractor';
1211
export * from './test-element';
13-
export * from './testbed';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class UnitTestElement implements TestElement {
8686

8787
async text(): Promise<string> {
8888
await this._stabilize();
89-
return this.element.textContent || '';
89+
return (this.element.textContent || '').trim();
9090
}
9191

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

src/material-experimental/mdc-checkbox/BUILD.bazel

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package(default_visibility = ["//visibility:public"])
22

33
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary", "sass_library")
44
load("@npm_angular_bazel//:index.bzl", "protractor_web_test_suite")
5-
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_module", "ng_test_library", "ng_web_test_suite")
5+
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_module", "ng_test_library", "ng_web_test_suite", "ts_library")
66

77
ng_module(
88
name = "mdc-checkbox",
99
srcs = glob(
1010
["**/*.ts"],
11-
exclude = ["**/*.spec.ts", "harnesses/**"],
11+
exclude = ["**/*.spec.ts", "harness/**"],
1212
),
1313
assets = [":checkbox_scss"] + glob(["**/*.html"]),
1414
module_name = "@angular/material-experimental/mdc-checkbox",
@@ -25,6 +25,15 @@ ng_module(
2525
],
2626
)
2727

28+
ts_library(
29+
name = "harness",
30+
srcs = glob(["harness/**/*.ts"], exclude = ["**/*.spec.ts"]),
31+
deps = [
32+
"//src/cdk/coercion",
33+
"//src/cdk-experimental/testing",
34+
]
35+
)
36+
2837
sass_library(
2938
name = "mdc_checkbox_scss_lib",
3039
srcs = glob(["**/_*.scss"]),
@@ -55,8 +64,11 @@ ng_test_library(
5564
exclude = ["**/*.e2e.spec.ts"],
5665
),
5766
deps = [
67+
":harness",
5868
":mdc-checkbox",
5969
"//src/cdk/testing",
70+
"//src/cdk-experimental/testing",
71+
"//src/material/checkbox",
6072
"@npm//@angular/forms",
6173
"@npm//@angular/platform-browser",
6274
],
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import {HarnessLoader} from '@angular/cdk-experimental/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3+
import {Component} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {FormControl, ReactiveFormsModule} from '@angular/forms';
6+
import {MatCheckboxModule} from '@angular/material/checkbox';
7+
import {MatCheckboxModule as MatMdcCheckboxModule} from '../index';
8+
import {MatCheckboxHarness} from './checkbox-harness';
9+
import {MatCheckboxHarness as MatMdcCheckboxHarness} from './mdc-checkbox-harness';
10+
11+
let fixture: ComponentFixture<unknown>;
12+
let loader: HarnessLoader;
13+
let checkboxHarness: typeof MatCheckboxHarness;
14+
15+
describe('MatCheckboxHarness', () => {
16+
describe('non-MDC-based', () => {
17+
beforeEach(async () => {
18+
await TestBed.configureTestingModule({
19+
imports: [MatCheckboxModule, ReactiveFormsModule],
20+
declarations: [CheckboxHarnessTest],
21+
}).compileComponents();
22+
23+
fixture = TestBed.createComponent(CheckboxHarnessTest);
24+
fixture.detectChanges();
25+
loader = TestbedHarnessEnvironment.loader(fixture);
26+
checkboxHarness = MatCheckboxHarness;
27+
});
28+
29+
runTests();
30+
});
31+
32+
describe('MDC-based', () => {
33+
beforeEach(async () => {
34+
await TestBed.configureTestingModule({
35+
imports: [MatMdcCheckboxModule, ReactiveFormsModule],
36+
declarations: [CheckboxHarnessTest],
37+
}).compileComponents();
38+
39+
fixture = TestBed.createComponent(CheckboxHarnessTest);
40+
fixture.detectChanges();
41+
loader = TestbedHarnessEnvironment.loader(fixture);
42+
// Public APIs are the same as MatCheckboxHarness, but cast is necessary because of different
43+
// private fields.
44+
checkboxHarness = MatMdcCheckboxHarness as any;
45+
});
46+
47+
runTests();
48+
});
49+
});
50+
51+
/** Shared tests to run on both the original and MDC-based checkboxes. */
52+
function runTests() {
53+
it('should load all checkbox harnesses', async () => {
54+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
55+
expect(checkboxes.length).toBe(2);
56+
});
57+
58+
it('should load checkbox with exact label', async () => {
59+
const checkboxes = await loader.getAllHarnesses(checkboxHarness.with({label: 'First'}));
60+
expect(checkboxes.length).toBe(1);
61+
expect(await checkboxes[0].getLabelText()).toBe('First');
62+
});
63+
64+
it('should load checkbox with regex label match', async () => {
65+
const checkboxes = await loader.getAllHarnesses(checkboxHarness.with({label: /^s/i}));
66+
expect(checkboxes.length).toBe(1);
67+
expect(await checkboxes[0].getLabelText()).toBe('Second');
68+
});
69+
70+
it('should get checked state', async () => {
71+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
72+
expect(await checkboxes[0].isChecked()).toBe(true);
73+
expect(await checkboxes[1].isChecked()).toBe(false);
74+
});
75+
76+
it('should get indeterminate state', async () => {
77+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
78+
expect(await checkboxes[0].isIndeterminate()).toBe(false);
79+
expect(await checkboxes[1].isIndeterminate()).toBe(true);
80+
});
81+
82+
it('should get disabled state', async () => {
83+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
84+
expect(await checkboxes[0].isDisabled()).toBe(false);
85+
expect(await checkboxes[1].isDisabled()).toBe(true);
86+
});
87+
88+
it('should get required state', async () => {
89+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
90+
expect(await checkboxes[0].isRequired()).toBe(true);
91+
expect(await checkboxes[1].isRequired()).toBe(false);
92+
});
93+
94+
it('should get valid state', async () => {
95+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
96+
expect(await checkboxes[0].isValid()).toBe(true);
97+
expect(await checkboxes[1].isValid()).toBe(true);
98+
await checkboxes[0].uncheck();
99+
expect(await checkboxes[0].isValid()).toBe(false);
100+
});
101+
102+
it('should get name', async () => {
103+
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'}));
104+
expect(await checkbox.getName()).toBe('first-name');
105+
});
106+
107+
it('should get value', async () => {
108+
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'}));
109+
expect(await checkbox.getValue()).toBe('first-value');
110+
});
111+
112+
it('should get aria-label', async () => {
113+
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'}));
114+
expect(await checkbox.getAriaLabel()).toBe('First checkbox');
115+
});
116+
117+
it('should get aria-labelledby', async () => {
118+
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'Second'}));
119+
expect(await checkbox.getAriaLabelledby()).toBe('second-label');
120+
});
121+
122+
it('should get label text', async () => {
123+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
124+
expect(await checkboxes[0].getLabelText()).toBe('First');
125+
expect(await checkboxes[1].getLabelText()).toBe('Second');
126+
});
127+
128+
it('should focus checkbox', async () => {
129+
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'}));
130+
expect(getActiveElementTagName()).not.toBe('input');
131+
await checkbox.foucs();
132+
expect(getActiveElementTagName()).toBe('input');
133+
});
134+
135+
it('should blur checkbox', async () => {
136+
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'}));
137+
await checkbox.foucs();
138+
expect(getActiveElementTagName()).toBe('input');
139+
await checkbox.blur();
140+
expect(getActiveElementTagName()).not.toBe('input');
141+
});
142+
143+
it('should toggle checkbox', async () => {
144+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
145+
await checkboxes[0].toggle();
146+
await checkboxes[1].toggle();
147+
expect(await checkboxes[0].isChecked()).toBe(false);
148+
expect(await checkboxes[1].isChecked()).toBe(true);
149+
});
150+
151+
it('should check checkbox', async () => {
152+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
153+
await checkboxes[0].check();
154+
await checkboxes[1].check();
155+
expect(await checkboxes[0].isChecked()).toBe(true);
156+
expect(await checkboxes[1].isChecked()).toBe(true);
157+
});
158+
159+
it('should uncheck checkbox', async () => {
160+
const checkboxes = await loader.getAllHarnesses(checkboxHarness);
161+
await checkboxes[0].uncheck();
162+
await checkboxes[1].uncheck();
163+
expect(await checkboxes[0].isChecked()).toBe(false);
164+
expect(await checkboxes[1].isChecked()).toBe(false);
165+
});
166+
}
167+
168+
function getActiveElementTagName() {
169+
return document.activeElement ? document.activeElement.tagName.toLowerCase() : '';
170+
}
171+
172+
@Component({
173+
template: `
174+
<mat-checkbox
175+
[formControl]="ctrl"
176+
required
177+
name="first-name"
178+
value="first-value"
179+
aria-label="First checkbox">
180+
First
181+
</mat-checkbox>
182+
<mat-checkbox indeterminate="true" disabled="true" aria-labelledby="second-label">
183+
Second
184+
</mat-checkbox>
185+
<span id="second-label">Second checkbox</span>
186+
`
187+
})
188+
class CheckboxHarnessTest {
189+
ctrl = new FormControl(true);
190+
}
191+

src/material-experimental/mdc-checkbox/harnes/mat-checkbox-harness.ts renamed to src/material-experimental/mdc-checkbox/harness/checkbox-harness.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk-experimental/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111

12+
/**
13+
* Harness for interacting with a standard mat-checkbox in tests.
14+
* @dynamic
15+
*/
1216
export class MatCheckboxHarness extends ComponentHarness {
1317
static hostSelector = 'mat-checkbox';
1418

src/material-experimental/mdc-checkbox/harnes/mat-mdc-checkbox-harness.ts renamed to src/material-experimental/mdc-checkbox/harness/mdc-checkbox-harness.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk-experimental/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111

12+
/**
13+
* Harness for interacting with a MDC-based mat-checkbox in tests.
14+
* @dynamic
15+
*/
1216
export class MatCheckboxHarness extends ComponentHarness {
1317
static hostSelector = 'mat-checkbox';
1418

test/karma-system-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ System.config({
9393
'@angular/cdk-experimental/popover-edit': 'dist/packages/cdk-experimental/popover-edit/index.js',
9494
'@angular/cdk-experimental/scrolling': 'dist/packages/cdk-experimental/scrolling/index.js',
9595
'@angular/cdk-experimental/testing': 'dist/packages/cdk-experimental/testing/index.js',
96+
'@angular/cdk-experimental/testing/testbed': 'dist/packages/cdk-experimental/testing/testbed/index.js',
97+
'@angular/cdk-experimental/testing/protractor': 'dist/packages/cdk-experimental/testing/protractor/index.js',
9698

9799
'@angular/material/autocomplete': 'dist/packages/material/autocomplete/index.js',
98100
'@angular/material/badge': 'dist/packages/material/badge/index.js',

0 commit comments

Comments
 (0)