|
1 | 1 | /** |
2 | | - * @file Jest mock for @papi/frontend/react. Provides stub implementations of various PAPI React hooks so |
| 2 | + * @file Jest mock for @papi/frontend/react. Provides stub implementations of PAPI React hooks so |
3 | 3 | * WebView/frontend components can be unit-tested without the real Platform API. |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | /** |
7 | | - * useData('providerName') returns an object whose keys are data type names and values are hooks. |
8 | | - * Mock: any property returns a function that returns [undefined, setter, false]. |
| 7 | + * Mock for `useProjectData`. Returns a `Proxy` whose property accesses each yield a function |
| 8 | + * returning `[undefined, jest.fn(), false]`, matching the real hook's `[data, setter, isLoading]` |
| 9 | + * tuple without requiring a live data provider. |
9 | 10 | */ |
10 | | -const createUseDataLikeHook = () => |
11 | | - jest.fn(() => |
12 | | - new Proxy( |
13 | | - {}, |
14 | | - { |
15 | | - get: () => () => [undefined, jest.fn(), false], |
16 | | - }, |
17 | | - ), |
18 | | - ); |
| 11 | +const useProjectData = jest.fn(() => |
| 12 | + new Proxy( |
| 13 | + {}, |
| 14 | + { |
| 15 | + get: () => () => [undefined, jest.fn(), false], |
| 16 | + }, |
| 17 | + ), |
| 18 | +); |
19 | 19 |
|
20 | | -const useDataProvider = jest.fn().mockReturnValue(undefined); |
21 | | -const useData = createUseDataLikeHook(); |
22 | | -const useScrollGroupScrRef = jest.fn().mockReturnValue([undefined, jest.fn()]); |
23 | | -const useSetting = jest.fn().mockImplementation((_key: string, defaultState: unknown) => [defaultState, jest.fn()]); |
24 | | -const useProjectData = createUseDataLikeHook(); |
25 | | -const useProjectDataProvider = jest.fn().mockReturnValue(undefined); |
| 20 | +/** |
| 21 | + * Mock for `useProjectSetting`. Returns `[defaultState, setSetting, resetSetting, isLoading]`, |
| 22 | + * passing `defaultState` through unchanged so callers receive a predictable initial value. |
| 23 | + * |
| 24 | + * @param _projectDataProviderSource - Ignored project data provider source. |
| 25 | + * @param _key - Ignored setting key. |
| 26 | + * @param defaultState - Value surfaced as the current setting state. |
| 27 | + * @returns Tuple of `[defaultState, jest.fn(), jest.fn(), false]`. |
| 28 | + */ |
26 | 29 | const useProjectSetting = jest |
27 | 30 | .fn() |
28 | | - .mockImplementation((_projectInterface: string, _projectIdOrPdp: unknown, _key: string, defaultState: unknown) => [ |
| 31 | + .mockImplementation((_projectDataProviderSource: unknown, _key: string, defaultState: unknown) => [ |
29 | 32 | defaultState, |
30 | 33 | jest.fn(), |
| 34 | + jest.fn(), |
| 35 | + false, |
31 | 36 | ]); |
32 | | -const useDialogCallback = jest.fn().mockReturnValue(jest.fn()); |
33 | | -const useDataProviderMulti = jest.fn().mockReturnValue([]); |
34 | | -/** Returns a map of localization key -> key (so tests get a string for each key). */ |
35 | | -const useLocalizedStrings = jest.fn().mockImplementation((keys: string[]) => |
36 | | - Array.isArray(keys) ? keys.reduce<Record<string, string>>((acc, k) => ({ ...acc, [k]: k }), {}) : {}, |
37 | | -); |
38 | | -const useWebViewController = jest.fn().mockReturnValue(undefined); |
39 | | -const useRecentScriptureRefs = jest.fn().mockReturnValue([]); |
| 37 | + |
| 38 | +/** |
| 39 | + * Mock for `useLocalizedStrings`. Maps each requested key to itself so tests receive a |
| 40 | + * predictable `Record<string, string>` without a real localization service. |
| 41 | + * |
| 42 | + * @param keys - BCP47-style string keys to resolve. |
| 43 | + * @returns Tuple of `[record, isLoading]` where every key maps to itself and `isLoading` is |
| 44 | + * `false`. |
| 45 | + */ |
| 46 | +const useLocalizedStrings = jest.fn().mockImplementation((keys: string[]) => [ |
| 47 | + Array.isArray(keys) ? keys.reduce<Record<string, string>>((acc, k) => { acc[k] = k; return acc; }, {}) : {}, |
| 48 | + false, |
| 49 | +]); |
| 50 | + |
| 51 | +/** |
| 52 | + * Mock for `useRecentScriptureRefs`. Returns an empty history and a no-op `addRecentScriptureRef` |
| 53 | + * so components that display recent references render without errors. |
| 54 | + * |
| 55 | + * @returns Object with `recentScriptureRefs` (empty array) and `addRecentScriptureRef` (jest spy). |
| 56 | + */ |
| 57 | +const useRecentScriptureRefs = jest |
| 58 | + .fn() |
| 59 | + .mockImplementation(() => ({ recentScriptureRefs: [], addRecentScriptureRef: jest.fn() })); |
40 | 60 |
|
41 | 61 | module.exports = { |
42 | 62 | __esModule: true, |
43 | | - useDataProvider, |
44 | | - useData, |
45 | | - useScrollGroupScrRef, |
46 | | - useSetting, |
47 | 63 | useProjectData, |
48 | | - useProjectDataProvider, |
49 | 64 | useProjectSetting, |
50 | | - useDialogCallback, |
51 | | - useDataProviderMulti, |
52 | 65 | useLocalizedStrings, |
53 | | - useWebViewController, |
54 | 66 | useRecentScriptureRefs, |
55 | | - __mockUseDataProvider: useDataProvider, |
56 | | - __mockUseData: useData, |
57 | | - __mockUseLocalizedStrings: useLocalizedStrings, |
58 | | - __mockUseSetting: useSetting, |
59 | | - __mockUseProjectData: useProjectData, |
60 | | - __mockUseProjectDataProvider: useProjectDataProvider, |
61 | | - __mockUseProjectSetting: useProjectSetting, |
62 | | - __mockUseWebViewController: useWebViewController, |
63 | 67 | }; |
| 68 | + |
| 69 | +/** Marks this file as a module so top-level const/let are module-scoped. */ |
| 70 | +export {}; |
0 commit comments