|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { experimental_AstroContainer as AstroContainer } from 'astro/container' |
| 3 | +import PrivacyForm from '@components/Forms/Privacy/index.astro' |
| 4 | +import type { PrivacyFormElement as PrivacyFormElementInstance } from '../index' |
| 5 | +import type { WebComponentModule } from '@components/scripts/@types/webComponentModule' |
| 6 | +import { executeRender } from '@test/unit/helpers/litRuntime' |
| 7 | +import { getPrivacyFormElements } from '../selectors' |
| 8 | + |
| 9 | +type PrivacyFormModule = WebComponentModule<PrivacyFormElementInstance> |
| 10 | + |
| 11 | +type ActionResult<TData> = { data?: TData; error?: { message?: string } } |
| 12 | + |
| 13 | +type VerifyResult = |
| 14 | + | { status: 'download'; filename: string; json: string } |
| 15 | + | { status: 'deleted' } |
| 16 | + | { status: 'expired' } |
| 17 | + |
| 18 | +const requestDataMock = vi.fn< |
| 19 | + (_input: { email: string; requestType: 'ACCESS' | 'DELETE' }) => Promise<ActionResult<{ message: string }>> |
| 20 | +>() |
| 21 | + |
| 22 | +const verifyDsarMock = vi.fn<(_input: { token: string }) => Promise<ActionResult<VerifyResult>>>() |
| 23 | + |
| 24 | +vi.mock('astro:actions', () => ({ |
| 25 | + actions: { |
| 26 | + gdpr: { |
| 27 | + requestData: requestDataMock, |
| 28 | + verifyDsar: verifyDsarMock, |
| 29 | + }, |
| 30 | + }, |
| 31 | +})) |
| 32 | + |
| 33 | +async function flushMicrotasks(): Promise<void> { |
| 34 | + await Promise.resolve() |
| 35 | + await Promise.resolve() |
| 36 | +} |
| 37 | + |
| 38 | +describe('PrivacyForm behavior', () => { |
| 39 | + let container: AstroContainer |
| 40 | + |
| 41 | + beforeEach(async () => { |
| 42 | + container = await AstroContainer.create() |
| 43 | + requestDataMock.mockReset() |
| 44 | + verifyDsarMock.mockReset() |
| 45 | + }) |
| 46 | + |
| 47 | + it('submits an access request and shows success', async () => { |
| 48 | + requestDataMock.mockResolvedValue({ data: { message: 'Access request sent.' } }) |
| 49 | + |
| 50 | + await executeRender<PrivacyFormModule>({ |
| 51 | + container, |
| 52 | + component: PrivacyForm, |
| 53 | + moduleSpecifier: '@components/Forms/Privacy/client/index', |
| 54 | + args: { |
| 55 | + props: { |
| 56 | + status: undefined, |
| 57 | + }, |
| 58 | + }, |
| 59 | + waitForReady: async (element: PrivacyFormElementInstance) => { |
| 60 | + window.history.replaceState({}, '', 'http://localhost/privacy/my-data') |
| 61 | + element.initialize() |
| 62 | + }, |
| 63 | + assert: async ({ element }) => { |
| 64 | + const elements = getPrivacyFormElements(element) |
| 65 | + elements.accessEmailInput.value = 'test@example.com' |
| 66 | + |
| 67 | + elements.accessForm.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) |
| 68 | + await flushMicrotasks() |
| 69 | + |
| 70 | + expect(requestDataMock).toHaveBeenCalledWith({ email: 'test@example.com', requestType: 'ACCESS' }) |
| 71 | + expect(elements.accessMessage.textContent).toBe('Access request sent.') |
| 72 | + expect(elements.accessMessage.classList.contains('hidden')).toBe(false) |
| 73 | + expect(elements.accessMessage.classList.contains('border-success')).toBe(true) |
| 74 | + expect(elements.accessEmailInput.value).toBe('') |
| 75 | + }, |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + it('blocks delete submit when confirmation is not checked', async () => { |
| 80 | + requestDataMock.mockResolvedValue({ data: { message: 'Delete request sent.' } }) |
| 81 | + |
| 82 | + await executeRender<PrivacyFormModule>({ |
| 83 | + container, |
| 84 | + component: PrivacyForm, |
| 85 | + moduleSpecifier: '@components/Forms/Privacy/client/index', |
| 86 | + args: { |
| 87 | + props: { |
| 88 | + status: undefined, |
| 89 | + }, |
| 90 | + }, |
| 91 | + waitForReady: async (element: PrivacyFormElementInstance) => { |
| 92 | + window.history.replaceState({}, '', 'http://localhost/privacy/my-data') |
| 93 | + element.initialize() |
| 94 | + }, |
| 95 | + assert: async ({ element }) => { |
| 96 | + const elements = getPrivacyFormElements(element) |
| 97 | + elements.deleteEmailInput.value = 'test@example.com' |
| 98 | + elements.deleteConfirmCheckbox.checked = false |
| 99 | + |
| 100 | + elements.deleteForm.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) |
| 101 | + await flushMicrotasks() |
| 102 | + |
| 103 | + expect(requestDataMock).not.toHaveBeenCalled() |
| 104 | + expect(elements.deleteMessage.textContent).toBe('Please confirm you understand the deletion request.') |
| 105 | + expect(elements.deleteMessage.classList.contains('border-danger')).toBe(true) |
| 106 | + }, |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + it('verifies token, downloads JSON, then redirects', async () => { |
| 111 | + verifyDsarMock.mockResolvedValue({ |
| 112 | + data: { status: 'download', filename: 'dsar.json', json: '{"ok":true}' }, |
| 113 | + }) |
| 114 | + |
| 115 | + const downloadJsonSpy = vi.fn() |
| 116 | + const navigateToSpy = vi.fn() |
| 117 | + |
| 118 | + await executeRender<PrivacyFormModule>({ |
| 119 | + container, |
| 120 | + component: PrivacyForm, |
| 121 | + moduleSpecifier: '@components/Forms/Privacy/client/index', |
| 122 | + args: { |
| 123 | + props: { |
| 124 | + status: undefined, |
| 125 | + }, |
| 126 | + }, |
| 127 | + waitForReady: async (element: PrivacyFormElementInstance) => { |
| 128 | + window.history.replaceState({}, '', 'http://localhost/privacy/my-data?token=unit-test-token') |
| 129 | + ;(element as unknown as { downloadJson: typeof downloadJsonSpy }).downloadJson = downloadJsonSpy |
| 130 | + ;(element as unknown as { navigateTo: typeof navigateToSpy }).navigateTo = navigateToSpy |
| 131 | + element.initialize() |
| 132 | + }, |
| 133 | + assert: async () => { |
| 134 | + await flushMicrotasks() |
| 135 | + |
| 136 | + expect(verifyDsarMock).toHaveBeenCalledWith({ token: 'unit-test-token' }) |
| 137 | + expect(downloadJsonSpy).toHaveBeenCalledWith('dsar.json', '{"ok":true}') |
| 138 | + expect(navigateToSpy).toHaveBeenCalledWith('/privacy/my-data?status=already-completed') |
| 139 | + }, |
| 140 | + }) |
| 141 | + }) |
| 142 | +}) |
0 commit comments