Skip to content

Commit dab283d

Browse files
Enhance README and Jest mocks for improved testing clarity
- Updated README to specify the single mock for `@papi/backend` and its usage in Jest tests. - Modified `web-view-inline.js` mock to return `null` instead of `undefined` for better React compliance. - Removed redundant `@papi/backend` mock file to streamline mock management. - Clarified comments in `interlinearizer.web-view.test.tsx` regarding the web-view module loading pattern. - Improved type handling in `interlinearXmlParser.ts` for index and length attributes.
1 parent bb1a4a3 commit dab283d

File tree

6 files changed

+24
-43
lines changed

6 files changed

+24
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The general file structure for an extension is as follows:
100100
- `*.web-view.scss` files provide styles for WebViews
101101
- `*.web-view.html` files are a conventional way to provide HTML WebViews (no special functionality)
102102
- `src/__tests__/` contains unit tests (Jest) for the extension, including parser tests and web-view tests
103-
- `__mocks__/` contains Jest mocks for the PAPI, file modules, and test fixtures used by tests in `src/__tests__/`
103+
- `__mocks__/` contains Jest mocks for the PAPI, file modules, and test fixtures used by tests in `src/__tests__/`. The single `@papi/backend` mock is `__mocks__/papi-backend.js` (Jest's `moduleNameMapper` in `jest.config.ts` points `@papi/backend` here)
104104
- `assets/` contains asset files the extension and its WebViews can retrieve using the `papi-extension:` protocol, as well as textual descriptions in various languages. It is copied into the build folder
105105
- `assets/displayData.json` contains (optionally) a path to the extension's icon file as well as text for the extension's display name, short summary, and path to the full description file
106106
- `assets/descriptions/` contains textual descriptions of the extension in various languages

__mocks__/@papi/backend.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

__mocks__/papi-backend.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
* Jest mock for @papi/backend. Provides papi and logger so main.ts can be unit-tested without
33
* loading the real Platform API.
44
*
5-
* Main.ts uses: import papi, { logger } from '@papi/backend' With esModuleInterop, default export
6-
* must be the papi object; logger as named export.
5+
* This is the single mock for @papi/backend. Jest's moduleNameMapper in jest.config.ts maps
6+
* '^@papi/backend$' to this file; the __mocks__/@papi/backend.js location is not used so we
7+
* avoid maintaining two identical mocks.
8+
*
9+
* Main.ts uses: import papi, { logger } from '@papi/backend'. With esModuleInterop, default
10+
* export must be the papi object; logger as named export.
711
*/
812

913
const mockRegisterWebViewProvider = jest.fn().mockResolvedValue({ dispose: jest.fn() });

__mocks__/web-view-inline.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/**
22
* Jest mock for webpack ?inline component import (interlinearizer.web-view?inline). Exports a dummy
33
* function so main.ts can pass it to the WebView provider without pulling in React.
4+
* Returns null so that if this mock is ever rendered as a React component, it follows React
5+
* semantics (null = render nothing; undefined can trigger strict-mode warnings).
46
*/
57
function MockWebViewComponent() {
6-
return undefined;
8+
return null;
79
}
810

911
module.exports = MockWebViewComponent;

src/__tests__/interlinearizer.web-view.test.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ jest.mock('../parsers/interlinearXmlParser', () => {
3131
};
3232
});
3333

34-
/** Load the web-view module; it assigns the component to globalThis.webViewComponent. */
34+
/**
35+
* Load the web-view module; it assigns the component to globalThis.webViewComponent. This pattern
36+
* is required by the Platform.Bible web-view framework: the web-view entry is built with a ?inline
37+
* query and consumed by main.ts, so the component is not a normal export. Tests that need to render
38+
* the component must require() the module and read globalThis. If the web-view export mechanism
39+
* changes, update this test accordingly.
40+
*/
3541
require('../interlinearizer.web-view');
3642

3743
const InterlinearizerWebView = globalThis.webViewComponent;

src/parsers/interlinearXmlParser.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {
1111
// ---------------------------------------------------------------------------
1212
// Internal types: raw shape from fast-xml-parser with attributeNamePrefix: '@_'.
1313
// Public API types (InterlinearData, VerseData, etc.) live in interlinearizer.
14+
//
15+
// Style: prefer undefined and empty defaults over null in this module.
1416
// ---------------------------------------------------------------------------
1517

1618
/**
@@ -124,16 +126,16 @@ function extractPunctuationsFromVerse(verseDataElement: ParsedVerseData): Punctu
124126
.filter((el): el is ParsedPunctuation & { Range: ParsedRange } => {
125127
const rangeElement = el.Range;
126128
if (!rangeElement) return false;
127-
const indexRaw = Number(rangeElement['@_Index']);
128-
const lengthRaw = Number(rangeElement['@_Length']);
129-
return Number.isFinite(indexRaw) && Number.isFinite(lengthRaw);
129+
const index = Number(rangeElement['@_Index']);
130+
const length = Number(rangeElement['@_Length']);
131+
return Number.isFinite(index) && Number.isFinite(length);
130132
})
131133
.map((el) => {
132134
const rangeElement = el.Range;
133135
return {
134136
TextRange: {
135-
Index: rangeElement['@_Index'],
136-
Length: rangeElement['@_Length'],
137+
Index: Number(rangeElement['@_Index']),
138+
Length: Number(rangeElement['@_Length']),
137139
},
138140
BeforeText: el.BeforeText ?? '',
139141
AfterText: el.AfterText ?? '',

0 commit comments

Comments
 (0)