|
5 | 5 | import { render, screen } from '@testing-library/react'; |
6 | 6 | import userEvent from '@testing-library/user-event'; |
7 | 7 | import type { AssignmentStatus, Token } from 'interlinearizer'; |
8 | | -import type { ReactNode } from 'react'; |
9 | 8 | import { AnalysisStoreProvider } from '../../components/AnalysisStore'; |
10 | 9 | import { PhraseBox } from '../../components/PhraseBox'; |
11 | 10 |
|
12 | | -// --------------------------------------------------------------------------- |
13 | | -// AnalysisStore mock — reactive useState-based stub so AnalysisStore.tsx stays out of scope |
14 | | -// --------------------------------------------------------------------------- |
15 | | - |
16 | | -jest.mock('../../components/AnalysisStore', () => { |
17 | | - const { createContext, useCallback, useContext, useMemo, useState } = |
18 | | - jest.requireActual<typeof import('react')>('react'); |
19 | | - |
20 | | - type GlossMap = Record<string, string>; |
21 | | - type MockCtxValue = { |
22 | | - glosses: GlossMap; |
23 | | - dispatch: (tokenRef: string, surfaceText: string, value: string) => void; |
24 | | - }; |
25 | | - const MockCtx = createContext<MockCtxValue>({ glosses: {}, dispatch: () => {} }); |
26 | | - |
27 | | - return { |
28 | | - __esModule: true, |
29 | | - AnalysisStoreProvider({ |
30 | | - children, |
31 | | - initialAnalysis, |
32 | | - analysisLanguage, |
33 | | - onGlossChange, |
34 | | - }: Readonly<{ |
35 | | - children: ReactNode; |
36 | | - initialAnalysis?: { |
37 | | - tokenAnalyses: { id: string; gloss?: GlossMap }[]; |
38 | | - tokenAnalysisLinks: { |
39 | | - analysisId: string; |
40 | | - status: AssignmentStatus; |
41 | | - token: { tokenRef: string }; |
42 | | - }[]; |
43 | | - }; |
44 | | - analysisLanguage: string; |
45 | | - onGlossChange?: (tokenRef: string, value: string) => void; |
46 | | - }>) { |
47 | | - const byId = new Map((initialAnalysis?.tokenAnalyses ?? []).map((ta) => [ta.id, ta])); |
48 | | - const seed: GlossMap = (initialAnalysis?.tokenAnalysisLinks ?? []) |
49 | | - .filter((link) => link.status === 'approved') |
50 | | - .reduce((acc, link) => { |
51 | | - const gloss = byId.get(link.analysisId)?.gloss?.[analysisLanguage]; |
52 | | - return gloss === undefined ? acc : { ...acc, [link.token.tokenRef]: gloss }; |
53 | | - }, {}); |
54 | | - const [glosses, setGlosses] = useState<GlossMap>(seed); |
55 | | - const dispatch = useCallback( |
56 | | - (tokenRef: string, _surfaceText: string, value: string) => { |
57 | | - setGlosses((prev) => ({ ...prev, [tokenRef]: value })); |
58 | | - onGlossChange?.(tokenRef, value); |
59 | | - }, |
60 | | - [onGlossChange], |
61 | | - ); |
62 | | - const ctx = useMemo(() => ({ glosses, dispatch }), [glosses, dispatch]); |
63 | | - return <MockCtx value={ctx}>{children}</MockCtx>; |
64 | | - }, |
65 | | - useGloss(tokenRef: string) { |
66 | | - return useContext(MockCtx).glosses[tokenRef] ?? ''; |
67 | | - }, |
68 | | - useGlossDispatch() { |
69 | | - return useContext(MockCtx).dispatch; |
70 | | - }, |
71 | | - }; |
72 | | -}); |
| 11 | +jest.mock('../../components/AnalysisStore'); |
73 | 12 |
|
74 | 13 | jest.mock('../../components/TokenChip', () => { |
75 | 14 | const { useGloss, useGlossDispatch } = jest.requireMock< |
|
0 commit comments