Skip to content

Commit

Permalink
reverse snack-bar/testing => legacy-snack-bar/testing dep
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Aug 15, 2022
1 parent b63f32b commit 0f4deef
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatLegacySnackBarHarness} from '@angular/material/legacy-snack-bar/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {MatLegacySnackBarModule} from '@angular/material/legacy-snack-bar';
import {SnackBarHarnessExample} from './snack-bar-harness-example';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MatLegacySnackBarHarness} from '@angular/material/legacy-snack-bar/testing';

describe('SnackBarHarnessExample', () => {
let fixture: ComponentFixture<SnackBarHarnessExample>;
Expand Down
22 changes: 3 additions & 19 deletions src/material/legacy-snack-bar/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ts_library(
deps = [
"//src/cdk/a11y",
"//src/cdk/testing",
"//src/material/snack-bar/testing",
],
)

Expand All @@ -19,32 +20,15 @@ filegroup(
srcs = glob(["**/*.ts"]),
)

ng_test_library(
name = "harness_tests_lib",
srcs = ["shared.spec.ts"],
deps = [
":testing",
"//src/cdk/overlay",
"//src/cdk/testing",
"//src/cdk/testing/private",
"//src/cdk/testing/testbed",
"//src/material/legacy-snack-bar",
"@npm//@angular/platform-browser",
],
)

ng_test_library(
name = "unit_tests_lib",
srcs = glob(
["**/*.spec.ts"],
exclude = ["shared.spec.ts"],
),
srcs = glob(["**/*.spec.ts"]),
deps = [
":harness_tests_lib",
":testing",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/legacy-snack-bar",
"//src/material/snack-bar/testing:harness_tests_lib",
"@npm//@angular/platform-browser",
],
)
Expand Down
2 changes: 1 addition & 1 deletion src/material/legacy-snack-bar/testing/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

export * from './snack-bar-harness';
export * from './snack-bar-harness-filters';
export {SnackBarHarnessFilters as LegacySnackBarHarnessFilters} from '@angular/material/snack-bar/testing';
10 changes: 7 additions & 3 deletions src/material/legacy-snack-bar/testing/snack-bar-harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import {
MatLegacySnackBarConfig,
MatLegacySnackBarModule,
} from '@angular/material/legacy-snack-bar';
import {runHarnessTests} from '@angular/material/legacy-snack-bar/testing/shared.spec';
import {MatLegacySnackBarHarness} from './snack-bar-harness';
import {runHarnessTests} from '@angular/material/snack-bar/testing/shared.spec';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component, TemplateRef, ViewChild} from '@angular/core';
import {MatLegacySnackBarHarness} from './snack-bar-harness';

describe('Non-MDC-based MatSnackBarHarness', () => {
runHarnessTests(MatLegacySnackBarModule, MatLegacySnackBar, MatLegacySnackBarHarness);
runHarnessTests(
MatLegacySnackBarModule,
MatLegacySnackBar as any,
MatLegacySnackBarHarness as any,
);
});

describe('Non-MDC-based MatSnackBarHarness (non-MDC only behavior)', () => {
Expand Down
129 changes: 21 additions & 108 deletions src/material/legacy-snack-bar/testing/snack-bar-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,131 +5,44 @@
* 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
*/

import {AriaLivePoliteness} from '@angular/cdk/a11y';
import {ContentContainerComponentHarness, HarnessPredicate, parallel} from '@angular/cdk/testing';
import {LegacySnackBarHarnessFilters} from './snack-bar-harness-filters';
import {ComponentHarnessConstructor, HarnessPredicate} from '@angular/cdk/testing';
import {
MatSnackBarHarness as SnackBarHarnessBase,
SnackBarHarnessFilters,
} from '@angular/material/snack-bar/testing';

/** Harness for interacting with a standard mat-snack-bar in tests. */
export class MatLegacySnackBarHarness extends ContentContainerComponentHarness<string> {
// Developers can provide a custom component or template for the
// snackbar. The canonical snack-bar parent is the "MatSnackBarContainer".
export class MatLegacySnackBarHarness extends SnackBarHarnessBase {
// Developers can provide a custom component or template for the snackbar. The canonical snack-bar
// parent is the "MatSnackBarContainer". We use `:not([mat-exit])` to exclude snack bars that
// are in the process of being dismissed, because the element only gets removed after the
// animation is finished and since it runs outside of Angular, we don't have a way of being
// notified when it's done.
/** The selector for the host element of a `MatSnackBar` instance. */
static hostSelector = '.mat-snack-bar-container';
protected _messageSelector = '.mat-simple-snackbar > span';
protected _actionButtonSelector = '.mat-simple-snackbar-action > button';
private _snackBarLiveRegion = this.locatorFor('[aria-live]');
static override hostSelector = '.mat-snack-bar-container';
protected override _messageSelector = '.mat-simple-snackbar > span';
protected override _actionButtonSelector = '.mat-simple-snackbar-action > button';

/**
* Gets a `HarnessPredicate` that can be used to search for a `MatSnackBarHarness` that meets
* certain criteria.
* Gets a `HarnessPredicate` that can be used to search for a snack bar with specific attributes.
* @param options Options for filtering which snack bar instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(
options: LegacySnackBarHarnessFilters = {},
): HarnessPredicate<MatLegacySnackBarHarness> {
return new HarnessPredicate(MatLegacySnackBarHarness, options);
}

/**
* Gets the role of the snack-bar. The role of a snack-bar is determined based
* on the ARIA politeness specified in the snack-bar config.
* @deprecated Use `getAriaLive` instead.
* @breaking-change 13.0.0
*/
async getRole(): Promise<'alert' | 'status' | null> {
return (await this.host()).getAttribute('role') as Promise<'alert' | 'status' | null>;
}

/**
* Gets the aria-live of the snack-bar's live region. The aria-live of a snack-bar is
* determined based on the ARIA politeness specified in the snack-bar config.
*/
async getAriaLive(): Promise<AriaLivePoliteness> {
return (await this._snackBarLiveRegion()).getAttribute(
'aria-live',
) as Promise<AriaLivePoliteness>;
}

/**
* Whether the snack-bar has an action. Method cannot be used for snack-bar's with custom content.
*/
async hasAction(): Promise<boolean> {
await this._assertContentAnnotated();
return (await this._getActionButton()) !== null;
}

/**
* Gets the description of the snack-bar. Method cannot be used for snack-bar's without action or
* with custom content.
*/
async getActionDescription(): Promise<string> {
await this._assertHasAction();
return (await this._getActionButton())!.text();
}

/**
* Dismisses the snack-bar by clicking the action button. Method cannot be used for snack-bar's
* without action or with custom content.
*/
async dismissWithAction(): Promise<void> {
await this._assertHasAction();
await (await this._getActionButton())!.click();
}

/**
* Gets the message of the snack-bar. Method cannot be used for snack-bar's with custom content.
*/
async getMessage(): Promise<string> {
await this._assertContentAnnotated();
return (await this.locatorFor(this._messageSelector)()).text();
}

/** Gets whether the snack-bar has been dismissed. */
async isDismissed(): Promise<boolean> {
// We consider the snackbar dismissed if it's not in the DOM. We can assert that the
// element isn't in the DOM by seeing that its width and height are zero.

const host = await this.host();
const [exit, dimensions] = await parallel(() => [
// The snackbar container is marked with the "exit" attribute after it has been dismissed
// but before the animation has finished (after which it's removed from the DOM).
host.getAttribute('mat-exit'),
host.getDimensions(),
]);

return exit != null || (!!dimensions && dimensions.height === 0 && dimensions.width === 0);
static override with<T extends MatLegacySnackBarHarness>(
this: ComponentHarnessConstructor<T>,
options: SnackBarHarnessFilters = {},
): HarnessPredicate<T> {
return new HarnessPredicate(this, options);
}

/**
* Asserts that the current snack-bar has annotated content. Promise reject
* if content is not annotated.
*/
protected async _assertContentAnnotated(): Promise<void> {
protected override async _assertContentAnnotated() {
if (!(await this._isSimpleSnackBar())) {
throw Error('Method cannot be used for snack-bar with custom content.');
}
}

/**
* Asserts that the current snack-bar has an action defined. Otherwise the
* promise will reject.
*/
protected async _assertHasAction(): Promise<void> {
await this._assertContentAnnotated();
if (!(await this.hasAction())) {
throw Error('Method cannot be used for a snack-bar without an action.');
}
}

/** Whether the snack-bar is using the default content template. */
private async _isSimpleSnackBar(): Promise<boolean> {
return (await this.locatorForOptional('.mat-simple-snackbar')()) !== null;
}

/** Gets the simple snack bar action button. */
private async _getActionButton() {
return this.locatorForOptional(this._actionButtonSelector)();
}
}
23 changes: 20 additions & 3 deletions src/material/snack-bar/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ ts_library(
exclude = ["**/*.spec.ts"],
),
deps = [
"//src/cdk/a11y",
"//src/cdk/testing",
"//src/material/legacy-snack-bar/testing",
],
)

Expand All @@ -19,14 +19,31 @@ filegroup(
srcs = glob(["**/*.ts"]),
)

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

ng_test_library(
name = "unit_tests_lib",
srcs = glob(["**/*.spec.ts"]),
srcs = glob(
["**/*.spec.ts"],
exclude = ["shared.spec.ts"],
),
deps = [
":harness_tests_lib",
":testing",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/legacy-snack-bar/testing:harness_tests_lib",
"//src/material/snack-bar",
"@npm//@angular/platform-browser",
],
Expand Down
2 changes: 1 addition & 1 deletion src/material/snack-bar/testing/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

export * from './snack-bar-harness';
export {LegacySnackBarHarnessFilters} from '@angular/material/legacy-snack-bar/testing';
export * from './snack-bar-harness-filters';
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ import {HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component, TemplateRef, ViewChild, Injector} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {
MatLegacySnackBar,
MatLegacySnackBarConfig,
MatLegacySnackBarModule,
} from '@angular/material/legacy-snack-bar';
import {MatSnackBar, MatLegacySnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MatLegacySnackBarHarness} from './snack-bar-harness';
import {MatSnackBarHarness} from './snack-bar-harness';

/**
* Function that can be used to run the shared snack-bar harness tests for either
* the non-MDC or MDC based snack-bar harness.
*/
export function runHarnessTests(
snackBarModule: typeof MatLegacySnackBarModule,
snackBarToken: typeof MatLegacySnackBar,
snackBarHarness: typeof MatLegacySnackBarHarness,
snackBarModule: typeof MatSnackBarModule,
snackBarToken: typeof MatSnackBar,
snackBarHarness: typeof MatSnackBarHarness,
) {
let fixture: ComponentFixture<SnackbarHarnessTest>;
let loader: HarnessLoader;
Expand Down Expand Up @@ -142,7 +138,7 @@ export function runHarnessTests(
})
class SnackbarHarnessTest {
@ViewChild(TemplateRef) customTmpl: TemplateRef<any>;
snackBar: MatLegacySnackBar;
snackBar: MatSnackBar;

constructor(injector: Injector) {
this.snackBar = injector.get(snackBarToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
import {BaseHarnessFilters} from '@angular/cdk/testing';

/** A set of criteria that can be used to filter a list of `MatSnackBarHarness` instances. */
export interface LegacySnackBarHarnessFilters extends BaseHarnessFilters {}
export interface SnackBarHarnessFilters extends BaseHarnessFilters {}
6 changes: 3 additions & 3 deletions src/material/snack-bar/testing/snack-bar-harness.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {MatSnackBar, MatLegacySnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar';
import {runHarnessTests} from '@angular/material/legacy-snack-bar/testing/shared.spec';
import {MatSnackBarHarness} from './snack-bar-harness';
import {runHarnessTests} from './shared.spec';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component, TemplateRef, ViewChild} from '@angular/core';
import {MatSnackBarHarness} from './snack-bar-harness';

describe('MDC-based MatSnackBarHarness', () => {
runHarnessTests(MatSnackBarModule, MatSnackBar as any, MatSnackBarHarness as any);
runHarnessTests(MatSnackBarModule, MatSnackBar, MatSnackBarHarness);
});

describe('MDC-based MatSnackBarHarness (MDC only behavior)', () => {
Expand Down
Loading

0 comments on commit 0f4deef

Please sign in to comment.