@@ -60,20 +60,23 @@ const waitForBannerReady = async (element: ConsentBannerElement) => {
6060
6161type JsdomWindow = Window & typeof globalThis
6262
63+ let container : AstroContainer
64+
6365const renderConsentBanner = async (
6466 assertion : ( _context : {
6567 element : ConsentBannerElement
6668 window : JsdomWindow
6769 } ) => Promise < void > | void
6870) => {
69- const container = await AstroContainer . create ( )
70-
7171 await executeRender < ConsentBannerModule > ( {
7272 container,
7373 component : ConsentBanner ,
7474 moduleSpecifier : '@components/Consent/Banner/client/index' ,
7575 selector : 'consent-banner' ,
76- waitForReady : waitForBannerReady ,
76+ waitForReady : async ( element : ConsentBannerElement ) => {
77+ await waitForBannerReady ( element )
78+ await element . updateComplete
79+ } ,
7780 assert : async ( { element, window } ) => {
7881 if ( ! window ) {
7982 throw new TestError ( 'JSDOM window is not available for consent banner tests' )
@@ -85,6 +88,7 @@ const renderConsentBanner = async (
8588}
8689
8790beforeEach ( async ( ) => {
91+ container = await AstroContainer . create ( )
8892 initConsentCookiesMock . mockReturnValue ( true )
8993 showConsentBannerMock . mockClear ( )
9094 hideConsentBannerMock . mockClear ( )
@@ -193,4 +197,28 @@ describe('ConsentBannerElement', () => {
193197 }
194198 } )
195199 } )
200+
201+ it ( 'records visibility before Astro view transitions' , async ( ) => {
202+ await renderConsentBanner ( ( { window } ) => {
203+ const wrapper = window . document . getElementById ( 'consent-modal-id' ) as HTMLDivElement | null
204+ expect ( wrapper ) . not . toBeNull ( )
205+ wrapper ! . style . display = 'block'
206+
207+ window . document . dispatchEvent ( new window . CustomEvent ( 'astro:before-swap' ) )
208+
209+ expect ( showConsentBannerMock ) . toHaveBeenCalled ( )
210+ } )
211+ } )
212+
213+ it ( 'clears visibility when the banner is hidden before Astro view transitions' , async ( ) => {
214+ await renderConsentBanner ( ( { window } ) => {
215+ const wrapper = window . document . getElementById ( 'consent-modal-id' ) as HTMLDivElement | null
216+ expect ( wrapper ) . not . toBeNull ( )
217+ wrapper ! . style . display = 'none'
218+
219+ window . document . dispatchEvent ( new window . CustomEvent ( 'astro:before-swap' ) )
220+
221+ expect ( hideConsentBannerMock ) . toHaveBeenCalled ( )
222+ } )
223+ } )
196224} )
0 commit comments