Skip to content
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

docs(material/snack-bar): add harness example for snack bar #21790

Merged
merged 1 commit into from
Feb 5, 2021
Merged
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
33 changes: 31 additions & 2 deletions src/components-examples/material/snack-bar/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
load("//tools:defaults.bzl", "ng_module")
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")

package(default_visibility = ["//visibility:public"])

ng_module(
name = "snack-bar",
srcs = glob(["**/*.ts"]),
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
assets = glob([
"**/*.html",
"**/*.css",
]),
module_name = "@angular/components-examples/material/snack-bar",
deps = [
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/button",
"//src/material/input",
"//src/material/select",
"//src/material/snack-bar",
"//src/material/snack-bar/testing",
"@npm//@angular/forms",
"@npm//@angular/platform-browser",
"@npm//@angular/platform-browser-dynamic",
"@npm//@types/jasmine",
],
)

Expand All @@ -27,3 +36,23 @@ filegroup(
"**/*.ts",
]),
)

ng_test_library(
name = "unit_tests_lib",
srcs = glob(["**/*.spec.ts"]),
deps = [
":snack-bar",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/snack-bar",
"//src/material/snack-bar/testing",
"@npm//@angular/platform-browser",
"@npm//@angular/platform-browser-dynamic",
],
)

ng_web_test_suite(
name = "unit_tests",
exclude_init_script = True,
deps = [":unit_tests_lib"],
)
3 changes: 3 additions & 0 deletions src/components-examples/material/snack-bar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import {
} from './snack-bar-component/snack-bar-component-example';
import {SnackBarOverviewExample} from './snack-bar-overview/snack-bar-overview-example';
import {SnackBarPositionExample} from './snack-bar-position/snack-bar-position-example';
import {SnackBarHarnessExample} from './snack-bar-harness/snack-bar-harness-example';

export {
SnackBarComponentExample,
SnackBarHarnessExample,
SnackBarOverviewExample,
SnackBarPositionExample,
PizzaPartyComponent,
};

const EXAMPLES = [
SnackBarComponentExample,
SnackBarHarnessExample,
SnackBarOverviewExample,
SnackBarPositionExample,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ng-template>Hello from the snackbar</ng-template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {TestBed, ComponentFixture} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatSnackBarHarness} from '@angular/material/snack-bar/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
import {MatSnackBarModule} from '@angular/material/snack-bar';
import {SnackBarHarnessExample} from './snack-bar-harness-example';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';


describe('SnackBarHarnessExample', () => {
let fixture: ComponentFixture<SnackBarHarnessExample>;
let loader: HarnessLoader;

beforeAll(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
});

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MatSnackBarModule, NoopAnimationsModule],
declarations: [SnackBarHarnessExample]
}).compileComponents();
fixture = TestBed.createComponent(SnackBarHarnessExample);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
});

it('should load harness for simple snack-bar', async () => {
const snackBarRef = fixture.componentInstance.open('Hi!', '');
let snackBars = await loader.getAllHarnesses(MatSnackBarHarness);

expect(snackBars.length).toBe(1);

snackBarRef.dismiss();
snackBars = await loader.getAllHarnesses(MatSnackBarHarness);
expect(snackBars.length).toBe(0);
});

it('should be able to get message of simple snack-bar', async () => {
fixture.componentInstance.open('Subscribed to newsletter.');
let snackBar = await loader.getHarness(MatSnackBarHarness);
expect(await snackBar.getMessage()).toBe('Subscribed to newsletter.');
});

it('should be able to get action description of simple snack-bar', async () => {
fixture.componentInstance.open('Hello', 'Unsubscribe');
let snackBar = await loader.getHarness(MatSnackBarHarness);
expect(await snackBar.getActionDescription()).toBe('Unsubscribe');
});

it('should be able to check whether simple snack-bar has action', async () => {
fixture.componentInstance.open('With action', 'Unsubscribe');
let snackBar = await loader.getHarness(MatSnackBarHarness);
expect(await snackBar.hasAction()).toBe(true);

fixture.componentInstance.open('No action');
snackBar = await loader.getHarness(MatSnackBarHarness);
expect(await snackBar.hasAction()).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Component} from '@angular/core';
import {MatSnackBar, MatSnackBarConfig} from '@angular/material/snack-bar';

/**
* @title Testing with MatSnackBarHarness
*/
@Component({
selector: 'snack-bar-harness-example',
templateUrl: 'snack-bar-harness-example.html',
})
export class SnackBarHarnessExample {
constructor(readonly snackBar: MatSnackBar) {}

open(message: string, action = '', config?: MatSnackBarConfig) {
return this.snackBar.open(message, action, config);
}
}