Skip to content

Commit 7de6851

Browse files
committed
feat(material-experimental): add autocomplete test harness
* Adds a test harness for `mat-autocomplete`. * Fixes the global element locator not working for `TestBed` elements. * Adds the ability to get a property of a `TestElement`.
1 parent 88631b9 commit 7de6851

20 files changed

+508
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
# Material experimental package
8686
/src/material-experimental/* @jelbourn
87+
/src/material-experimental/mdc-autocomplete/** @crisbeto
8788
/src/material-experimental/mdc-button/** @andrewseguin
8889
/src/material-experimental/mdc-card/** @mmalerba
8990
/src/material-experimental/mdc-checkbox/** @mmalerba

src/cdk-experimental/testing/protractor/protractor-element.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ export class ProtractorElement implements TestElement {
5555
const classes = (await this.getAttribute('class')) || '';
5656
return new Set(classes.split(/\s+/).filter(c => c)).has(name);
5757
}
58+
59+
async getPropertyValue(name: string): Promise<any> {
60+
return browser.executeScript(`arguments[0][${name}]`, this.element);
61+
}
5862
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ export interface TestElement {
4646

4747
/** Checks whether the element has the given class. */
4848
hasClass(name: string): Promise<boolean>;
49+
50+
/** Gets a property of an element. */
51+
getPropertyValue(name: string): Promise<any>;
4952
}

src/cdk-experimental/testing/testbed/testbed-harness-environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
3737
}
3838

3939
protected getDocumentRoot(): Element {
40-
return this._fixture.nativeElement;
40+
return document.body;
4141
}
4242

4343
protected createTestElement(element: Element): TestElement {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,9 @@ export class UnitTestElement implements TestElement {
105105
await this._stabilize();
106106
return this.element.classList.contains(name);
107107
}
108+
109+
async getPropertyValue(name: string): Promise<any> {
110+
await this._stabilize();
111+
return (this.element as any)[name];
112+
}
108113
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ describe('ProtractorHarnessEnvironment', () => {
230230
expect(await (await browser.switchTo().activeElement()).getText())
231231
.not.toBe(await button.text());
232232
});
233+
234+
it('should be able to get an element property', async () => {
235+
const input = await harness.input();
236+
await input.sendKeys('Hello there');
237+
expect(await input.getPropertyValue('value')).toBe('Hello there');
238+
});
239+
233240
});
234241

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ describe('TestbedHarnessEnvironment', () => {
247247
await button.blur();
248248
expect(activeElementText()).not.toBe(await button.text());
249249
});
250+
251+
it('should be able to get an element property', async () => {
252+
const input = await harness.input();
253+
await input.sendKeys('Hello there');
254+
expect(await input.getPropertyValue('value')).toBe('Hello there');
255+
});
256+
250257
});
251258

252259
describe('HarnessPredicate', () => {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary", "sass_library")
4+
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_module", "ng_test_library", "ng_web_test_suite", "ts_library")
5+
load("//src/e2e-app:test_suite.bzl", "e2e_test_suite")
6+
7+
ng_module(
8+
name = "mdc-autocomplete",
9+
srcs = glob(
10+
["**/*.ts"],
11+
exclude = [
12+
"**/*.spec.ts",
13+
"harness/**",
14+
],
15+
),
16+
assets = [
17+
# TODO: include scss assets
18+
] + glob(["**/*.html"]),
19+
module_name = "@angular/material-experimental/mdc-autocomplete",
20+
deps = [
21+
"//src/material/core",
22+
],
23+
)
24+
25+
ts_library(
26+
name = "harness",
27+
srcs = glob(
28+
["harness/**/*.ts"],
29+
exclude = ["**/*.spec.ts"],
30+
),
31+
deps = [
32+
"//src/cdk-experimental/testing",
33+
"//src/cdk/coercion",
34+
],
35+
)
36+
37+
sass_library(
38+
name = "mdc_autocomplete_scss_lib",
39+
srcs = glob(["**/_*.scss"]),
40+
deps = [
41+
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
42+
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",
43+
"//src/material/core:core_scss_lib",
44+
],
45+
)
46+
47+
sass_binary(
48+
name = "autocomplete_scss",
49+
src = "autocomplete.scss",
50+
include_paths = [
51+
"external/npm/node_modules",
52+
],
53+
deps = [
54+
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
55+
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",
56+
"//src/material/core:all_themes",
57+
],
58+
)
59+
60+
ng_test_library(
61+
name = "autocomplete_tests_lib",
62+
srcs = [
63+
"harness/autocomplete-harness.spec.ts",
64+
],
65+
deps = [
66+
":harness",
67+
":mdc-autocomplete",
68+
"//src/cdk-experimental/testing",
69+
"//src/cdk-experimental/testing/testbed",
70+
"//src/cdk/platform",
71+
"//src/cdk/testing",
72+
"//src/material/autocomplete",
73+
"@npm//@angular/platform-browser",
74+
],
75+
)
76+
77+
ng_web_test_suite(
78+
name = "unit_tests",
79+
deps = [
80+
":autocomplete_tests_lib",
81+
"//src/material-experimental:mdc_require_config.js",
82+
],
83+
)
84+
85+
ng_e2e_test_library(
86+
name = "e2e_test_sources",
87+
srcs = glob(["**/*.e2e.spec.ts"]),
88+
deps = [
89+
"//src/cdk/testing/e2e",
90+
],
91+
)
92+
93+
e2e_test_suite(
94+
name = "e2e_tests",
95+
deps = [
96+
":e2e_test_sources",
97+
"//src/cdk/testing/e2e",
98+
],
99+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- TODO -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@import '../mdc-helpers/mdc-helpers';
2+
3+
@mixin mat-autocomplete-theme-mdc($theme) {
4+
@include mat-using-mdc-theme($theme) {
5+
// TODO: implement MDC-based autocomplete.
6+
}
7+
}
8+
9+
@mixin mat-autocomplete-typography-mdc($config) {
10+
@include mat-using-mdc-typography($config) {
11+
// TODO: implement MDC-based autocomplete.
12+
}
13+
}

0 commit comments

Comments
 (0)