Skip to content

Commit 02a0851

Browse files
committed
Refactor Consent/Banner component to extend LitElements
1 parent 03fdd7c commit 02a0851

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

src/components/Consent/Banner/client/__tests__/index.spec.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,23 @@ const waitForBannerReady = async (element: ConsentBannerElement) => {
6060

6161
type JsdomWindow = Window & typeof globalThis
6262

63+
let container: AstroContainer
64+
6365
const 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

8790
beforeEach(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
})

src/components/Consent/Banner/client/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Uses Light DOM (no Shadow DOM) with Astro-rendered templates
55
*/
66

7+
import { LitElement } from 'lit'
78
import type { WebComponentModule } from '@components/scripts/@types/webComponentModule'
89
import {
910
addButtonEventListeners,
@@ -32,7 +33,9 @@ const COMPONENT_TAG_NAME = 'consent-banner' as const
3233
const COMPONENT_SCRIPT_NAME = 'ConsentBannerElement'
3334
export const CONSENT_BANNER_READY_EVENT = 'consent-banner:ready'
3435

35-
export class ConsentBannerElement extends HTMLElement {
36+
export class ConsentBannerElement extends LitElement {
37+
static registeredName = COMPONENT_TAG_NAME
38+
3639
private wrapper!: HTMLDivElement
3740
private closeBtn!: HTMLButtonElement
3841
private allowBtn!: HTMLButtonElement
@@ -51,7 +54,12 @@ export class ConsentBannerElement extends HTMLElement {
5154
window.location.assign(url)
5255
}
5356

54-
connectedCallback(): void {
57+
protected override createRenderRoot(): HTMLElement {
58+
return this
59+
}
60+
61+
override connectedCallback(): void {
62+
super.connectedCallback()
5563
if (this.isInitialized || typeof document === 'undefined') {
5664
return
5765
}
@@ -76,7 +84,8 @@ export class ConsentBannerElement extends HTMLElement {
7684
}
7785
}
7886

79-
disconnectedCallback(): void {
87+
override disconnectedCallback(): void {
88+
super.disconnectedCallback()
8089
if (this.domReadyHandler) {
8190
document.removeEventListener('DOMContentLoaded', this.domReadyHandler)
8291
this.domReadyHandler = null
@@ -377,8 +386,10 @@ export const registerConsentBannerWebComponent = (tagName: string = COMPONENT_TA
377386
defineCustomElement(tagName, ConsentBannerElement)
378387
}
379388

389+
export const registerWebComponent = registerConsentBannerWebComponent
390+
380391
export const webComponentModule: WebComponentModule<ConsentBannerElement> = {
381392
registeredName: COMPONENT_TAG_NAME,
382393
componentCtor: ConsentBannerElement,
383-
registerWebComponent: registerConsentBannerWebComponent,
394+
registerWebComponent,
384395
}

0 commit comments

Comments
 (0)