Skip to content

Commit 66a770c

Browse files
annieywwagnermaciel
authored andcommitted
docs(material/autocomplete): add autocomplete harness example (#20830)
(cherry picked from commit e2ebfe0)
1 parent 0b91341 commit 66a770c

File tree

5 files changed

+120
-0
lines changed

5 files changed

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