1
- import { MatSnackBar , MatSnackBarConfig , MatSnackBarModule } from '@angular/material/snack-bar' ;
2
- import { runHarnessTests } from './shared.spec' ;
1
+ import { Component , TemplateRef , ViewChild } from '@angular/core' ;
3
2
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
4
3
import { HarnessLoader } from '@angular/cdk/testing' ;
5
- import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
6
4
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed' ;
7
- import { Component , TemplateRef , ViewChild } from '@angular/core' ;
5
+ import { MatSnackBar , MatSnackBarConfig , MatSnackBarModule } from '@angular/material/snack-bar' ;
6
+ import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
8
7
import { MatSnackBarHarness } from './snack-bar-harness' ;
9
8
10
- describe ( 'MDC-based MatSnackBarHarness' , ( ) => {
11
- runHarnessTests ( MatSnackBarModule , MatSnackBar , MatSnackBarHarness ) ;
12
- } ) ;
13
-
14
- describe ( 'MDC-based MatSnackBarHarness (MDC only behavior)' , ( ) => {
9
+ describe ( 'MatSnackBarHarness' , ( ) => {
15
10
let fixture : ComponentFixture < SnackbarHarnessTest > ;
16
11
let loader : HarnessLoader ;
17
12
@@ -26,6 +21,110 @@ describe('MDC-based MatSnackBarHarness (MDC only behavior)', () => {
26
21
loader = TestbedHarnessEnvironment . documentRootLoader ( fixture ) ;
27
22
} ) ;
28
23
24
+ it ( 'should load harness for simple snack-bar' , async ( ) => {
25
+ const snackBarRef = fixture . componentInstance . openSimple ( 'Hello!' , '' ) ;
26
+ let snackBars = await loader . getAllHarnesses ( MatSnackBarHarness ) ;
27
+
28
+ expect ( snackBars . length ) . toBe ( 1 ) ;
29
+
30
+ snackBarRef . dismiss ( ) ;
31
+ snackBars = await loader . getAllHarnesses ( MatSnackBarHarness ) ;
32
+ expect ( snackBars . length ) . toBe ( 0 ) ;
33
+ } ) ;
34
+
35
+ it ( 'should load harness for custom snack-bar' , async ( ) => {
36
+ const snackBarRef = fixture . componentInstance . openCustom ( ) ;
37
+ let snackBars = await loader . getAllHarnesses ( MatSnackBarHarness ) ;
38
+
39
+ expect ( snackBars . length ) . toBe ( 1 ) ;
40
+
41
+ snackBarRef . dismiss ( ) ;
42
+ snackBars = await loader . getAllHarnesses ( MatSnackBarHarness ) ;
43
+ expect ( snackBars . length ) . toBe ( 0 ) ;
44
+ } ) ;
45
+
46
+ it ( 'should load snack-bar harness by selector' , async ( ) => {
47
+ fixture . componentInstance . openSimple ( 'Hello!' , '' , { panelClass : 'my-snack-bar' } ) ;
48
+ const snackBars = await loader . getAllHarnesses (
49
+ MatSnackBarHarness . with ( {
50
+ selector : '.my-snack-bar' ,
51
+ } ) ,
52
+ ) ;
53
+ expect ( snackBars . length ) . toBe ( 1 ) ;
54
+ } ) ;
55
+
56
+ it ( 'should be able to get role of snack-bar' , async ( ) => {
57
+ // Get role is now deprecated, so it should always return null.
58
+ fixture . componentInstance . openCustom ( ) ;
59
+ let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
60
+ expect ( await snackBar . getRole ( ) ) . toBe ( null ) ;
61
+
62
+ fixture . componentInstance . openCustom ( { politeness : 'polite' } ) ;
63
+ snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
64
+ expect ( await snackBar . getRole ( ) ) . toBe ( null ) ;
65
+
66
+ fixture . componentInstance . openCustom ( { politeness : 'off' } ) ;
67
+ snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
68
+ expect ( await snackBar . getRole ( ) ) . toBe ( null ) ;
69
+ } ) ;
70
+
71
+ it ( 'should be able to get aria-live of snack-bar' , async ( ) => {
72
+ fixture . componentInstance . openCustom ( ) ;
73
+ let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
74
+ expect ( await snackBar . getAriaLive ( ) ) . toBe ( 'assertive' ) ;
75
+
76
+ fixture . componentInstance . openCustom ( { politeness : 'polite' } ) ;
77
+ snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
78
+ expect ( await snackBar . getAriaLive ( ) ) . toBe ( 'polite' ) ;
79
+
80
+ fixture . componentInstance . openCustom ( { politeness : 'off' } ) ;
81
+ snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
82
+ expect ( await snackBar . getAriaLive ( ) ) . toBe ( 'off' ) ;
83
+ } ) ;
84
+
85
+ it ( 'should be able to get message of simple snack-bar' , async ( ) => {
86
+ fixture . componentInstance . openSimple ( 'Subscribed to newsletter.' ) ;
87
+ let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
88
+ expect ( await snackBar . getMessage ( ) ) . toBe ( 'Subscribed to newsletter.' ) ;
89
+ } ) ;
90
+
91
+ it ( 'should be able to get action description of simple snack-bar' , async ( ) => {
92
+ fixture . componentInstance . openSimple ( 'Hello' , 'Unsubscribe' ) ;
93
+ let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
94
+ expect ( await snackBar . getActionDescription ( ) ) . toBe ( 'Unsubscribe' ) ;
95
+ } ) ;
96
+
97
+ it ( 'should be able to check whether simple snack-bar has action' , async ( ) => {
98
+ fixture . componentInstance . openSimple ( 'With action' , 'Unsubscribe' ) ;
99
+ let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
100
+ expect ( await snackBar . hasAction ( ) ) . toBe ( true ) ;
101
+
102
+ fixture . componentInstance . openSimple ( 'No action' ) ;
103
+ snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
104
+ expect ( await snackBar . hasAction ( ) ) . toBe ( false ) ;
105
+ } ) ;
106
+
107
+ it ( 'should be able to dismiss simple snack-bar with action' , async ( ) => {
108
+ const snackBarRef = fixture . componentInstance . openSimple ( 'With action' , 'Unsubscribe' ) ;
109
+ let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
110
+ let actionCount = 0 ;
111
+ snackBarRef . onAction ( ) . subscribe ( ( ) => actionCount ++ ) ;
112
+
113
+ expect ( await snackBar . isDismissed ( ) )
114
+ . withContext ( 'The snackbar should be present in the DOM before dismiss' )
115
+ . toBe ( false ) ;
116
+
117
+ await snackBar . dismissWithAction ( ) ;
118
+ expect ( actionCount ) . toBe ( 1 ) ;
119
+ expect ( await snackBar . isDismissed ( ) )
120
+ . withContext ( 'The snackbar should be absent from the DOM after dismiss' )
121
+ . toBe ( true ) ;
122
+
123
+ fixture . componentInstance . openSimple ( 'No action' ) ;
124
+ snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
125
+ await expectAsync ( snackBar . dismissWithAction ( ) ) . toBeRejectedWithError ( / w i t h o u t a n a c t i o n / ) ;
126
+ } ) ;
127
+
29
128
it ( 'should be able to get message of a snack-bar with custom content' , async ( ) => {
30
129
fixture . componentInstance . openCustom ( ) ;
31
130
let snackBar = await loader . getHarness ( MatSnackBarHarness ) ;
@@ -74,6 +173,10 @@ class SnackbarHarnessTest {
74
173
75
174
constructor ( public snackBar : MatSnackBar ) { }
76
175
176
+ openSimple ( message : string , action = '' , config ?: MatSnackBarConfig ) {
177
+ return this . snackBar . open ( message , action , config ) ;
178
+ }
179
+
77
180
openCustom ( config ?: MatSnackBarConfig ) {
78
181
return this . snackBar . openFromTemplate ( this . customTmpl , config ) ;
79
182
}
0 commit comments