Skip to content

Commit ea3df99

Browse files
committed
Refactor Consent/Preferences component to extend LitElements
1 parent 02a0851 commit ea3df99

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,23 @@ const waitForPreferencesReady = async (element: ConsentPreferencesElement) => {
3434
})
3535
}
3636

37+
let container: AstroContainer
38+
3739
const renderConsentPreferences = async (
3840
assertion: (_context: {
3941
element: ConsentPreferencesElement
4042
window: JsdomWindow
4143
}) => Promise<void> | void
4244
) => {
43-
const container = await AstroContainer.create()
44-
4545
await executeRender<ConsentPreferencesModule>({
4646
container,
4747
component: ConsentPreferencesComponent,
4848
moduleSpecifier: '@components/Consent/Preferences/client/index',
4949
selector: 'consent-preferences',
50-
waitForReady: waitForPreferencesReady,
50+
waitForReady: async (element: ConsentPreferencesElement) => {
51+
await waitForPreferencesReady(element)
52+
await element.updateComplete
53+
},
5154
assert: async ({ element, window }) => {
5255
if (!window) {
5356
throw new TestError('Consent preferences tests require a window instance')
@@ -117,6 +120,7 @@ vi.mock('@components/scripts/store', () => {
117120

118121
describe('ConsentPreferencesElement', () => {
119122
beforeEach(async () => {
123+
container = await AstroContainer.create()
120124
consentMockHelpers.reset()
121125
vi.clearAllMocks()
122126

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Uses centralized state management from scripts/store
55
*/
66

7+
import { LitElement } from 'lit'
78
import type { WebComponentModule } from '@components/scripts/@types/webComponentModule'
89
import { isInputElement } from '@components/scripts/assertions/elements'
910
import { addButtonEventListeners } from '@components/scripts/elementListeners'
@@ -28,7 +29,9 @@ const COMPONENT_TAG_NAME = 'consent-preferences' as const
2829
const COMPONENT_SCRIPT_NAME = 'ConsentPreferencesElement'
2930
export const CONSENT_PREFERENCES_READY_EVENT = 'consent-preferences:ready'
3031

31-
export class ConsentPreferencesElement extends HTMLElement {
32+
export class ConsentPreferencesElement extends LitElement {
33+
static registeredName = COMPONENT_TAG_NAME
34+
3235
private allowAllBtn!: HTMLButtonElement
3336
private denyAllBtn!: HTMLButtonElement
3437
private saveBtn!: HTMLButtonElement
@@ -38,7 +41,12 @@ export class ConsentPreferencesElement extends HTMLElement {
3841
private unsubscribeConsent: (() => void) | null = null
3942
private isInitialized = false
4043

41-
connectedCallback(): void {
44+
protected override createRenderRoot(): HTMLElement {
45+
return this
46+
}
47+
48+
override connectedCallback(): void {
49+
super.connectedCallback()
4250
if (typeof document === 'undefined' || this.isInitialized) {
4351
return
4452
}
@@ -63,7 +71,8 @@ export class ConsentPreferencesElement extends HTMLElement {
6371
}
6472
}
6573

66-
disconnectedCallback(): void {
74+
override disconnectedCallback(): void {
75+
super.disconnectedCallback()
6776
if (this.domReadyHandler) {
6877
document.removeEventListener('DOMContentLoaded', this.domReadyHandler)
6978
this.domReadyHandler = null
@@ -283,8 +292,10 @@ export const registerConsentPreferencesWebComponent = (tagName: string = COMPONE
283292
defineCustomElement(tagName, ConsentPreferencesElement)
284293
}
285294

295+
export const registerWebComponent = registerConsentPreferencesWebComponent
296+
286297
export const webComponentModule: WebComponentModule<ConsentPreferencesElement> = {
287298
registeredName: COMPONENT_TAG_NAME,
288299
componentCtor: ConsentPreferencesElement,
289-
registerWebComponent: registerConsentPreferencesWebComponent,
300+
registerWebComponent,
290301
}

0 commit comments

Comments
 (0)