Skip to content

Commit 402f4d6

Browse files
committed
docs(material/autocomplete): add autocomplete harness example
1 parent 2d79ab7 commit 402f4d6

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

src/components-examples/material/autocomplete/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/autocomplete",
1313
deps = [
14+
"//src/cdk/overlay",
15+
"//src/cdk/testing",
16+
"//src/cdk/testing/testbed",
1417
"//src/material/autocomplete",
18+
"//src/material/autocomplete/testing",
1519
"//src/material/form-field",
1620
"//src/material/input",
1721
"//src/material/slide-toggle",
1822
"@npm//@angular/forms",
23+
"@npm//@angular/platform-browser",
24+
"@npm//@angular/platform-browser-dynamic",
25+
"@npm//@types/jasmine",
1926
],
2027
)
2128

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<mat-autocomplete #autocomplete="matAutocomplete">
2+
<mat-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</mat-option>
3+
</mat-autocomplete>
4+
5+
<input id="plain" [matAutocomplete]="autocomplete">
6+
<input id="disabled" disabled [matAutocomplete]="autocomplete">
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {TestBed, ComponentFixture, waitForAsync, inject} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatAutocompleteHarness} from '@angular/material/autocomplete/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatAutocompleteModule} from '@angular/material/autocomplete';
8+
import {AutocompleteHarnessExample} from './autocomplete-harness-example';
9+
import {OverlayContainer} from '@angular/cdk/overlay';
10+
import {beforeEach} from 'selenium-webdriver/testing';
11+
12+
describe('AutocompleteHarnessExample', () => {
13+
let fixture: ComponentFixture<AutocompleteHarnessExample>;
14+
let loader: HarnessLoader;
15+
let overlayContainer: OverlayContainer;
16+
17+
beforeAll(() => {
18+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
19+
});
20+
21+
beforeEach(
22+
waitForAsync(() => {
23+
TestBed.configureTestingModule({
24+
imports: [MatAutocompleteModule],
25+
declarations: [AutocompleteHarnessExample]
26+
}).compileComponents();
27+
}));
28+
29+
beforeEach( () => {
30+
fixture = TestBed.createComponent(AutocompleteHarnessExample);
31+
fixture.detectChanges();
32+
loader = TestbedHarnessEnvironment.loader(fixture);
33+
inject([OverlayContainer], (oc: OverlayContainer) => {
34+
overlayContainer = oc;
35+
})();
36+
});
37+
38+
afterEach(() => {
39+
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
40+
overlayContainer.ngOnDestroy();
41+
overlayContainer = null!;
42+
});
43+
44+
it('should load all autocomplete harnesses', async () => {
45+
const autocompletes = await loader.getAllHarnesses(MatAutocompleteHarness);
46+
expect(autocompletes.length).toBe(2);
47+
}
48+
);
49+
50+
it('should get disabled state', async () => {
51+
const enabled = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
52+
const disabled = await loader.getHarness(MatAutocompleteHarness.with({selector: '#disabled'}));
53+
54+
expect(await enabled.isDisabled()).toBe(false);
55+
expect(await disabled.isDisabled()).toBe(true);
56+
});
57+
58+
it('should focus and blur an input', async () => {
59+
const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
60+
expect(await input.isFocused()).toBe(false);
61+
await input.focus();
62+
expect(await input.isFocused()).toBe(true);
63+
await input.blur();
64+
expect(await input.isFocused()).toBe(false);
65+
});
66+
67+
it('should be able to type in an input', async () => {
68+
const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
69+
await input.enterText('Hello there');
70+
expect(await input.getValue()).toBe('Hello there');
71+
});
72+
73+
it('should be able to get filtered options', async () => {
74+
const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
75+
await input.focus();
76+
const options = await input.getOptions({text: /New/});
77+
78+
expect(options.length).toBe(1);
79+
expect(await options[0].getText()).toBe('New York');
80+
});
81+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatAutocompleteHarness
5+
*/
6+
@Component({
7+
selector: 'autocomplete-harness-example',
8+
templateUrl: 'autocomplete-harness-example.html'
9+
})
10+
export class AutocompleteHarnessExample {
11+
states = [
12+
{code: 'AL', name: 'Alabama'},
13+
{code: 'CA', name: 'California'},
14+
{code: 'FL', name: 'Florida'},
15+
{code: 'KS', name: 'Kansas'},
16+
{code: 'MA', name: 'Massachusetts'},
17+
{code: 'NY', name: 'New York'},
18+
{code: 'OR', name: 'Oregon'},
19+
{code: 'PA', name: 'Pennsylvania'},
20+
{code: 'TN', name: 'Tennessee'},
21+
{code: 'VA', name: 'Virginia'},
22+
{code: 'WY', name: 'Wyoming'},
23+
];
24+
}

src/components-examples/material/autocomplete/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import {
1616
AutocompletePlainInputExample
1717
} from './autocomplete-plain-input/autocomplete-plain-input-example';
1818
import {AutocompleteSimpleExample} from './autocomplete-simple/autocomplete-simple-example';
19+
import {AutocompleteHarnessExample} from './autocomplete-harness/autocomplete-harness-example';
1920

2021
export {
2122
AutocompleteAutoActiveFirstOptionExample,
2223
AutocompleteDisplayExample,
2324
AutocompleteFilterExample,
25+
AutocompleteHarnessExample,
2426
AutocompleteOptgroupExample,
2527
AutocompleteOverviewExample,
2628
AutocompletePlainInputExample,
@@ -31,6 +33,7 @@ const EXAMPLES = [
3133
AutocompleteAutoActiveFirstOptionExample,
3234
AutocompleteDisplayExample,
3335
AutocompleteFilterExample,
36+
AutocompleteHarnessExample,
3437
AutocompleteOptgroupExample,
3538
AutocompleteOverviewExample,
3639
AutocompletePlainInputExample,

0 commit comments

Comments
 (0)