@@ -14,16 +14,16 @@ describe('Alert component & showAlert', () => {
14
14
15
15
it ( 'renders an alert with heading and text' , async ( ) => {
16
16
const real = await vi . importActual < typeof import ( '../Alert' ) > ( '../Alert' ) ;
17
- real . showAlert ( 'Some text' , 'danger' , 'abc' , 'Heading Text' ) ;
17
+ await real . showAlert ( 'Some text' , 'danger' , 'abc' , 'Heading Text' ) ;
18
18
render ( < real . ErrorAlert /> ) ;
19
19
expect ( screen . getByText ( 'Some text' ) ) . toBeTruthy ( ) ;
20
20
expect ( screen . getByTestId ( 'alert-heading' ) ) . toHaveTextContent ( 'Heading Text' ) ;
21
21
} ) ;
22
22
23
23
it ( 'suppresses alert containing Failed to fetch' , async ( ) => {
24
24
const real = await vi . importActual < typeof import ( '../Alert' ) > ( '../Alert' ) ;
25
- const warnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => undefined ) ;
26
- real . showAlert ( 'Failed to fetch resource' , 'danger' ) ;
25
+ const warnSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => undefined ) ;
26
+ await real . showAlert ( 'Failed to fetch resource' , 'danger' ) ;
27
27
render ( < real . ErrorAlert /> ) ;
28
28
expect ( screen . queryByText ( 'Failed to fetch resource' ) ) . toBeNull ( ) ;
29
29
expect ( warnSpy ) . toHaveBeenCalled ( ) ;
@@ -32,7 +32,7 @@ describe('Alert component & showAlert', () => {
32
32
33
33
it ( 'auto dismisses alert after timeout' , async ( ) => {
34
34
const real = await vi . importActual < typeof import ( '../Alert' ) > ( '../Alert' ) ;
35
- real . showAlert ( 'Autoclose' , 'success' , 'auto' , 'Auto Heading' , 2000 ) ;
35
+ await real . showAlert ( 'Autoclose' , 'success' , 'auto' , 'Auto Heading' , 2000 ) ;
36
36
const { rerender } = render ( < real . ErrorAlert /> ) ;
37
37
expect ( screen . getByText ( 'Autoclose' ) ) . toBeTruthy ( ) ;
38
38
await advanceTimers ( 2000 ) ;
@@ -43,20 +43,36 @@ describe('Alert component & showAlert', () => {
43
43
it ( 'replaces alert with same id and clears previous timeout' , async ( ) => {
44
44
const real = await vi . importActual < typeof import ( '../Alert' ) > ( '../Alert' ) ;
45
45
const clearSpy = vi . spyOn ( window , 'clearTimeout' ) ;
46
- real . showAlert ( 'First' , 'warning' , 'same' , 'First Heading' , 5000 ) ;
46
+ await real . showAlert ( 'First' , 'warning' , 'same' , 'First Heading' , 5000 ) ;
47
47
const utils = render ( < real . ErrorAlert /> ) ;
48
48
expect ( screen . getByText ( 'First' ) ) . toBeTruthy ( ) ;
49
- real . showAlert ( 'Second' , 'warning' , 'same' , 'Second Heading' ) ;
49
+ await real . showAlert ( 'Second' , 'warning' , 'same' , 'Second Heading' ) ;
50
50
utils . rerender ( < real . ErrorAlert /> ) ;
51
51
expect ( screen . getByText ( 'Second' ) ) . toBeTruthy ( ) ;
52
52
expect ( screen . queryByText ( 'First' ) ) . toBeNull ( ) ;
53
53
expect ( clearSpy ) . toHaveBeenCalled ( ) ;
54
54
clearSpy . mockRestore ( ) ;
55
55
} ) ;
56
56
57
+ it ( 'replaces alert with same text/heading/variant (no id) and clears previous timeout' , async ( ) => {
58
+ const real = await vi . importActual < typeof import ( '../Alert' ) > ( '../Alert' ) ;
59
+ const clearSpy = vi . spyOn ( window , 'clearTimeout' ) ;
60
+ await real . showAlert ( 'Same Text' , 'warning' , undefined , 'Same Heading' , 2000 ) ;
61
+ const utils = render ( < real . ErrorAlert /> ) ;
62
+ expect ( screen . getByText ( 'Same Text' ) ) . toBeTruthy ( ) ;
63
+ await real . showAlert ( 'Same Text' , 'warning' , undefined , 'Same Heading' ) ;
64
+ utils . rerender ( < real . ErrorAlert /> ) ;
65
+ expect ( screen . getByText ( 'Same Text' ) ) . toBeTruthy ( ) ;
66
+ await advanceTimers ( 2000 ) ;
67
+ utils . rerender ( < real . ErrorAlert /> ) ;
68
+ expect ( screen . getByText ( 'Same Text' ) ) . toBeTruthy ( ) ;
69
+ expect ( clearSpy ) . toHaveBeenCalled ( ) ;
70
+ clearSpy . mockRestore ( ) ;
71
+ } ) ;
72
+
57
73
it ( 'manual dismiss via close button triggers onClose and removes alert' , async ( ) => {
58
74
const real = await vi . importActual < typeof import ( '../Alert' ) > ( '../Alert' ) ;
59
- real . showAlert ( 'Dismiss me' , 'danger' , 'dismiss' , 'Dismiss Heading' ) ;
75
+ await real . showAlert ( 'Dismiss me' , 'danger' , 'dismiss' , 'Dismiss Heading' ) ;
60
76
const { rerender } = render ( < real . ErrorAlert /> ) ;
61
77
expect ( screen . getByText ( 'Dismiss me' ) ) . toBeTruthy ( ) ;
62
78
const closeButtons = screen . getAllByTestId ( 'close-alert' ) ;
0 commit comments