Skip to content

Commit d7d3b8a

Browse files
test(html): cover the deferred JSON body a renderer answers for
1 parent 848e470 commit d7d3b8a

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

‎test/HtmlSyntax.unittest.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4291,6 +4291,49 @@ describe("SourceProcessor — inline <script> whitespace", () => {
42914291
expect(out).toBe('<script type=application/json>{ "a" : 1 }</script>');
42924292
});
42934293

4294+
/**
4295+
* Print with a renderer that answers asynchronously, as a caller reaching a
4296+
* worker pool does.
4297+
* @param {string} html input markup
4298+
* @param {(source: string) => string | undefined} answer what the renderer makes of one body
4299+
* @returns {Promise<{ code: string, offered: string[] }>} the output and each body offered
4300+
*/
4301+
const deferredBody = async (html, answer) => {
4302+
/** @type {string[]} */
4303+
const offered = [];
4304+
const { code } = await new SourceProcessor().processAsync(html, {
4305+
mode: /** @type {"minify"} */ ("minify"),
4306+
collapseWhitespace: /** @type {"all"} */ ("all"),
4307+
renderEmbeddedSource: (/** @type {string} */ source) => {
4308+
offered.push(source);
4309+
return Promise.resolve(answer(source));
4310+
}
4311+
});
4312+
return { code, offered };
4313+
};
4314+
4315+
it("offers a deferred JSON body trimmed", async () => {
4316+
const { code, offered } = await deferredBody(
4317+
'<script type="application/ld+json"> { "a" : 1 } </script>',
4318+
() => '{"answered":1}'
4319+
);
4320+
4321+
expect(offered).toEqual(['{ "a" : 1 }']);
4322+
expect(code).toBe(
4323+
'<script type=application/ld+json>{"answered":1}</script>'
4324+
);
4325+
});
4326+
4327+
it("strips a deferred JSON body the renderer declines", async () => {
4328+
// The built-in stands in, and it reads the same trimmed body.
4329+
const { code } = await deferredBody(
4330+
'<script type="application/ld+json"> { "a" : 1 } </script>',
4331+
() => undefined
4332+
);
4333+
4334+
expect(code).toBe('<script type=application/ld+json>{"a":1}</script>');
4335+
});
4336+
42944337
it("keeps a JSON body that is only whitespace", () => {
42954338
expect(minifyBody("application/json", " ")).toBe(" ");
42964339
});

0 commit comments

Comments
 (0)