|
1 | 1 | import { jest } from '@jest/globals'; |
| 2 | +import SAX from 'sax'; |
2 | 3 | import { parseSvg } from './parser.js'; |
3 | 4 | import { stringifySvg } from './stringifier.js'; |
4 | 5 |
|
@@ -33,53 +34,39 @@ test('a text preserved', () => { |
33 | 34 | }); |
34 | 35 |
|
35 | 36 | describe('maxEntityCount', () => { |
36 | | - /** |
37 | | - * Re-import parseSvg with a mocked sax that records the options it receives. |
38 | | - * |
39 | | - * @returns {Promise<{ parseSvg: typeof parseSvg, getOpt: () => any }>} |
40 | | - */ |
41 | | - const importWithSaxSpy = async () => { |
42 | | - /** @type {any} */ |
43 | | - let receivedOpt; |
44 | | - jest.resetModules(); |
45 | | - jest.unstable_mockModule('sax', () => ({ |
46 | | - default: { |
47 | | - /** |
48 | | - * @param {boolean} _strict |
49 | | - * @param {any} opt |
50 | | - */ |
51 | | - parser: (_strict, opt) => { |
52 | | - receivedOpt = opt; |
53 | | - return { |
54 | | - ENTITIES: {}, |
55 | | - write() { |
56 | | - return this; |
57 | | - }, |
58 | | - close() { |
59 | | - return this; |
60 | | - }, |
61 | | - }; |
| 37 | + // sax mutates the options object it receives (it stamps a default |
| 38 | + // `maxEntityCount`), so snapshot the options at call time, before sax runs. |
| 39 | + /** @type {any} */ |
| 40 | + let receivedOpt; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + jest.spyOn(SAX, 'parser').mockImplementation((_strict, opt) => { |
| 44 | + receivedOpt = { ...opt }; |
| 45 | + // Minimal stub: parseSvg only assigns handlers then calls write().close(). |
| 46 | + return /** @type {any} */ ({ |
| 47 | + ENTITIES: {}, |
| 48 | + write() { |
| 49 | + return this; |
| 50 | + }, |
| 51 | + close() { |
| 52 | + return this; |
62 | 53 | }, |
63 | | - }, |
64 | | - })); |
65 | | - const mod = await import('./parser.js'); |
66 | | - return { parseSvg: mod.parseSvg, getOpt: () => receivedOpt }; |
67 | | - }; |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
68 | 57 |
|
69 | 58 | afterEach(() => { |
70 | | - jest.dontMock('sax'); |
71 | | - jest.resetModules(); |
| 59 | + jest.restoreAllMocks(); |
| 60 | + receivedOpt = undefined; |
72 | 61 | }); |
73 | 62 |
|
74 | | - test('forwards a provided maxEntityCount to the sax parser', async () => { |
75 | | - const { parseSvg: parse, getOpt } = await importWithSaxSpy(); |
76 | | - parse('<svg xmlns="http://www.w3.org/2000/svg"/>', undefined, 1234); |
77 | | - expect(getOpt().maxEntityCount).toBe(1234); |
| 63 | + test('forwards a provided maxEntityCount to the parser options', () => { |
| 64 | + parseSvg('<svg xmlns="http://www.w3.org/2000/svg"/>', undefined, 1234); |
| 65 | + expect(receivedOpt.maxEntityCount).toBe(1234); |
78 | 66 | }); |
79 | 67 |
|
80 | | - test('does not pass maxEntityCount when it is omitted', async () => { |
81 | | - const { parseSvg: parse, getOpt } = await importWithSaxSpy(); |
82 | | - parse('<svg xmlns="http://www.w3.org/2000/svg"/>'); |
83 | | - expect(getOpt().maxEntityCount).toBeUndefined(); |
| 68 | + test('does not set maxEntityCount when it is omitted', () => { |
| 69 | + parseSvg('<svg xmlns="http://www.w3.org/2000/svg"/>'); |
| 70 | + expect(receivedOpt.maxEntityCount).toBeUndefined(); |
84 | 71 | }); |
85 | 72 | }); |
0 commit comments