import { beforeEach, describe, expect, it } from 'vitest'; import { screen, waitFor, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { renderApp, mockAuthenticated } from '@/test/utils'; import { getMockBanners, resetMockOfferings, setMockBanners } from '@/features/offerings/mocks/handlers'; import { bannerStatus, fromLocalInput, isAllowedBannerLink, offeringBannerFormSchema, toLocalInput, } from '@/features/offerings/schemas/offeringBanner'; import type { OfferingBannerDto } from '@/features/offerings/services/types'; import { _resetSetupStatusCache } from '@/router'; beforeEach(() => { _resetSetupStatusCache(); resetMockOfferings(); }); const T = { timeout: 10000 }; async function openBannerTab() { mockAuthenticated(); renderApp('/offerings'); await userEvent.click(await screen.findByTestId('offerings-tab-banner', {}, T)); } const banner = (over: Partial & { id: string }): OfferingBannerDto => ({ position: 'bottom', title: 'Titel', text: 'Tekst', linkLabel: null, linkUrl: null, isActive: true, startsAt: null, endsAt: null, updatedAt: '2026-01-01T00:00:00Z', ...over, }); describe('Banner tab', () => { it('switches between the offerings and the banner tab, hiding the add-offering actions on the banner tab', async () => { mockAuthenticated(); renderApp('/offerings'); expect(await screen.findByTestId('offerings-add-button', {}, T)).toBeInTheDocument(); await userEvent.click(screen.getByTestId('offerings-tab-banner')); expect(await screen.findByTestId('banners-tab')).toBeInTheDocument(); expect(screen.getByTestId('offerings-tab-banner')).toHaveAttribute('aria-selected', 'true'); expect(screen.queryByTestId('offerings-add-button')).not.toBeInTheDocument(); expect(screen.queryByTestId('offerings-add-fab')).not.toBeInTheDocument(); await userEvent.click(screen.getByTestId('offerings-tab-offerings')); expect(await screen.findByTestId('offerings-add-button')).toBeInTheDocument(); }); it('shows two sections (top and bottom), each with its own empty state and "nothing active" hint', async () => { await openBannerTab(); const top = await screen.findByTestId('banners-section-top', {}, T); const bottom = screen.getByTestId('banners-section-bottom'); expect(within(top).getByTestId('banners-empty-top')).toBeInTheDocument(); expect(within(bottom).getByTestId('banners-empty-bottom')).toBeInTheDocument(); expect(within(top).getByTestId('banner-add-top')).toBeInTheDocument(); expect(within(bottom).getByTestId('banner-add-bottom')).toBeInTheDocument(); expect(screen.getByTestId('banner-live-hint-top')).toHaveTextContent(/niets actief|Nothing is active/); expect(screen.getByTestId('banner-live-hint-bottom')).toHaveTextContent(/niets actief|Nothing is active/); }); it('creates a bottom banner with a link and a schedule, sending position bottom', async () => { await openBannerTab(); expect(await screen.findByTestId('banners-empty-bottom', {}, T)).toBeInTheDocument(); await userEvent.click(screen.getByTestId('banner-add-bottom')); await userEvent.type(await screen.findByTestId('banner-title'), 'Zomeractie'); await userEvent.type(screen.getByTestId('banner-text'), '10% korting in juli'); await userEvent.type(screen.getByTestId('banner-link-label'), 'Bekijk'); await userEvent.type(screen.getByTestId('banner-link-url'), '/privacy'); await userEvent.type(screen.getByTestId('banner-starts'), '2030-07-01T09:00'); await userEvent.type(screen.getByTestId('banner-ends'), '2030-07-31T23:00'); await userEvent.click(screen.getByTestId('banner-submit')); await waitFor(() => expect(getMockBanners()).toHaveLength(1)); const saved = getMockBanners()[0]; expect(saved).toMatchObject({ position: 'bottom', title: 'Zomeractie', linkLabel: 'Bekijk', linkUrl: '/privacy', isActive: true, }); expect(saved.startsAt).toBe(fromLocalInput('2030-07-01T09:00')); expect(await screen.findByText('Zomeractie')).toBeInTheDocument(); expect(within(screen.getByTestId('banners-section-bottom')).getByTestId(`banner-status-${saved.id}`)).toHaveTextContent( /Gepland|Scheduled/, ); expect(screen.getByTestId('banners-empty-top')).toBeInTheDocument(); }); it('requires a title for a bottom banner', async () => { await openBannerTab(); await userEvent.click(await screen.findByTestId('banner-add-bottom', {}, T)); await userEvent.type(await screen.findByTestId('banner-text'), 'Alleen tekst'); await userEvent.click(screen.getByTestId('banner-submit')); expect(await screen.findByTestId('banner-title-error')).toBeInTheDocument(); expect(getMockBanners()).toHaveLength(0); }); it('creates a top banner without a title, limited to 120 characters with a counter', async () => { await openBannerTab(); await userEvent.click(await screen.findByTestId('banner-add-top', {}, T)); expect(await screen.findByTestId('banner-text')).toHaveAttribute('maxlength', '120'); expect(screen.getByTestId('banner-text-counter')).toHaveTextContent('0/120'); await userEvent.type(screen.getByTestId('banner-text'), 'Gratis verzending deze week'); expect(screen.getByTestId('banner-text-counter')).toHaveTextContent('27/120'); await userEvent.click(screen.getByTestId('banner-submit')); await waitFor(() => expect(getMockBanners()).toHaveLength(1)); expect(getMockBanners()[0]).toMatchObject({ position: 'top', title: '', text: 'Gratis verzending deze week' }); const top = screen.getByTestId('banners-section-top'); expect(await within(top).findByText('Gratis verzending deze week')).toBeInTheDocument(); expect(screen.getByTestId('banners-empty-bottom')).toBeInTheDocument(); }); it('rejects an unsafe link and a link without button text, and an end before the start', async () => { await openBannerTab(); await userEvent.click(await screen.findByTestId('banner-add-bottom', {}, T)); await userEvent.type(await screen.findByTestId('banner-title'), 'X'); await userEvent.type(screen.getByTestId('banner-text'), 'Y'); await userEvent.type(screen.getByTestId('banner-link-label'), 'Go'); await userEvent.type(screen.getByTestId('banner-link-url'), 'javascript:alert(1)'); await userEvent.click(screen.getByTestId('banner-submit')); expect(await screen.findByTestId('banner-link-url-error')).toBeInTheDocument(); await userEvent.clear(screen.getByTestId('banner-link-label')); await userEvent.clear(screen.getByTestId('banner-link-url')); await userEvent.type(screen.getByTestId('banner-link-url'), '/ok'); await userEvent.click(screen.getByTestId('banner-submit')); expect(await screen.findByTestId('banner-link-label-error')).toBeInTheDocument(); await userEvent.clear(screen.getByTestId('banner-link-url')); await userEvent.type(screen.getByTestId('banner-starts'), '2030-07-31T09:00'); await userEvent.type(screen.getByTestId('banner-ends'), '2030-07-01T09:00'); await userEvent.click(screen.getByTestId('banner-submit')); expect(await screen.findByTestId('banner-ends-error')).toBeInTheDocument(); expect(getMockBanners()).toHaveLength(0); }); it('lists banners under their own position and shows which one is live (most recently changed wins)', async () => { setMockBanners([ banner({ id: 'b-old', title: 'Oud', updatedAt: '2026-01-01T00:00:00Z' }), banner({ id: 'b-new', title: 'Nieuw', updatedAt: '2026-02-01T00:00:00Z' }), banner({ id: 't1', position: 'top', title: '', text: 'Balktekst' }), ]); await openBannerTab(); const top = await screen.findByTestId('banners-section-top', {}, T); const bottom = screen.getByTestId('banners-section-bottom'); expect(within(top).getByTestId('banner-row-t1')).toBeInTheDocument(); expect(within(top).queryByTestId('banner-row-b-new')).not.toBeInTheDocument(); expect(within(bottom).getByTestId('banner-row-b-old')).toBeInTheDocument(); expect(within(bottom).getByTestId('banner-row-b-new')).toBeInTheDocument(); expect(screen.getByTestId('banner-live-hint-bottom')).toHaveTextContent(/Nu live: Nieuw|Live now: Nieuw/); expect(screen.getByTestId('banner-live-hint-top')).toHaveTextContent(/Balktekst/); }); it('turns a banner off and on from its section', async () => { setMockBanners([banner({ id: 't1', position: 'top', title: '', text: 'Balktekst' })]); await openBannerTab(); expect(await screen.findByTestId('banner-status-t1', {}, T)).toHaveTextContent(/Actief|Active/); await userEvent.click(screen.getByTestId('banner-toggle-t1')); await waitFor(() => expect(getMockBanners()[0].isActive).toBe(false)); expect(getMockBanners()[0].position).toBe('top'); expect(await screen.findByTestId('banner-status-t1')).toHaveTextContent(/Uit|Off/); expect(screen.getByTestId('banner-live-hint-top')).toHaveTextContent(/niets actief|Nothing is active/); await userEvent.click(screen.getByTestId('banner-toggle-t1')); await waitFor(() => expect(getMockBanners()[0].isActive).toBe(true)); }); it('edits and deletes an existing banner, keeping its position', async () => { setMockBanners([banner({ id: 'b1', title: 'Oud' })]); await openBannerTab(); expect(await screen.findByTestId('banner-status-b1', {}, T)).toHaveTextContent(/Actief|Active/); await userEvent.click(screen.getByTestId('banner-edit-b1')); const title = await screen.findByTestId('banner-title'); expect(title).toHaveValue('Oud'); await userEvent.clear(title); await userEvent.type(title, 'Nieuw'); await userEvent.click(screen.getByTestId('banner-active')); await userEvent.click(screen.getByTestId('banner-submit')); await waitFor(() => expect(getMockBanners()[0]).toMatchObject({ title: 'Nieuw', isActive: false, position: 'bottom' })); expect(await screen.findByTestId('banner-status-b1')).toHaveTextContent(/Uit|Off/); await userEvent.click(screen.getByTestId('banner-delete-b1')); expect(await screen.findByTestId('banner-delete-message')).toHaveTextContent('Nieuw'); await userEvent.click(screen.getByTestId('banner-delete-confirm')); await waitFor(() => expect(getMockBanners()).toHaveLength(0)); expect(await screen.findByTestId('banners-empty-bottom')).toBeInTheDocument(); }); }); describe('banner form schema', () => { const base = { position: 'bottom' as const, title: 'T', text: 'X', linkLabel: '', linkUrl: '', isActive: true, startsAt: '', endsAt: '', }; const fieldErrors = (v: object) => { const r = offeringBannerFormSchema.safeParse(v); return r.success ? [] : r.error.issues.map((i) => i.path[0]); }; it('bottom: title required, text up to 300', () => { expect(fieldErrors(base)).toEqual([]); expect(fieldErrors({ ...base, title: '' })).toContain('title'); expect(fieldErrors({ ...base, text: 'x'.repeat(300) })).toEqual([]); expect(fieldErrors({ ...base, text: 'x'.repeat(301) })).toContain('text'); }); it('top: title optional, text up to 120', () => { const top = { ...base, position: 'top' as const, title: '' }; expect(fieldErrors(top)).toEqual([]); expect(fieldErrors({ ...top, text: 'x'.repeat(120) })).toEqual([]); expect(fieldErrors({ ...top, text: 'x'.repeat(121) })).toContain('text'); expect(fieldErrors({ ...top, text: '' })).toContain('text'); }); }); describe('banner helpers', () => { it.each([ ['https://slpsoftware.nl/actie', true], ['/aanbieding', true], ['/', true], ['http://x.nl', false], ['//evil.example', false], ['/\\evil', false], ['javascript:alert(1)', false], ['mailto:a@b.nl', false], ['actie', false], ['/with space', false], ])('link %s → %s', (url, ok) => { expect(isAllowedBannerLink(url)).toBe(ok); }); it('derives the status from switch and schedule', () => { const now = new Date('2026-11-12T12:00:00Z'); expect(bannerStatus({ isActive: false, startsAt: null, endsAt: null }, now)).toBe('off'); expect(bannerStatus({ isActive: true, startsAt: '2026-11-13T00:00:00Z', endsAt: null }, now)).toBe('scheduled'); expect(bannerStatus({ isActive: true, startsAt: null, endsAt: '2026-11-12T12:00:00Z' }, now)).toBe('expired'); expect(bannerStatus({ isActive: true, startsAt: '2026-11-01T00:00:00Z', endsAt: '2026-12-01T00:00:00Z' }, now)).toBe('live'); }); it('round-trips datetime-local values', () => { expect(toLocalInput(fromLocalInput('2030-07-01T09:00'))).toBe('2030-07-01T09:00'); expect(fromLocalInput('')).toBeNull(); expect(toLocalInput(null)).toBe(''); }); });