|
4 | 4 |
|
5 | 5 | import type { WebViewProps } from '@papi/core'; |
6 | 6 | import type { SerializedVerseRef } from '@sillsdev/scripture'; |
7 | | -import { render, screen } from '@testing-library/react'; |
8 | | -import { InterlinearXmlParser } from 'parsers/interlinearXmlParser'; |
9 | | - |
10 | | -/** Mock parser to allow overriding constructor behavior per test. */ |
11 | | -jest.mock('parsers/interlinearXmlParser', () => { |
12 | | - const actual = jest.requireActual<typeof import('parsers/interlinearXmlParser')>( |
13 | | - 'parsers/interlinearXmlParser', |
14 | | - ); |
15 | | - return { |
16 | | - InterlinearXmlParser: jest.fn().mockImplementation(() => new actual.InterlinearXmlParser()), |
17 | | - }; |
18 | | -}); |
| 7 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 8 | +import type { InterlinearData } from 'paratext-9-types'; |
| 9 | + |
| 10 | +/** Stub InterlinearData returned by the mocked parser. Matches shape the WebView displays. */ |
| 11 | +const stubInterlinearData: InterlinearData = { |
| 12 | + glossLanguage: 'en', |
| 13 | + bookId: 'MAT', |
| 14 | + verses: {}, |
| 15 | +}; |
| 16 | + |
| 17 | +/** Stub Interlinearization returned by the mocked converter. Matches shape the WebView displays. */ |
| 18 | +const stubInterlinearization = { |
| 19 | + id: 'mock-interlinear-id', |
| 20 | + sourceWritingSystem: '', |
| 21 | + analysisLanguages: ['en'], |
| 22 | + books: [{ id: 'mock-book-id', bookRef: 'MAT', textVersion: '', segments: [] }], |
| 23 | +}; |
| 24 | + |
| 25 | +const mockParse = jest.fn().mockReturnValue(stubInterlinearData); |
| 26 | +const mockConvert = jest.fn().mockReturnValue(stubInterlinearization); |
| 27 | + |
| 28 | +/** Mock parser: no real XML parsing; returns stub data. Parser/converter are tested elsewhere. */ |
| 29 | +jest.mock('parsers/paratext-9/paratext9Parser', () => ({ |
| 30 | + Paratext9Parser: jest.fn().mockImplementation(() => ({ |
| 31 | + parse: mockParse, |
| 32 | + })), |
| 33 | +})); |
| 34 | + |
| 35 | +/** Mock converter: no real conversion; returns stub Interlinearization. */ |
| 36 | +jest.mock('parsers/paratext-9/paratext9Converter', () => ({ |
| 37 | + convertParatext9ToInterlinearization: mockConvert, |
| 38 | +})); |
19 | 39 |
|
20 | 40 | /** |
21 | 41 | * Load the WebView module; it assigns the component to globalThis.webViewComponent. This pattern is |
@@ -66,56 +86,90 @@ describe('InterlinearizerWebView', () => { |
66 | 86 | expect(screen.getByText(/test-data\/Interlinear_en_MAT\.xml/i)).toBeInTheDocument(); |
67 | 87 | }); |
68 | 88 |
|
69 | | - it('parses the bundled test XML and displays parsed JSON', () => { |
| 89 | + it('renders the JSON view mode switch (InterlinearData / Interlinearization)', () => { |
70 | 90 | render(<InterlinearizerWebView {...testWebViewProps} />); |
71 | 91 |
|
72 | | - expect(screen.getByText(/parsed interlinear data \(json\)/i)).toBeInTheDocument(); |
73 | | - expect(screen.getByText(/"GlossLanguage"/)).toBeInTheDocument(); |
74 | | - expect(screen.getByText(/"BookId"/)).toBeInTheDocument(); |
| 92 | + const group = screen.getByRole('group', { name: /json view mode/i }); |
| 93 | + expect(group).toBeInTheDocument(); |
| 94 | + expect(screen.getByRole('button', { name: /^interlineardata$/i })).toBeInTheDocument(); |
| 95 | + expect(screen.getByRole('button', { name: /^interlinearization$/i })).toBeInTheDocument(); |
| 96 | + expect(screen.getByText(/view json as:/i)).toBeInTheDocument(); |
75 | 97 | }); |
76 | 98 |
|
77 | | - it('displays parsed structure with expected verse data', () => { |
| 99 | + it('displays InterlinearData JSON by default when parser returns data', () => { |
| 100 | + render(<InterlinearizerWebView {...testWebViewProps} />); |
| 101 | + |
| 102 | + expect(screen.getByText(/^InterlinearData \(JSON\):$/)).toBeInTheDocument(); |
| 103 | + expect(screen.getByText(/glossLanguage/i)).toBeInTheDocument(); |
| 104 | + expect(screen.getByText(/bookId/i)).toBeInTheDocument(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('displays parsed structure including glossLanguage and bookId values', () => { |
78 | 108 | render(<InterlinearizerWebView {...testWebViewProps} />); |
79 | 109 |
|
80 | 110 | expect(screen.getByText(/"en"/)).toBeInTheDocument(); |
81 | 111 | expect(screen.getByText(/"MAT"/)).toBeInTheDocument(); |
82 | 112 | }); |
83 | 113 |
|
84 | | - it('does not show parse error when XML is valid', () => { |
| 114 | + it('does not show parse error when parser succeeds', () => { |
85 | 115 | render(<InterlinearizerWebView {...testWebViewProps} />); |
86 | 116 |
|
87 | 117 | expect(screen.queryByText(/^parse error$/i)).not.toBeInTheDocument(); |
88 | 118 | }); |
89 | 119 |
|
90 | 120 | it('displays parse error when parser throws an Error (uses err.message)', () => { |
91 | | - const actual = jest.requireActual<typeof import('../parsers/interlinearXmlParser')>( |
92 | | - '../parsers/interlinearXmlParser', |
93 | | - ); |
94 | | - const realInstance = new actual.InterlinearXmlParser(); |
95 | | - const throwingParse = (): never => { |
| 121 | + mockParse.mockImplementationOnce(() => { |
96 | 122 | throw new Error('Invalid XML structure'); |
97 | | - }; |
98 | | - Object.defineProperty(realInstance, 'parse', { value: throwingParse, writable: true }); |
99 | | - jest.mocked(InterlinearXmlParser).mockImplementationOnce(() => realInstance); |
| 123 | + }); |
100 | 124 |
|
101 | 125 | render(<InterlinearizerWebView {...testWebViewProps} />); |
102 | 126 |
|
103 | 127 | expect(screen.getByRole('heading', { name: /^parse error$/i })).toBeInTheDocument(); |
104 | 128 | expect(screen.getByText(/invalid xml structure/i)).toBeInTheDocument(); |
105 | 129 | }); |
106 | 130 |
|
| 131 | + it('switching to Interlinearization shows converted model JSON', () => { |
| 132 | + render(<InterlinearizerWebView {...testWebViewProps} />); |
| 133 | + |
| 134 | + fireEvent.click(screen.getByRole('button', { name: /^interlinearization$/i })); |
| 135 | + |
| 136 | + expect(screen.getByText(/^Interlinearization \(JSON\):$/)).toBeInTheDocument(); |
| 137 | + expect(screen.getByText(/analysisLanguages/i)).toBeInTheDocument(); |
| 138 | + expect(screen.getByText(/sourceWritingSystem/i)).toBeInTheDocument(); |
| 139 | + expect(screen.getByText(/segments/i)).toBeInTheDocument(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('switching back to InterlinearData shows PT9 structure JSON', () => { |
| 143 | + render(<InterlinearizerWebView {...testWebViewProps} />); |
| 144 | + |
| 145 | + fireEvent.click(screen.getByRole('button', { name: /^interlinearization$/i })); |
| 146 | + expect(screen.getByText(/^Interlinearization \(JSON\):$/)).toBeInTheDocument(); |
| 147 | + |
| 148 | + fireEvent.click(screen.getByRole('button', { name: /^interlineardata$/i })); |
| 149 | + |
| 150 | + expect(screen.getByText(/^InterlinearData \(JSON\):$/)).toBeInTheDocument(); |
| 151 | + expect(screen.getByText(/glossLanguage/i)).toBeInTheDocument(); |
| 152 | + expect(screen.getByText(/bookId/i)).toBeInTheDocument(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('renders empty JSON pre when jsonToShow is undefined (converter returns undefined)', () => { |
| 156 | + mockConvert.mockReturnValueOnce(undefined); |
| 157 | + |
| 158 | + const { container } = render(<InterlinearizerWebView {...testWebViewProps} />); |
| 159 | + fireEvent.click(screen.getByRole('button', { name: /^interlinearization$/i })); |
| 160 | + |
| 161 | + const jsonPre = container.querySelector('pre'); |
| 162 | + expect(jsonPre).toBeInTheDocument(); |
| 163 | + expect(jsonPre).toBeEmptyDOMElement(); |
| 164 | + expect(jsonPre).not.toHaveTextContent('undefined'); |
| 165 | + }); |
| 166 | + |
107 | 167 | it('displays parse error when parser throws non-Error (uses String(err))', () => { |
108 | | - const actual = jest.requireActual<typeof import('../parsers/interlinearXmlParser')>( |
109 | | - '../parsers/interlinearXmlParser', |
110 | | - ); |
111 | | - const realInstance = new actual.InterlinearXmlParser(); |
112 | | - const throwingParse = (): never => { |
| 168 | + mockParse.mockImplementationOnce(() => { |
113 | 169 | // Intentionally throw a non-Error to test the String(err) branch in the catch block. |
114 | 170 | // eslint-disable-next-line no-throw-literal -- testing non-Error handling |
115 | 171 | throw 'plain string error'; |
116 | | - }; |
117 | | - Object.defineProperty(realInstance, 'parse', { value: throwingParse, writable: true }); |
118 | | - jest.mocked(InterlinearXmlParser).mockImplementationOnce(() => realInstance); |
| 172 | + }); |
119 | 173 |
|
120 | 174 | render(<InterlinearizerWebView {...testWebViewProps} />); |
121 | 175 |
|
|
0 commit comments