-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
shared.spec.ts
164 lines (135 loc) · 6.45 KB
/
shared.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {OverlayContainer} from '@angular/cdk/overlay';
import {HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component, TemplateRef, ViewChild, Injector} from '@angular/core';
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {MatSnackBar, MatSnackBarConfig, MatSnackBarModule} from '@angular/material/snack-bar';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
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 MatSnackBarModule,
snackBarToken: typeof MatSnackBar,
snackBarHarness: typeof MatSnackBarHarness) {
let fixture: ComponentFixture<SnackbarHarnessTest>;
let loader: HarnessLoader;
let overlayContainer: OverlayContainer;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [snackBarModule, NoopAnimationsModule],
declarations: [SnackbarHarnessTest],
}).compileComponents();
fixture = TestBed.createComponent(SnackbarHarnessTest);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainer = oc;
})();
});
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 harness for simple snack-bar', async () => {
const snackBarRef = fixture.componentInstance.openSimple('Hello!', '');
let snackBars = await loader.getAllHarnesses(snackBarHarness);
expect(snackBars.length).toBe(1);
snackBarRef.dismiss();
snackBars = await loader.getAllHarnesses(snackBarHarness);
expect(snackBars.length).toBe(0);
});
it('should load harness for custom snack-bar', async () => {
const snackBarRef = fixture.componentInstance.openCustom();
let snackBars = await loader.getAllHarnesses(snackBarHarness);
expect(snackBars.length).toBe(1);
snackBarRef.dismiss();
snackBars = await loader.getAllHarnesses(snackBarHarness);
expect(snackBars.length).toBe(0);
});
it('should load snack-bar harness by selector', async () => {
fixture.componentInstance.openSimple('Hello!', '', {panelClass: 'my-snack-bar'});
const snackBars = await loader.getAllHarnesses(snackBarHarness.with({
selector: '.my-snack-bar'
}));
expect(snackBars.length).toBe(1);
});
it('should be able to get role of snack-bar', async () => {
fixture.componentInstance.openCustom();
let snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.getRole()).toBe('alert');
fixture.componentInstance.openCustom({politeness: 'polite'});
snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.getRole()).toBe('status');
fixture.componentInstance.openCustom({politeness: 'off'});
snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.getRole()).toBe(null);
});
it('should be able to get message of simple snack-bar', async () => {
fixture.componentInstance.openSimple('Subscribed to newsletter.');
let snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.getMessage()).toBe('Subscribed to newsletter.');
// For snack-bar's with custom template, the message cannot be
// retrieved. We expect an error to be thrown.
fixture.componentInstance.openCustom();
snackBar = await loader.getHarness(snackBarHarness);
await expectAsync(snackBar.getMessage()).toBeRejectedWithError(/custom content/);
});
it('should be able to get action description of simple snack-bar', async () => {
fixture.componentInstance.openSimple('Hello', 'Unsubscribe');
let snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.getActionDescription()).toBe('Unsubscribe');
// For snack-bar's with custom template, the action description
// cannot be retrieved. We expect an error to be thrown.
fixture.componentInstance.openCustom();
snackBar = await loader.getHarness(snackBarHarness);
await expectAsync(snackBar.getActionDescription()).toBeRejectedWithError(/custom content/);
});
it('should be able to check whether simple snack-bar has action', async () => {
fixture.componentInstance.openSimple('With action', 'Unsubscribe');
let snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.hasAction()).toBe(true);
fixture.componentInstance.openSimple('No action');
snackBar = await loader.getHarness(snackBarHarness);
expect(await snackBar.hasAction()).toBe(false);
// For snack-bar's with custom template, the action cannot
// be found. We expect an error to be thrown.
fixture.componentInstance.openCustom();
snackBar = await loader.getHarness(snackBarHarness);
await expectAsync(snackBar.hasAction()).toBeRejectedWithError(/custom content/);
});
it('should be able to dismiss simple snack-bar with action', async () => {
const snackBarRef = fixture.componentInstance.openSimple('With action', 'Unsubscribe');
let snackBar = await loader.getHarness(snackBarHarness);
let actionCount = 0;
snackBarRef.onAction().subscribe(() => actionCount++);
expect(await snackBar.isDismissed())
.toBe(false, 'The snackbar should be present in the DOM before dismiss');
await snackBar.dismissWithAction();
expect(actionCount).toBe(1);
expect(await snackBar.isDismissed())
.toBe(true, 'The snackbar should be absent from the DOM after dismiss');
fixture.componentInstance.openSimple('No action');
snackBar = await loader.getHarness(snackBarHarness);
await expectAsync(snackBar.dismissWithAction()).toBeRejectedWithError(/without action/);
});
@Component({
template: `<ng-template>My custom snack-bar.</ng-template>`
})
class SnackbarHarnessTest {
@ViewChild(TemplateRef) customTmpl: TemplateRef<any>;
snackBar: MatSnackBar;
constructor(injector: Injector) {
this.snackBar = injector.get(snackBarToken);
}
openSimple(message: string, action = '', config?: MatSnackBarConfig) {
return this.snackBar.open(message, action, config);
}
openCustom(config?: MatSnackBarConfig) {
return this.snackBar.openFromTemplate(this.customTmpl, config);
}
}
}