Skip to content

feat(autocomplete): move harness out of experimental #17080

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

Merged
merged 2 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 0 additions & 38 deletions src/material-experimental/mdc-autocomplete/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ng_module(
["**/*.ts"],
exclude = [
"**/*.spec.ts",
"harness/**",
],
),
assets = [
Expand All @@ -22,18 +21,6 @@ ng_module(
],
)

ts_library(
name = "harness",
srcs = glob(
["harness/**/*.ts"],
exclude = ["**/*.spec.ts"],
),
deps = [
"//src/cdk/coercion",
"//src/cdk/testing",
],
)

sass_library(
name = "mdc_autocomplete_scss_lib",
srcs = glob(["**/_*.scss"]),
Expand All @@ -57,31 +44,6 @@ sass_binary(
],
)

ng_test_library(
name = "autocomplete_tests_lib",
srcs = [
"harness/autocomplete-harness.spec.ts",
],
deps = [
":harness",
":mdc-autocomplete",
"//src/cdk/overlay",
"//src/cdk/platform",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/autocomplete",
"@npm//@angular/platform-browser",
],
)

ng_web_test_suite(
name = "unit_tests",
deps = [
":autocomplete_tests_lib",
"//src/material-experimental:mdc_require_config.js",
],
)

ng_e2e_test_library(
name = "e2e_test_sources",
srcs = glob(["**/*.e2e.spec.ts"]),
Expand Down

This file was deleted.

14 changes: 0 additions & 14 deletions src/material-experimental/mdc-checkbox/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ng_module(
["**/*.ts"],
exclude = [
"**/*.spec.ts",
"harness/**",
],
),
assets = [":checkbox_scss"] + glob(["**/*.html"]),
Expand All @@ -29,18 +28,6 @@ ng_module(
],
)

ts_library(
name = "harness",
srcs = glob(
["harness/**/*.ts"],
exclude = ["**/*.spec.ts"],
),
deps = [
"//src/cdk/coercion",
"//src/cdk/testing",
],
)

sass_library(
name = "mdc_checkbox_scss_lib",
srcs = glob(["**/_*.scss"]),
Expand Down Expand Up @@ -69,7 +56,6 @@ ng_test_library(
exclude = ["**/*.e2e.spec.ts"],
),
deps = [
":harness",
":mdc-checkbox",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
Expand Down
48 changes: 48 additions & 0 deletions src/material/autocomplete/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package(default_visibility = ["//visibility:public"])

load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")

ng_module(
name = "testing",
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
module_name = "@angular/material/autocomplete/testing",
deps = [
"//src/cdk/coercion",
"//src/cdk/testing",
],
)

ng_test_library(
name = "shared_unit_tests",
srcs = ["shared.spec.ts"],
deps = [
":testing",
"//src/cdk/overlay",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/autocomplete",
"@npm//@angular/forms",
"@npm//@angular/platform-browser",
],
)

ng_test_library(
name = "unit_tests_lib",
srcs = glob(
["**/*.spec.ts"],
exclude = ["shared.spec.ts"],
),
deps = [
":shared_unit_tests",
":testing",
"//src/material/autocomplete",
],
)

ng_web_test_suite(
name = "unit_tests",
deps = [":unit_tests_lib"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {MatAutocompleteModule} from '@angular/material/autocomplete';
import {runTests} from '@angular/material/autocomplete/testing/shared.spec';
import {MatAutocompleteHarness} from './autocomplete-harness';

describe('Non-MDC-based MatAutocompleteHarness', () => {
runTests(MatAutocompleteModule, MatAutocompleteHarness);
});
9 changes: 9 additions & 0 deletions src/material/autocomplete/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './public-api';
10 changes: 10 additions & 0 deletions src/material/autocomplete/testing/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './autocomplete-harness';
export * from './autocomplete-harness-filters';
Original file line number Diff line number Diff line change
@@ -1,84 +1,62 @@
import {OverlayContainer} from '@angular/cdk/overlay';
import {HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component, Type} from '@angular/core';
import {ComponentFixture, TestBed, inject} from '@angular/core/testing';
import {Component} from '@angular/core';
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {MatAutocompleteModule} from '@angular/material/autocomplete';
import {OverlayContainer} from '@angular/cdk/overlay';
import {MatAutocompleteModule as MatMdcAutocompleteModule} from '../index';
import {MatAutocompleteHarness} from './autocomplete-harness';
import {MatAutocompleteHarness as MatMdcAutocompleteHarness} from './mdc-autocomplete-harness';

let fixture: ComponentFixture<AutocompleteHarnessTest>;
let loader: HarnessLoader;
let harness: typeof MatAutocompleteHarness;
let overlayContainer: OverlayContainer;

describe('MatAutocompleteHarness', () => {
describe('non-MDC-based', () => {
beforeEach(async () => {
await prepareTests(MatAutocompleteModule, AutocompleteHarnessTest);
harness = MatAutocompleteHarness;
});

runTests();
});

describe('MDC-based', () => {
beforeEach(async () => {
await prepareTests(MatMdcAutocompleteModule, AutocompleteHarnessTest);
// Public APIs are the same as MatAutocompleteHarness, but cast
// is necessary because of different private fields.
harness = MatMdcAutocompleteHarness as any;
});

// TODO: enable after MDC autocomplete is implemented
// runTests();
import {MatAutocompleteHarness} from '@angular/material/autocomplete/testing';

/**
* Function that can be used to run the shared autocomplete harness tests for either the non-MDC or
* MDC based checkbox harness.
*/
export function runTests(
autocompleteModule: typeof MatAutocompleteModule,
autocompleteHarness: typeof MatAutocompleteHarness) {
let fixture: ComponentFixture<AutocompleteHarnessTest>;
let loader: HarnessLoader;
let overlayContainer: OverlayContainer;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [autocompleteModule],
declarations: [AutocompleteHarnessTest],
}).compileComponents();

fixture = TestBed.createComponent(AutocompleteHarnessTest);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainer = oc;
})();
});
});

/** Shared test setup logic. */
async function prepareTests(moduleType: Type<any>, fixtureType: Type<any>) {
await TestBed.configureTestingModule({
imports: [moduleType],
declarations: [fixtureType],
}).compileComponents();

fixture = TestBed.createComponent(fixtureType);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainer = oc;
})();
}

/** Shared tests to run on both the original and MDC-based autocomplete. */
function runTests() {
afterEach(() => {
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
overlayContainer.ngOnDestroy();
overlayContainer = null!;
});

it('should load all autocomplete harnesses', async () => {
const inputs = await loader.getAllHarnesses(harness);
const inputs = await loader.getAllHarnesses(autocompleteHarness);
expect(inputs.length).toBe(5);
});

it('should be able to get text inside the input', async () => {
const input = await loader.getHarness(harness.with({selector: '#prefilled'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#prefilled'}));
expect(await input.getText()).toBe('Prefilled value');
});

it('should get disabled state', async () => {
const enabled = await loader.getHarness(harness.with({selector: '#plain'}));
const disabled = await loader.getHarness(harness.with({selector: '#disabled'}));
const enabled = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
const disabled = await loader.getHarness(autocompleteHarness.with({selector: '#disabled'}));

expect(await enabled.isDisabled()).toBe(false);
expect(await disabled.isDisabled()).toBe(true);
});

it('should focus and blur an input', async () => {
const input = await loader.getHarness(harness.with({selector: '#plain'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
expect(getActiveElementId()).not.toBe('plain');
await input.focus();
expect(getActiveElementId()).toBe('plain');
Expand All @@ -87,19 +65,19 @@ function runTests() {
});

it('should be able to type in an input', async () => {
const input = await loader.getHarness(harness.with({selector: '#plain'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
await input.enterText('Hello there');
expect(await input.getText()).toBe('Hello there');
});

it('should be able to get the autocomplete panel', async () => {
const input = await loader.getHarness(harness.with({selector: '#plain'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
await input.focus();
expect(await input.getPanel()).toBeTruthy();
});

it('should be able to get the autocomplete panel options', async () => {
const input = await loader.getHarness(harness.with({selector: '#plain'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
await input.focus();
const options = await input.getOptions();

Expand All @@ -108,7 +86,7 @@ function runTests() {
});

it('should be able to get the autocomplete panel groups', async () => {
const input = await loader.getHarness(harness.with({selector: '#grouped'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#grouped'}));
await input.focus();
const groups = await input.getOptionGroups();
const options = await input.getOptions();
Expand All @@ -122,19 +100,18 @@ function runTests() {
fixture.componentInstance.states = [];
fixture.detectChanges();

const input = await loader.getHarness(harness.with({selector: '#plain'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
await input.focus();
expect(await input.isPanelVisible()).toBe(false);
});

it('should be able to get whether the autocomplete is open', async () => {
const input = await loader.getHarness(harness.with({selector: '#plain'}));
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));

expect(await input.isOpen()).toBe(false);
await input.focus();
expect(await input.isOpen()).toBe(true);
});

}

function getActiveElementId() {
Expand Down Expand Up @@ -192,4 +169,3 @@ class AutocompleteHarnessTest {
}
];
}

6 changes: 4 additions & 2 deletions test/karma-system-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ System.config({
'@angular/cdk-experimental/scrolling': 'dist/packages/cdk-experimental/scrolling/index.js',

'@angular/material/autocomplete': 'dist/packages/material/autocomplete/index.js',
'@angular/material/autocomplete/testing': 'dist/packages/material/autocomplete/testing/index.js',
'@angular/material/autocomplete/testing/shared.spec': 'dist/packages/material/autocomplete/testing/shared.spec.js',
'@angular/material/badge': 'dist/packages/material/badge/index.js',
'@angular/material/bottom-sheet': 'dist/packages/material/bottom-sheet/index.js',
'@angular/material/button': 'dist/packages/material/button/index.js',
Expand Down Expand Up @@ -134,14 +136,14 @@ System.config({
'@angular/material/tooltip': 'dist/packages/material/tooltip/index.js',
'@angular/material/tree': 'dist/packages/material/tree/index.js',

'@angular/material-experimental/mdc-autocomplete':
'dist/packages/material-experimental/mdc-autocomplete/index.js',
'@angular/material-experimental/mdc-button':
'dist/packages/material-experimental/mdc-button/index.js',
'@angular/material-experimental/mdc-card':
'dist/packages/material-experimental/mdc-card/index.js',
'@angular/material-experimental/mdc-checkbox':
'dist/packages/material-experimental/mdc-checkbox/index.js',
'@angular/material-experimental/mdc-checkbox/testing':
'dist/packages/material-experimental/mdc-checkbox/testing/index.js',
'@angular/material-experimental/mdc-chips':
'dist/packages/material-experimental/mdc-chips/index.js',
'@angular/material-experimental/mdc-helpers':
Expand Down