1
- import { render } from '@testing-library/svelte' ;
2
- import { describe , it , expect } from 'vitest' ;
3
-
1
+ import { render , screen } from '@testing-library/svelte' ;
2
+ import { describe , it , expect , beforeEach , vi , afterEach } from 'vitest' ;
4
3
import type { ToastSettings } from './types.js' ;
5
4
import ToastTest from './ToastTest.svelte' ;
6
5
@@ -16,6 +15,57 @@ const toastMessage: ToastSettings = {
16
15
} ;
17
16
18
17
describe ( 'Toast.svelte' , ( ) => {
18
+ const snackbarWrapperTestId = 'snackbar-wrapper' ;
19
+
20
+ // see: https://testing-library.com/docs/svelte-testing-library/faq/#why-arent-transition-events-running
21
+ beforeEach ( ( ) => {
22
+ const rafMock = ( fn : ( _ : Date ) => void ) => setTimeout ( ( ) => fn ( new Date ( ) ) , 16 ) ;
23
+ vi . stubGlobal ( 'requestAnimationFrame' , rafMock ) ;
24
+ } ) ;
25
+
26
+ afterEach ( ( ) => {
27
+ vi . unstubAllGlobals ( ) ;
28
+ } ) ;
29
+
30
+ it ( 'does not show the toast wrapper if the toast queue is empty' , ( ) => {
31
+ expect ( ( ) => screen . getByTestId ( snackbarWrapperTestId ) ) . toThrow ( ) ;
32
+ } ) ;
33
+
34
+ it ( 'keeps the toast wrapper visible if a second toast is scheduled on the same tick as the closing of the first one, until the outro animation of the first toast is finished' , async ( ) => {
35
+ const { getByTestId } = render ( ToastTest , {
36
+ props : {
37
+ max : 2 ,
38
+ toastSettings : [
39
+ // note how toast B is scheduled to trigger at the same tick as toast A
40
+ { message : 'A' , timeout : 10 } ,
41
+ { message : 'B' , triggerDelay : 10 , timeout : 10 }
42
+ ]
43
+ }
44
+ } ) ;
45
+
46
+ const getWrapperElementAfterTimeout = ( timeout : number ) =>
47
+ new Promise ( ( resolve ) =>
48
+ setTimeout ( ( ) => {
49
+ try {
50
+ const el = getByTestId ( snackbarWrapperTestId ) ;
51
+ resolve ( el ) ;
52
+ } catch {
53
+ resolve ( false ) ;
54
+ }
55
+ } , timeout )
56
+ ) ;
57
+
58
+ const [ wrapperVisibilityOnAToBChange , wrapperVisibilityAfterAOutroFinishes , wrapperVisibilityAfterBOutroFinishes ] = await Promise . all ( [
59
+ getWrapperElementAfterTimeout ( 10 ) ,
60
+ getWrapperElementAfterTimeout ( 16 ) ,
61
+ getWrapperElementAfterTimeout ( 50 )
62
+ ] ) ;
63
+
64
+ expect ( wrapperVisibilityOnAToBChange ) . toBeTruthy ( ) ;
65
+ expect ( wrapperVisibilityAfterAOutroFinishes ) . toBeTruthy ( ) ;
66
+ expect ( wrapperVisibilityAfterBOutroFinishes ) . toBeFalsy ( ) ;
67
+ } ) ;
68
+
19
69
it ( 'Renders modal alert' , async ( ) => {
20
70
const { getByTestId } = render ( ToastTest , { props : { toastSettings : [ toastMessage ] } } ) ;
21
71
expect ( getByTestId ( 'toast' ) ) . toBeTruthy ( ) ;
0 commit comments