Skip to content

(test): add e2e tests for autocomplete (#4491) #9077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@

# E2E app
/e2e/* @jelbourn
/e2e/components/autocomplete-e2e.spec.ts @crisbeto
/e2e/components/block-scroll-strategy-e2e.spec.ts @andrewseguin @crisbeto
/e2e/components/button-e2e.spec.ts @tinayuangao
/e2e/components/button-toggle-e2e.spec.ts @tinayuangao
Expand All @@ -152,6 +153,7 @@
/e2e/components/toolbar-e2e.spec.ts @devversion
/e2e/util/** @jelbourn
/src/e2e-app/* @jelbourn
/src/e2e-app/autocomplete/** @crisbeto
/src/e2e-app/block-scroll-strategy/** @andrewseguin @crisbeto
/src/e2e-app/button/** @tinayuangao
/src/e2e-app/checkbox/** @tinayuangao @devversion
Expand Down
60 changes: 60 additions & 0 deletions e2e/components/autocomplete-e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {browser, by, element} from 'protractor';
import {screenshot} from '../screenshot';
import {expectToExist} from '../util/index';


describe('autocomplete', () => {
const autocompletePanelSelector = '.mat-autocomplete-panel';
let page: AutocompletePage;

beforeEach(() => page = new AutocompletePage());

it('should open the panel when the input is focused', async () => {
expectToExist(autocompletePanelSelector, false);
page.autocompleteInput().click();

expectToExist(autocompletePanelSelector);
expect(await page.autocompletePanel().getText()).toEqual('One\nTwo\nThree\nFour');
screenshot();
});

it('should close the panel when an option is clicked', async () => {
page.autocompleteInput().click();
page.option('One').click();

expectToExist(autocompletePanelSelector, false);
screenshot();
});

it('should set the selected option text to the input', async () => {
page.autocompleteInput().click();
page.option('One').click();

expect(await page.getInputText()).toEqual('One');
});

it('should trigger filtering with proper key', async () => {
page.autocompleteInput().sendKeys('T');

expect(await page.autocompletePanel().getText()).toEqual('Two\nThree');
screenshot();
});
});

class AutocompletePage {
constructor() {
browser.get('/autocomplete');
}
autocompleteInput() {
return element(by.id('autocomplete-input'));
}
autocompletePanel() {
return element(by.css('.mat-autocomplete-panel'));
}
option(text: string) {
return element(by.cssContainingText('mat-option', text));
}
getInputText() {
return this.autocompleteInput().getAttribute('value');
}
}
2 changes: 1 addition & 1 deletion src/cdk/accordion/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

export {CdkAccordionItem} from './accordion-item';
export {CdkAccordionItem} from './accordion-item';
export {CdkAccordion} from './accordion';
export * from './accordion-module';
11 changes: 11 additions & 0 deletions src/e2e-app/autocomplete/autocomplete-e2e.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<section>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that's not clear in the code by itself is that we're trying to move away from standalone e2e test pages. Instead, new e2e tests are based on the examples we publish to material.angular.io. Check out #5735 for an example of adding e2e tests for cards based on the published examples.

<mat-form-field>
<input id="autocomplete-input" matInput placeholder="Choose" [matAutocomplete]="auto" [formControl]="optionCtrl">
</mat-form-field>

<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</section>
37 changes: 37 additions & 0 deletions src/e2e-app/autocomplete/autocomplete-e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import {startWith} from 'rxjs/operators/startWith';
import {map} from 'rxjs/operators/map';
import {
Component,
QueryList,
ViewChild,
ViewChildren
} from '@angular/core';
import {
MatAutocompleteTrigger,
MatOption
} from '@angular/material';


@Component({
moduleId: module.id,
selector: 'autocomplete-e2e',
templateUrl: 'autocomplete-e2e.html',
})
export class AutocompleteE2E {
optionCtrl = new FormControl();
filteredOptions: Observable<string[]>;
options = ['One', 'Two', 'Three', 'Four'];

@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
@ViewChildren(MatOption) matOptions: QueryList<MatOption>;

constructor() {
this.filteredOptions = this.optionCtrl.valueChanges.pipe(
startWith(null),
map((val: string) => {
return val ? this.options.filter(option => option.startsWith(val)) : this.options.slice();
}));
}
}
8 changes: 6 additions & 2 deletions src/e2e-app/e2e-app-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {SlideToggleE2E} from './slide-toggle/slide-toggle-e2e';
import {InputE2E} from './input/input-e2e';
import {SidenavE2E} from './sidenav/sidenav-e2e';
import {BlockScrollStrategyE2E} from './block-scroll-strategy/block-scroll-strategy-e2e';
import {AutocompleteE2E} from './autocomplete/autocomplete-e2e';
import {
MatAutocompleteModule,
MatButtonModule,
MatCheckboxModule,
MatDialogModule,
Expand Down Expand Up @@ -47,6 +49,7 @@ import {ReactiveFormsModule} from '@angular/forms';
*/
@NgModule({
exports: [
MatAutocompleteModule,
MatButtonModule,
MatCheckboxModule,
MatDialogModule,
Expand Down Expand Up @@ -78,8 +81,10 @@ export class E2eMaterialModule {}
ReactiveFormsModule
],
declarations: [
AutocompleteE2E,
BasicTabs,
ButtonE2E,
BlockScrollStrategyE2E,
DialogE2E,
E2EApp,
FullscreenE2E,
Expand All @@ -95,8 +100,7 @@ export class E2eMaterialModule {}
SimpleRadioButtons,
SlideToggleE2E,
TestDialog,
TestDialogFullScreen,
BlockScrollStrategyE2E
TestDialogFullScreen
],
bootstrap: [E2EApp],
providers: [
Expand Down
1 change: 1 addition & 0 deletions src/e2e-app/e2e-app/e2e-app.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<button (click)="showLinks = !showLinks">Toggle Navigation Links</button>

<mat-nav-list *ngIf="showLinks">
<a mat-list-item [routerLink]="['autocomplete']">Autocomplete</a>
<a mat-list-item [routerLink]="['block-scroll-strategy']">Block scroll strategy</a>
<a mat-list-item [routerLink]="['button']">Button</a>
<a mat-list-item [routerLink]="['button-toggle']">Button Toggle</a>
Expand Down
2 changes: 2 additions & 0 deletions src/e2e-app/e2e-app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {FullscreenE2E} from '../fullscreen/fullscreen-e2e';
import {InputE2E} from '../input/input-e2e';
import {SidenavE2E} from '../sidenav/sidenav-e2e';
import {BlockScrollStrategyE2E} from '../block-scroll-strategy/block-scroll-strategy-e2e';
import {AutocompleteE2E} from '../autocomplete/autocomplete-e2e';
import {
CardFancyExample,
ListOverviewExample,
Expand All @@ -26,6 +27,7 @@ import {

export const E2E_APP_ROUTES: Routes = [
{path: '', component: Home},
{path: 'autocomplete', component: AutocompleteE2E},
{path: 'block-scroll-strategy', component: BlockScrollStrategyE2E},
{path: 'button', component: ButtonE2E},
{path: 'button-toggle', component: ButtonToggleOverviewExample},
Expand Down