Skip to content

Commit 13f89a1

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

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-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

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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
11+
describe('AutocompleteHarnessExample', () => {
12+
let fixture: ComponentFixture<AutocompleteHarnessExample>;
13+
let loader: HarnessLoader;
14+
let overlayContainer: OverlayContainer;
15+
16+
beforeAll(() => {
17+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
18+
});
19+
20+
beforeEach(
21+
waitForAsync(() => {
22+
TestBed.configureTestingModule({
23+
imports: [MatAutocompleteModule],
24+
declarations: [AutocompleteHarnessExample]
25+
}).compileComponents();
26+
fixture = TestBed.createComponent(AutocompleteHarnessExample);
27+
fixture.detectChanges();
28+
loader = TestbedHarnessEnvironment.loader(fixture);
29+
inject([OverlayContainer], (oc: OverlayContainer) => {
30+
overlayContainer = oc;
31+
})();
32+
})
33+
);
34+
35+
afterEach(() => {
36+
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
37+
overlayContainer.ngOnDestroy();
38+
overlayContainer = null!;
39+
});
40+
41+
it('should load all autocomplete harnesses', async () => {
42+
const autocompletes = await loader.getAllHarnesses(MatAutocompleteHarness);
43+
expect(autocompletes.length).toBe(2);
44+
}
45+
);
46+
47+
it('should get disabled state', async () => {
48+
const enabled = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
49+
const disabled = await loader.getHarness(MatAutocompleteHarness.with({selector: '#disabled'}));
50+
51+
expect(await enabled.isDisabled()).toBe(false);
52+
expect(await disabled.isDisabled()).toBe(true);
53+
});
54+
55+
it('should focus and blur an input', async () => {
56+
const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
57+
expect(await input.isFocused()).toBe(false);
58+
await input.focus();
59+
expect(await input.isFocused()).toBe(true);
60+
await input.blur();
61+
expect(await input.isFocused()).toBe(false);
62+
});
63+
64+
it('should be able to type in an input', async () => {
65+
const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
66+
await input.enterText('Hello there');
67+
expect(await input.getValue()).toBe('Hello there');
68+
});
69+
70+
it('should be able to get filtered options', async () => {
71+
const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'}));
72+
await input.focus();
73+
const options = await input.getOptions({text: /New/});
74+
75+
expect(options.length).toBe(1);
76+
expect(await options[0].getText()).toBe('New York');
77+
});
78+
});
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)