From 47e22f459015e3cba3ee66f87287b70dede9e494 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 6 Apr 2025 08:05:59 -0700 Subject: [PATCH] test: add unit tests for all preload strategies --- .../test/__snapshots__/beasties.test.ts.snap | 52 +++++++ packages/beasties/test/beasties.test.ts | 131 ++++++++++++++++-- 2 files changed, 171 insertions(+), 12 deletions(-) diff --git a/packages/beasties/test/__snapshots__/beasties.test.ts.snap b/packages/beasties/test/__snapshots__/beasties.test.ts.snap index 5fdcb16..adcca85 100644 --- a/packages/beasties/test/__snapshots__/beasties.test.ts.snap +++ b/packages/beasties/test/__snapshots__/beasties.test.ts.snap @@ -136,3 +136,55 @@ exports[`beasties > works with pseudo classes and elements 1`] = ` " `; + +exports[`preload modes > should handle "false" preload mode correctly 1`] = ` +" + + + + + +

Hello World!

+ + + " +`; + +exports[`preload modes > should use "js" preload mode correctly 1`] = ` +" + + + + + +

Hello World!

+ + + " +`; + +exports[`preload modes > should use "media" preload mode correctly 1`] = ` +" + + + + + +

Hello World!

+ + + " +`; + +exports[`preload modes > should use "swap" preload mode correctly 1`] = ` +" + + + + + +

Hello World!

+ + + " +`; diff --git a/packages/beasties/test/beasties.test.ts b/packages/beasties/test/beasties.test.ts index 489b7f6..1933e75 100644 --- a/packages/beasties/test/beasties.test.ts +++ b/packages/beasties/test/beasties.test.ts @@ -57,8 +57,8 @@ describe('beasties', () => { `) - expect(result).toMatch('') - expect(result).toMatch('') + expect(result).toContain('') + expect(result).toContain('') expect(result).toMatchSnapshot() }) @@ -111,9 +111,9 @@ describe('beasties', () => { `) - expect(result).toMatch('') - expect(result).toMatch('') - expect(result).toMatch('$title') + expect(result).toContain('') + expect(result).toContain('') + expect(result).toContain('$title') }) it('should keep existing link tag attributes in the noscript link', async () => { @@ -140,8 +140,8 @@ describe('beasties', () => { `) - expect(result).toMatch('') - expect(result).toMatch( + expect(result).toContain('') + expect(result).toContain( ``, ) expect(result).toMatchSnapshot() @@ -170,8 +170,8 @@ describe('beasties', () => { `) - expect(result).toMatch('') - expect(result).toMatch( + expect(result).toContain('') + expect(result).toContain( ``, ) expect(result).toMatchSnapshot() @@ -197,7 +197,7 @@ describe('beasties', () => { `) - expect(result).toMatch('<h1>Hello World!</h1>') + expect(result).toContain('<h1>Hello World!</h1>') }) it('prevent injection via media attr', async () => { @@ -294,8 +294,115 @@ describe('beasties', () => { `) expect(loggerWarnSpy).not.toHaveBeenCalled() - expect(result).toMatch('') - expect(result).toMatch('') + expect(result).toContain('') + expect(result).toContain('') + expect(result).toMatchSnapshot() + }) +}) + +describe('preload modes', () => { + it('should use "js" preload mode correctly', async () => { + const beasties = new Beasties({ + reduceInlineStyles: false, + path: '/', + preload: 'js', + }) + const assets: Record = { + '/style.css': 'h1 { color: blue; }', + } + beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]! + const result = await beasties.process(` + + + + + +

Hello World!

+ + + `) + expect(result).toContain('') + expect(result).toContain('') + expect(result).toContain(``) + expect(result).toMatchSnapshot() + }) + + it('should use "media" preload mode correctly', async () => { + const beasties = new Beasties({ + reduceInlineStyles: false, + path: '/', + preload: 'media', + }) + const assets: Record = { + '/style.css': 'h1 { color: blue; }', + } + beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]! + const result = await beasties.process(` + + + + + +

Hello World!

+ + + `) + expect(result).toContain('') + expect(result).toContain('') + expect(result).toContain('') + expect(result).toMatchSnapshot() + }) + + it('should use "swap" preload mode correctly', async () => { + const beasties = new Beasties({ + reduceInlineStyles: false, + path: '/', + preload: 'swap', + }) + const assets: Record = { + '/style.css': 'h1 { color: blue; }', + } + beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]! + const result = await beasties.process(` + + + + + +

Hello World!

+ + + `) + expect(result).toContain('') + expect(result).toContain('') + expect(result).toContain('') + expect(result).toMatchSnapshot() + }) + + it('should handle "false" preload mode correctly', async () => { + const beasties = new Beasties({ + reduceInlineStyles: false, + path: '/', + preload: false, + }) + const assets: Record = { + '/style.css': 'h1 { color: blue; }', + } + beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]! + const result = await beasties.process(` + + + + + +

Hello World!

+ + + `) + expect(result).toContain('') + expect(result).toContain('') + expect(result).not.toContain('onload=') + expect(result).not.toContain('