-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpapi-frontend-react.ts
More file actions
112 lines (103 loc) · 3.92 KB
/
papi-frontend-react.ts
File metadata and controls
112 lines (103 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @file Jest mock for @papi/frontend/react. Provides stub implementations of PAPI React hooks so
* WebView/frontend components can be unit-tested without the real Platform API.
*/
/**
* Known data-provider method names exposed by this mock. Tests that call an unlisted method will
* receive a descriptive error rather than silently returning `undefined`, which mirrors the real
* PAPI behaviour where requesting an unsupported provider key is a programmer error.
*/
const KNOWN_PROJECT_DATA_METHODS = new Set(['BookUSJ']);
/** Known method names on data providers accessed via {@link useData}. */
const KNOWN_DATA_METHODS = new Set(['WebViewMenu']);
/**
* Mock for `useData`. Returns an object whose known methods each return
* `[undefined, jest.fn(), false]`, matching the real hook's `[data, setter, isLoading]` tuple.
* Accessing an unknown method throws to catch misspelled provider keys in tests.
*
* @param _providerName - Ignored data provider name.
* @returns Proxy object exposing known data methods.
*/
const useData = jest.fn((_providerName: unknown) =>
new Proxy(
{},
{
get(_target, prop: string | symbol) {
if (typeof prop === 'string' && KNOWN_DATA_METHODS.has(prop)) {
return () => [undefined, jest.fn(), false];
}
throw new Error(
`useData mock: unknown method "${String(prop)}". Add it to KNOWN_DATA_METHODS if intentional.`,
);
},
},
),
);
/**
* Mock for `useProjectData`. Returns an object whose known methods each return
* `[undefined, jest.fn(), false]`, matching the real hook's `[data, setter, isLoading]` tuple.
* Accessing an unknown method throws to catch misspelled provider keys in tests.
*/
const useProjectData = jest.fn(() =>
new Proxy(
{},
{
get(_target, prop: string | symbol) {
if (typeof prop === 'string' && KNOWN_PROJECT_DATA_METHODS.has(prop)) {
return () => [undefined, jest.fn(), false];
}
throw new Error(
`useProjectData mock: unknown method "${String(prop)}". Add it to KNOWN_PROJECT_DATA_METHODS if intentional.`,
);
},
},
),
);
/**
* Mock for `useProjectSetting`. Returns `[defaultState, setSetting, resetSetting, isLoading]`,
* passing `defaultState` through unchanged so callers receive a predictable initial value.
*
* @param _projectDataProviderSource - Ignored project data provider source.
* @param _key - Ignored setting key.
* @param defaultState - Value surfaced as the current setting state.
* @returns Tuple of `[defaultState, jest.fn(), jest.fn(), false]`.
*/
const useProjectSetting = jest
.fn()
.mockImplementation((_projectDataProviderSource: unknown, _key: string, defaultState: unknown) => [
defaultState,
jest.fn(),
jest.fn(),
false,
]);
/**
* Mock for `useLocalizedStrings`. Maps each requested key to itself so tests receive a
* predictable `Record<string, string>` without a real localization service.
*
* @param keys - BCP47-style string keys to resolve.
* @returns Tuple of `[record, isLoading]` where every key maps to itself and `isLoading` is
* `false`.
*/
const useLocalizedStrings = jest.fn().mockImplementation((keys: string[]) => [
Array.isArray(keys) ? keys.reduce<Record<string, string>>((acc, k) => { acc[k] = k; return acc; }, {}) : {},
false,
]);
/**
* Mock for `useRecentScriptureRefs`. Returns an empty history and a no-op `addRecentScriptureRef`
* so components that display recent references render without errors.
*
* @returns Object with `recentScriptureRefs` (empty array) and `addRecentScriptureRef` (jest spy).
*/
const useRecentScriptureRefs = jest
.fn()
.mockImplementation(() => ({ recentScriptureRefs: [], addRecentScriptureRef: jest.fn() }));
module.exports = {
__esModule: true,
useData,
useProjectData,
useProjectSetting,
useLocalizedStrings,
useRecentScriptureRefs,
};
/** Marks this file as a module so top-level const/let are module-scoped. */
export {};