@@ -23,47 +23,52 @@ jest.mock('next-auth/react', () => ({
2323
2424let AppAlertComponent : React . FC < AppAlertProps >
2525
26- describe ( 'Banner suppression ' , ( ) => {
26+ describe ( 'AppAlert ' , ( ) => {
2727 beforeAll ( async ( ) => {
2828 // why async import? see https://github.com/facebook/jest/issues/10025#issuecomment-716789840
2929 const module = await import ( '../AppAlert' )
3030 AppAlertComponent = module . AppAlert
3131 } )
3232
33- it ( 'doesn\'t show alert when cookie exists' , async ( ) => {
34- // cookie exists
35- cookieGetter . mockReturnValueOnce ( 'foo' )
33+ beforeEach ( ( ) => {
34+ cookieGetter . mockClear ( )
35+ cookieSetter . mockClear ( )
36+ } )
37+
38+ it ( 'renders alert with message' , ( ) => {
3639 render (
3740 < AppAlertComponent
41+ cookieStorageKey = 'test'
3842 message = {
3943 < div >
4044 important message
4145 </ div >
42- }
43- /> )
46+ }
47+ />
48+ )
4449
45- expect ( screen . queryAllByRole ( 'button' ) . length ) . toEqual ( 0 )
46- cookieGetter . mockClear ( )
50+ expect ( screen . getByText ( 'important message' ) ) . toBeInTheDocument ( )
51+ expect ( screen . getByRole ( 'button' , { name : / D o n ' t s h o w t h i s a g a i n / i } ) ) . toBeInTheDocument ( )
4752 } )
4853
49- it ( 'shows alert' , async ( ) => {
50- // Clear previous cookie setting if any
51- // cookieGetter.mockClear()
52- // cookieGetter.mockRejectedValueOnce(null)
54+ it ( 'sets cookie when suppress button is clicked' , async ( ) => {
5355 const user = userEvent . setup ( { skipHover : true } )
56+
5457 render (
5558 < AppAlertComponent
59+ cookieStorageKey = 'test'
5660 message = {
5761 < div >
5862 important message 2
5963 </ div >
60- }
61- /> )
62- screen . debug ( )
64+ }
65+ />
66+ )
67+
6368 // click the Suppress button
6469 await user . click ( screen . getByRole ( 'button' , { name : / D o n ' t s h o w t h i s a g a i n / i } ) )
6570
66- // alert dismissed
67- expect ( screen . queryAllByRole ( 'button' ) ) . toHaveLength ( 0 )
71+ // verify cookie was set
72+ expect ( cookieSetter ) . toHaveBeenCalledWith ( 'test' , '1' , { strict : true , expires : 30 } )
6873 } )
6974} )
0 commit comments