Skip to content

Commit 808370e

Browse files
andrewseguinmmalerba
authored andcommitted
feat(material-experimental): add progress bar test harness skeleton (#16619)
* feat(material-experimental): add progress bar test harness skeleton * use number coercion
1 parent 0e1e33b commit 808370e

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
/src/material-experimental/mdc-chips/** @mmalerba
9595
/src/material-experimental/mdc-helpers/** @mmalerba
9696
/src/material-experimental/mdc-menu/** @crisbeto
97+
/src/material-experimental/mdc-progress-bar/** @andrewseguin
9798
# Note to implementer: please repossess
9899
/src/material-experimental/mdc-radio/** @mmalerba
99100
/src/material-experimental/mdc-slide-toggle/** @crisbeto
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_test_library", "ts_library")
4+
5+
ts_library(
6+
name = "harness",
7+
srcs = glob(
8+
["harness/**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
deps = [
12+
"//src/cdk-experimental/testing",
13+
"//src/cdk/coercion",
14+
],
15+
)
16+
17+
ng_test_library(
18+
name = "progress_bar_tests_lib",
19+
srcs = glob(
20+
["**/*.spec.ts"],
21+
),
22+
deps = [
23+
":harness",
24+
"//src/cdk-experimental/testing",
25+
"//src/cdk-experimental/testing/testbed",
26+
"//src/cdk/coercion",
27+
"//src/cdk/testing",
28+
"//src/material/progress-bar",
29+
],
30+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 {MatProgressBarModule} from '@angular/material/progress-bar';
6+
7+
import {MatProgressBarHarness} from './progress-bar-harness';
8+
9+
let fixture: ComponentFixture<ProgressBarHarnessTest>;
10+
let loader: HarnessLoader;
11+
let progressBarHarness: typeof MatProgressBarHarness;
12+
13+
describe('MatProgressBarHarness', () => {
14+
beforeEach(async () => {
15+
await TestBed
16+
.configureTestingModule({
17+
imports: [MatProgressBarModule],
18+
declarations: [ProgressBarHarnessTest],
19+
})
20+
.compileComponents();
21+
22+
fixture = TestBed.createComponent(ProgressBarHarnessTest);
23+
fixture.detectChanges();
24+
loader = TestbedHarnessEnvironment.loader(fixture);
25+
progressBarHarness = MatProgressBarHarness;
26+
});
27+
28+
runTests();
29+
});
30+
31+
function runTests() {
32+
it('should load all progress bar harnesses', async () => {
33+
const progressBars = await loader.getAllHarnesses(progressBarHarness);
34+
expect(progressBars.length).toBe(2);
35+
});
36+
37+
it('should get the value', async () => {
38+
fixture.componentInstance.value = 50;
39+
const [determinate, indeterminate] = await loader.getAllHarnesses(progressBarHarness);
40+
expect(await determinate.getValue()).toBe(50);
41+
expect(await indeterminate.getValue()).toBe(null);
42+
});
43+
44+
it('should get the mode', async () => {
45+
const [determinate, indeterminate] = await loader.getAllHarnesses(progressBarHarness);
46+
expect(await determinate.getMode()).toBe('determinate');
47+
expect(await indeterminate.getMode()).toBe('indeterminate');
48+
});
49+
}
50+
51+
// TODO: Add and test progress bars with modes `buffer` and `query`.
52+
@Component({
53+
template: `
54+
<mat-progress-bar mode="determinate" [value]="value"></mat-progress-bar>
55+
<mat-progress-bar mode="indeterminate"></mat-progress-bar>
56+
`
57+
})
58+
class ProgressBarHarnessTest {
59+
value: number;
60+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ComponentHarness} from '@angular/cdk-experimental/testing';
10+
import {coerceNumberProperty} from '@angular/cdk/coercion';
11+
12+
/**
13+
* Harness for interacting with a standard mat-progress-bar in tests.
14+
* @dynamic
15+
*/
16+
export class MatProgressBarHarness extends ComponentHarness {
17+
static hostSelector = 'mat-progress-bar';
18+
19+
/** Gets a promise for the progress bar's value. */
20+
async getValue(): Promise<number|null> {
21+
const host = await this.host();
22+
const ariaValue = await host.getAttribute('aria-valuenow');
23+
return ariaValue ? coerceNumberProperty(ariaValue) : null;
24+
}
25+
26+
/** Gets a promise for the progress bar's mode. */
27+
async getMode(): Promise<string|null> {
28+
return (await this.host()).getAttribute('mode');
29+
}
30+
}

0 commit comments

Comments
 (0)