11import { asyncExpect } from '../../../tests/utils' ;
2- import message from '..' ;
2+ import message , { getInstance } from '..' ;
33import SmileOutlined from '@ant-design/icons-vue/SmileOutlined' ;
44
55describe ( 'message' , ( ) => {
66 beforeEach ( ( ) => {
7+ jest . useFakeTimers ( ) ;
78 document . body . outerHTML = '' ;
89 } ) ;
910
1011 afterEach ( ( ) => {
1112 message . destroy ( ) ;
1213 } ) ;
1314
15+ afterEach ( ( ) => {
16+ message . destroy ( ) ;
17+ jest . useRealTimers ( ) ;
18+ } ) ;
19+
1420 it ( 'should be able to config top' , async ( ) => {
1521 message . config ( {
1622 top : '100px' ,
@@ -41,53 +47,42 @@ describe('message', () => {
4147 message . info ( 'test' ) ;
4248 }
4349 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' ) ;
4854 } ) ;
4955
5056 it ( 'should be able to hide manually' , async ( ) => {
5157 const hide1 = message . info ( 'whatever' , 0 ) ;
5258 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 ) ;
6467 } ) ;
6568
6669 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 ) ;
8478 } ) ;
8579
8680 it ( 'should not need to use duration argument when using the onClose arguments' , ( ) => {
8781 message . info ( 'whatever' , ( ) => { } ) ;
8882 } ) ;
8983
9084 it ( 'should have the default duration when using the onClose arguments' , done => {
85+ jest . useRealTimers ( ) ;
9186 const defaultDuration = 3 ;
9287 const now = Date . now ( ) ;
9388 message . info ( 'whatever' , ( ) => {
@@ -99,6 +94,7 @@ describe('message', () => {
9994 } ) ;
10095
10196 it ( 'should be called like promise' , done => {
97+ jest . useRealTimers ( ) ;
10298 const defaultDuration = 3 ;
10399 const now = Date . now ( ) ;
104100 message . info ( 'whatever' ) . then ( ( ) => {
@@ -112,38 +108,32 @@ describe('message', () => {
112108 // https:// github.com/ant-design/ant-design/issues/8201
113109 it ( 'should hide message correctly' , async ( ) => {
114110 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 ) ;
122117 } ) ;
123118 it ( 'should allow custom icon' , async ( ) => {
124119 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 ) ;
128122 } ) ;
129123
130124 it ( 'should have no icon' , async ( ) => {
131125 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 ) ;
135128 } ) ;
136129 // https://github.com/ant-design/ant-design/issues/8201
137130 it ( 'should destroy messages correctly' , async ( ) => {
138131 message . loading ( 'Action in progress1..' , 0 ) ;
139132 message . loading ( 'Action in progress2..' , 0 ) ;
140133 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 ) ;
148138 } ) ;
149139} ) ;
0 commit comments