/* eslint-env jest */ import { act } from 'react'; import { createRoot, type Root } from 'react-dom/client'; import type { SeoEditorEnv } from '../../env'; import { getLiveScore, setLiveScore } from '../../lib/live-score'; import { LiveScore } from '../LiveScore'; const meta: Record = {}; jest.mock('../../hooks/use-seo-meta', () => ({ usePostMeta: () => ({ meta, update: () => undefined }), useEditedPost: () => ({ id: 7, title: 'Zipper hoodie', content: '

The zipper hoodie keeps you warm. A zipper hoodie for cold days.

', excerpt: '', slug: 'zipper-hoodie', link: 'https://shop.example/zipper-hoodie/', status: 'draft', featuredImageId: 0, imageAlts: [], terms: {}, }), useAssignedTerms: () => [], })); ( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean } ).IS_REACT_ACT_ENVIRONMENT = true; const env = { object: 'post', type: 'post', typeLabel: 'Post', siteName: 'Shop', siteUrl: 'https://shop.example/', separator: '-', templates: { title: '%title% %sep% %sitename%', description: '' }, taxonomies: [], variables: [], analyzeFields: {}, fields: ['_title', '_keywords', '_score'], keys: { title: '_title', description: '_description', focusKeywords: '_keywords', score: '_score', }, } as unknown as SeoEditorEnv; let root: Root; let host: HTMLDivElement; beforeEach(() => { setLiveScore(null); host = document.createElement('div'); document.body.appendChild(host); root = createRoot(host); }); afterEach(() => { act(() => root.unmount()); host.remove(); delete meta._keywords; }); describe('LiveScore', () => { it('scores the post as edited, with the sidebar closed', () => { meta._keywords = 'zipper hoodie'; act(() => root.render()); const score = getLiveScore(); expect(score).not.toBeNull(); expect(score).toBeGreaterThan(0); }); it('keeps no score without a focus keyword', () => { act(() => root.render()); expect(getLiveScore()).toBeNull(); }); });