1
- import { Component , ViewChild } from '@angular/core' ;
2
- import { async , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
1
+ import { Component , ViewChild } from '@angular/core' ;
2
+ import { async , TestBed , fakeAsync , tick , ComponentFixture } from '@angular/core/testing' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
5
5
import { IgxToastComponent , IgxToastModule , IgxToastPosition } from './toast.component' ;
6
-
7
6
import { configureTestSuite } from '../test-utils/configure-suite' ;
8
- import { wait } from '../test-utils/ui-interactions.spec' ;
9
7
10
8
describe ( 'IgxToast' , ( ) => {
11
9
configureTestSuite ( ) ;
@@ -20,24 +18,32 @@ describe('IgxToast', () => {
20
18
]
21
19
} ) . compileComponents ( ) ;
22
20
} ) ) ;
23
- let fixture , toast , element ;
21
+
22
+ const baseClass = 'igx-toast' ;
23
+ const classes = {
24
+ top : `${ baseClass } --top` ,
25
+ middle : `${ baseClass } --middle` ,
26
+ bottom : `${ baseClass } --bottom` ,
27
+ } ;
28
+
29
+ let fixture : ComponentFixture < ToastInitializeTestComponent > ;
30
+ let toast : IgxToastComponent ;
31
+
24
32
beforeEach ( ( ) => {
25
33
fixture = TestBed . createComponent ( ToastInitializeTestComponent ) ;
26
34
toast = fixture . componentInstance . toast ;
27
35
toast . isVisible = true ;
28
36
fixture . detectChanges ( ) ;
29
- element = fixture . debugElement . query ( By . css ( '.igx-toast--bottom' ) ) ;
30
37
} ) ;
31
38
32
- it ( 'should properly initialize properties ' , ( ) => {
33
- const domToast = fixture . debugElement . query ( By . css ( 'igx-toast' ) ) . nativeElement ;
39
+ it ( 'should properly initialize' , ( ) => {
40
+ const domToast = fixture . debugElement . query ( By . css ( baseClass ) ) . nativeElement ;
34
41
expect ( toast . id ) . toContain ( 'igx-toast-' ) ;
35
42
expect ( domToast . id ) . toContain ( 'igx-toast-' ) ;
36
- expect ( toast . message ) . toBeUndefined ( ) ;
37
43
expect ( toast . displayTime ) . toBe ( 4000 ) ;
38
44
expect ( toast . autoHide ) . toBeTruthy ( ) ;
39
45
expect ( toast . isVisible ) . toBeTruthy ( ) ;
40
- expect ( element . classes [ toast . CSS_CLASSES . IGX_TOAST_BOTTOM ] ) . toBeTruthy ( ) ;
46
+ expect ( domToast . classList ) . toContain ( classes . bottom ) ;
41
47
42
48
toast . id = 'customToast' ;
43
49
fixture . detectChanges ( ) ;
@@ -49,92 +55,91 @@ describe('IgxToast', () => {
49
55
it ( 'should change toast position to middle' , ( ) => {
50
56
toast . position = IgxToastPosition . Middle ;
51
57
fixture . detectChanges ( ) ;
58
+ const domToast = fixture . debugElement . query ( By . css ( baseClass ) ) . nativeElement ;
52
59
53
- element = fixture . debugElement . query ( By . css ( '.igx-toast--middle' ) ) ;
54
- expect ( element . classes [ toast . CSS_CLASSES . IGX_TOAST_MIDDLE ] ) . toBeTruthy ( ) ;
60
+ expect ( domToast . classList ) . toContain ( classes . middle ) ;
55
61
} ) ;
56
62
57
63
it ( 'should change toast position to top' , ( ) => {
58
64
toast . position = IgxToastPosition . Top ;
59
65
fixture . detectChanges ( ) ;
66
+ const domToast = fixture . debugElement . query ( By . css ( baseClass ) ) . nativeElement ;
60
67
61
- element = fixture . debugElement . query ( By . css ( '.igx-toast--top' ) ) ;
62
- expect ( element . classes [ toast . CSS_CLASSES . IGX_TOAST_TOP ] ) . toBeTruthy ( ) ;
68
+ expect ( domToast . classList ) . toContain ( classes . top ) ;
63
69
} ) ;
64
70
65
- it ( 'should change toast position to bottom, the rest should be undefined ' , ( ) => {
71
+ it ( 'should change toast position to bottom' , ( ) => {
66
72
toast . position = IgxToastPosition . Bottom ;
67
73
fixture . detectChanges ( ) ;
74
+ const domToast = fixture . debugElement . query ( By . css ( baseClass ) ) . nativeElement ;
68
75
69
- expect ( element . classes [ toast . CSS_CLASSES . IGX_TOAST_TOP ] ) . toBeUndefined ( ) ;
70
- expect ( element . classes [ toast . CSS_CLASSES . IGX_TOAST_MIDDLE ] ) . toBeUndefined ( ) ;
71
- expect ( element . classes [ toast . CSS_CLASSES . IGX_TOAST_BOTTOM ] ) . toBe ( true ) ;
76
+ expect ( domToast . classList ) . not . toContain ( classes . top ) ;
77
+ expect ( domToast . classList ) . not . toContain ( classes . middle ) ;
78
+ expect ( domToast . classList ) . toContain ( classes . bottom ) ;
72
79
} ) ;
73
80
74
- it ( 'should auto hide 1 second after is open' , fakeAsync ( ( ) => {
81
+ it ( 'should auto hide 1 second after it\'s open' , fakeAsync ( ( ) => {
75
82
toast . displayTime = 1000 ;
76
83
77
84
toast . show ( ) ;
78
85
79
86
expect ( toast . isVisible ) . toBeTruthy ( ) ;
87
+ expect ( toast . animationState ) . toBe ( 'visible' ) ;
80
88
expect ( toast . autoHide ) . toBeTruthy ( ) ;
81
89
82
90
tick ( 1000 ) ;
83
- fixture . detectChanges ( ) ;
91
+
84
92
expect ( toast . isVisible ) . toBeFalsy ( ) ;
93
+ expect ( toast . animationState ) . toBe ( 'invisible' ) ;
85
94
} ) ) ;
86
95
87
- it ( 'should not auto hide seconds after is open' , fakeAsync ( ( ) => {
96
+ it ( 'should not auto hide after it\'s open' , fakeAsync ( ( ) => {
88
97
toast . displayTime = 1000 ;
89
98
toast . autoHide = false ;
90
99
91
100
toast . show ( ) ;
92
101
93
102
expect ( toast . isVisible ) . toBeTruthy ( ) ;
103
+ expect ( toast . animationState ) . toBe ( 'visible' ) ;
94
104
expect ( toast . autoHide ) . toBeFalsy ( ) ;
95
105
96
106
tick ( 1000 ) ;
97
- fixture . detectChanges ( ) ;
107
+
98
108
expect ( toast . isVisible ) . toBeTruthy ( ) ;
109
+ expect ( toast . animationState ) . toBe ( 'visible' ) ;
99
110
} ) ) ;
100
111
101
- it ( 'visibility is properly toggled by its toggle() method. ' , ( async ( ) => {
112
+ it ( 'visibility is updated by the toggle() method' , ( ) => {
102
113
spyOn ( toast . onShowing , 'emit' ) ;
103
114
spyOn ( toast . onShown , 'emit' ) ;
104
115
spyOn ( toast . onHiding , 'emit' ) ;
105
116
spyOn ( toast . onHidden , 'emit' ) ;
106
117
118
+ toast . show ( ) ;
107
119
expect ( toast . isVisible ) . toBe ( true ) ;
108
- toast . toggle ( ) ;
109
- await wait ( ) ;
110
- fixture . detectChanges ( ) ;
120
+ expect ( toast . animationState ) . toBe ( 'visible' ) ;
111
121
112
- expect ( toast . isVisible ) . toBe ( false ) ;
113
- expect ( toast . onShowing . emit ) . toHaveBeenCalledTimes ( 0 ) ;
114
- expect ( toast . onShown . emit ) . toHaveBeenCalledTimes ( 0 ) ;
115
- expect ( toast . onHiding . emit ) . toHaveBeenCalledTimes ( 1 ) ;
116
- expect ( toast . onHidden . emit ) . toHaveBeenCalledTimes ( 1 ) ;
122
+ expect ( toast . onShowing . emit ) . toHaveBeenCalledTimes ( 1 ) ;
123
+ expect ( toast . onShown . emit ) . toHaveBeenCalledTimes ( 1 ) ;
124
+ expect ( toast . onHiding . emit ) . toHaveBeenCalledTimes ( 0 ) ;
125
+ expect ( toast . onHidden . emit ) . toHaveBeenCalledTimes ( 0 ) ;
117
126
118
127
toast . toggle ( ) ;
119
- await wait ( ) ;
120
- fixture . detectChanges ( ) ;
128
+ expect ( toast . isVisible ) . toBe ( false ) ;
129
+ expect ( toast . animationState ) . toBe ( 'invisible' ) ;
121
130
122
- expect ( toast . isVisible ) . toBe ( true ) ;
123
131
expect ( toast . onShowing . emit ) . toHaveBeenCalledTimes ( 1 ) ;
124
132
expect ( toast . onShown . emit ) . toHaveBeenCalledTimes ( 1 ) ;
125
133
expect ( toast . onHiding . emit ) . toHaveBeenCalledTimes ( 1 ) ;
126
134
expect ( toast . onHidden . emit ) . toHaveBeenCalledTimes ( 1 ) ;
127
135
128
- toast . toggle ( ) ;
129
- await wait ( ) ;
130
- fixture . detectChanges ( ) ;
131
- expect ( toast . isVisible ) . toBe ( false ) ;
132
- } ) ) ;
136
+ } ) ;
133
137
} ) ;
138
+
134
139
@Component ( {
135
- template : `<igx-toast #toast>
136
- </igx-toast>`
140
+ template : `<igx-toast #toast></igx-toast>`
137
141
} )
138
142
class ToastInitializeTestComponent {
139
- @ViewChild ( IgxToastComponent , { static : true } ) public toast : IgxToastComponent ;
143
+ @ViewChild ( IgxToastComponent , { static : true } )
144
+ public toast : IgxToastComponent ;
140
145
}
0 commit comments