/* eslint-env jest */ import { act } from 'react'; import { createRoot, type Root } from 'react-dom/client'; import { useAppStateContext } from '../../context/user.data.context'; import { isContentSiteType, useContentSite } from '../content-site'; jest.mock('../../context/user.data.context', () => ({ useAppStateContext: jest.fn(), })); ( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean } ).IS_REACT_ACT_ENVIRONMENT = true; const mockedContext = useAppStateContext as jest.Mock; /** * Prints what the hook answers, so the rendered text can be read. * * @return {JSX.Element} The answer. */ const SiteTypeProbe = (): JSX.Element => { const reading = useContentSite() ? 'content site' : 'store'; return

{reading}

; }; let root: Root; let host: HTMLDivElement; beforeEach(() => { host = document.createElement('div'); document.body.appendChild(host); root = createRoot(host); }); afterEach(() => { act(() => root.unmount()); host.remove(); }); const readingFor = (user: unknown): string => { mockedContext.mockReturnValue({ user }); act(() => root.render()); return host.textContent ?? ''; }; describe('isContentSiteType', () => { it('reads only an account marked CONTENT as a content site', () => { expect(isContentSiteType('CONTENT')).toBe(true); expect(isContentSiteType('ECOMMERCE')).toBe(false); }); // An older /me without the field keeps today's store screens. it('reads a missing site type as a store', () => { expect(isContentSiteType(undefined)).toBe(false); expect(isContentSiteType(null)).toBe(false); expect(isContentSiteType('')).toBe(false); }); }); describe('useContentSite', () => { it('answers from the /me the app holds', () => { expect(readingFor({ site_type: 'CONTENT' })).toBe('content site'); expect(readingFor({ site_type: 'ECOMMERCE' })).toBe('store'); }); it('reads as a store before /me has answered', () => { expect(readingFor(null)).toBe('store'); }); });