1
1
import { asyncExpect } from '../../../tests/utils' ;
2
- import message from '..' ;
2
+ import message , { getInstance } from '..' ;
3
3
import SmileOutlined from '@ant-design/icons-vue/SmileOutlined' ;
4
4
5
5
describe ( 'message' , ( ) => {
6
6
beforeEach ( ( ) => {
7
+ jest . useFakeTimers ( ) ;
7
8
document . body . outerHTML = '' ;
8
9
} ) ;
9
10
10
11
afterEach ( ( ) => {
11
12
message . destroy ( ) ;
12
13
} ) ;
13
14
15
+ afterEach ( ( ) => {
16
+ message . destroy ( ) ;
17
+ jest . useRealTimers ( ) ;
18
+ } ) ;
19
+
14
20
it ( 'should be able to config top' , async ( ) => {
15
21
message . config ( {
16
22
top : '100px' ,
@@ -41,53 +47,42 @@ describe('message', () => {
41
47
message . info ( 'test' ) ;
42
48
}
43
49
message . info ( 'last' ) ;
44
- await asyncExpect ( ( ) => {
45
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 5 ) ;
46
- expect ( document . querySelectorAll ( '.ant-message-notice' ) [ 4 ] . textContent ) . toBe ( 'last' ) ;
47
- } , 0 ) ;
50
+ await Promise . resolve ( ) ;
51
+ jest . runAllTimers ( ) ;
52
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 5 ) ;
53
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) [ 4 ] . textContent ) . toBe ( 'last' ) ;
48
54
} ) ;
49
55
50
56
it ( 'should be able to hide manually' , async ( ) => {
51
57
const hide1 = message . info ( 'whatever' , 0 ) ;
52
58
const hide2 = message . info ( 'whatever' , 0 ) ;
53
- await asyncExpect ( ( ) => {
54
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 2 ) ;
55
- hide1 ( ) ;
56
- } , 0 ) ;
57
- await asyncExpect ( ( ) => {
58
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 1 ) ;
59
- hide2 ( ) ;
60
- } , 0 ) ;
61
- await asyncExpect ( ( ) => {
62
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
63
- } , 0 ) ;
59
+ await Promise . resolve ( ) ;
60
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 2 ) ;
61
+ hide1 ( ) ;
62
+ jest . runAllTimers ( ) ;
63
+ expect ( getInstance ( ) . component . value . notices ) . toHaveLength ( 1 ) ;
64
+ hide2 ( ) ;
65
+ jest . runAllTimers ( ) ;
66
+ expect ( getInstance ( ) . component . value . notices ) . toHaveLength ( 0 ) ;
64
67
} ) ;
65
68
66
69
it ( 'should be able to destroy globally' , async ( ) => {
67
- await asyncExpect ( ( ) => {
68
- message . info ( 'whatever' , 0 ) ;
69
- } ) ;
70
- await asyncExpect ( ( ) => {
71
- message . info ( 'whatever' , 0 ) ;
72
- } ) ;
73
- await asyncExpect ( ( ) => {
74
- expect ( document . querySelectorAll ( '.ant-message' ) . length ) . toBe ( 1 ) ;
75
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 2 ) ;
76
- } ) ;
77
- await asyncExpect ( ( ) => {
78
- message . destroy ( ) ;
79
- } ) ;
80
- await asyncExpect ( ( ) => {
81
- expect ( document . querySelectorAll ( '.ant-message' ) . length ) . toBe ( 0 ) ;
82
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
83
- } ) ;
70
+ message . info ( 'whatever' , 0 ) ;
71
+ message . info ( 'whatever' , 0 ) ;
72
+ await Promise . resolve ( ) ;
73
+ expect ( document . querySelectorAll ( '.ant-message' ) . length ) . toBe ( 1 ) ;
74
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 2 ) ;
75
+ message . destroy ( ) ;
76
+ expect ( document . querySelectorAll ( '.ant-message' ) . length ) . toBe ( 0 ) ;
77
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
84
78
} ) ;
85
79
86
80
it ( 'should not need to use duration argument when using the onClose arguments' , ( ) => {
87
81
message . info ( 'whatever' , ( ) => { } ) ;
88
82
} ) ;
89
83
90
84
it ( 'should have the default duration when using the onClose arguments' , done => {
85
+ jest . useRealTimers ( ) ;
91
86
const defaultDuration = 3 ;
92
87
const now = Date . now ( ) ;
93
88
message . info ( 'whatever' , ( ) => {
@@ -99,6 +94,7 @@ describe('message', () => {
99
94
} ) ;
100
95
101
96
it ( 'should be called like promise' , done => {
97
+ jest . useRealTimers ( ) ;
102
98
const defaultDuration = 3 ;
103
99
const now = Date . now ( ) ;
104
100
message . info ( 'whatever' ) . then ( ( ) => {
@@ -112,38 +108,32 @@ describe('message', () => {
112
108
// https:// github.com/ant-design/ant-design/issues/8201
113
109
it ( 'should hide message correctly' , async ( ) => {
114
110
let hide = message . loading ( 'Action in progress..' , 0 ) ;
115
- await asyncExpect ( ( ) => {
116
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 1 ) ;
117
- hide ( ) ;
118
- } , 0 ) ;
119
- await asyncExpect ( ( ) => {
120
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
121
- } , 0 ) ;
111
+ await Promise . resolve ( ) ;
112
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 1 ) ;
113
+ hide ( ) ;
114
+ await Promise . resolve ( ) ;
115
+ jest . runAllTimers ( ) ;
116
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
122
117
} ) ;
123
118
it ( 'should allow custom icon' , async ( ) => {
124
119
message . open ( { content : 'Message' , icon : < SmileOutlined /> } ) ;
125
- await asyncExpect ( ( ) => {
126
- expect ( document . querySelectorAll ( '.anticon-smile' ) . length ) . toBe ( 1 ) ;
127
- } , 0 ) ;
120
+ await Promise . resolve ( ) ;
121
+ expect ( document . querySelectorAll ( '.anticon-smile' ) . length ) . toBe ( 1 ) ;
128
122
} ) ;
129
123
130
124
it ( 'should have no icon' , async ( ) => {
131
125
message . open ( { content : 'Message' } ) ;
132
- await asyncExpect ( ( ) => {
133
- expect ( document . querySelectorAll ( '.ant-message-notice .anticon' ) . length ) . toBe ( 0 ) ;
134
- } , 0 ) ;
126
+ await Promise . resolve ( ) ;
127
+ expect ( document . querySelectorAll ( '.ant-message-notice .anticon' ) . length ) . toBe ( 0 ) ;
135
128
} ) ;
136
129
// https://github.com/ant-design/ant-design/issues/8201
137
130
it ( 'should destroy messages correctly' , async ( ) => {
138
131
message . loading ( 'Action in progress1..' , 0 ) ;
139
132
message . loading ( 'Action in progress2..' , 0 ) ;
140
133
setTimeout ( ( ) => message . destroy ( ) , 1000 ) ;
141
-
142
- await asyncExpect ( ( ) => {
143
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 2 ) ;
144
- } , 0 ) ;
145
- await asyncExpect ( ( ) => {
146
- expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
147
- } , 1500 ) ;
134
+ await Promise . resolve ( ) ;
135
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 2 ) ;
136
+ jest . runAllTimers ( ) ;
137
+ expect ( document . querySelectorAll ( '.ant-message-notice' ) . length ) . toBe ( 0 ) ;
148
138
} ) ;
149
139
} ) ;
0 commit comments