|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { embedFilesInHtml, embedScript } from './transformers'; |
| 8 | + |
| 9 | +const parseHtml = html => new DOMParser().parseFromString(html, 'text/html'); |
| 10 | + |
| 11 | +describe('embedFilesInHtml', () => { |
| 12 | + it('keeps deferred script.js in place', async () => { |
| 13 | + const result = await embedFilesInHtml([ |
| 14 | + { |
| 15 | + fileKey: 'indexhtml', |
| 16 | + contents: |
| 17 | + '<!doctype html><html><head><script defer src="script.js"></script></head><body><main id="app"></main></body></html>' |
| 18 | + }, |
| 19 | + { |
| 20 | + fileKey: 'scriptjs', |
| 21 | + contents: 'window.app = document.querySelector("#app");' |
| 22 | + } |
| 23 | + ]); |
| 24 | + |
| 25 | + const doc = parseHtml(result); |
| 26 | + const script = doc.querySelector('script[data-src="script.js"]'); |
| 27 | + |
| 28 | + expect(script).toBeTruthy(); |
| 29 | + expect(script?.getAttribute('src')).toBeNull(); |
| 30 | + expect(script?.textContent).toContain( |
| 31 | + 'window.app = document.querySelector("#app");' |
| 32 | + ); |
| 33 | + expect(script?.parentElement?.tagName).toBe('HEAD'); |
| 34 | + expect(doc.body.lastElementChild?.id).toBe('app'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('keeps non-deferred script.js in place when embedding', async () => { |
| 38 | + const result = await embedFilesInHtml([ |
| 39 | + { |
| 40 | + fileKey: 'indexhtml', |
| 41 | + contents: |
| 42 | + '<!doctype html><html><head><script src="script.js"></script></head><body><main id="app"></main></body></html>' |
| 43 | + }, |
| 44 | + { |
| 45 | + fileKey: 'scriptjs', |
| 46 | + contents: 'window.app = document.querySelector("#app");' |
| 47 | + } |
| 48 | + ]); |
| 49 | + |
| 50 | + const doc = parseHtml(result); |
| 51 | + const script = doc.querySelector('script[data-src="script.js"]'); |
| 52 | + |
| 53 | + expect(script).toBeTruthy(); |
| 54 | + expect(script?.getAttribute('src')).toBeNull(); |
| 55 | + expect(script?.parentElement?.tagName).toBe('HEAD'); |
| 56 | + expect(doc.body.lastElementChild?.id).toBe('app'); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('embedScript', () => { |
| 61 | + const rawScript = 'console.log("Hello, world!");'; |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + delete document.__hasRun; |
| 65 | + document.body.querySelectorAll('script').forEach(s => s.remove()); |
| 66 | + vi.restoreAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('runs deferred scripts when the readystate becomes interactive', async () => { |
| 70 | + const script = document.createElement('script'); |
| 71 | + script.setAttribute('defer', true); |
| 72 | + embedScript(script, 'script.js', 'document.__hasRun = true;'); |
| 73 | + |
| 74 | + // By default, the jsdom environment is "complete", so we need to mock it to |
| 75 | + // test the defer behavior. |
| 76 | + vi.spyOn(document, 'readyState', 'get').mockReturnValueOnce('interactive'); |
| 77 | + // We have to wait for something to happen inside the script. Since we |
| 78 | + // dispatch this event, that is something we can wait for. |
| 79 | + const scriptRan = new Promise(resolve => |
| 80 | + document.addEventListener('readystatechange', resolve, { |
| 81 | + once: true |
| 82 | + }) |
| 83 | + ); |
| 84 | + |
| 85 | + document.body.appendChild(script); |
| 86 | + document.dispatchEvent(new Event('readystatechange')); |
| 87 | + |
| 88 | + await scriptRan; |
| 89 | + expect(document.__hasRun).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('embeds script content into a script tag', () => { |
| 93 | + const script = document.createElement('script'); |
| 94 | + embedScript(script, 'script.js', rawScript); |
| 95 | + |
| 96 | + expect(script.getAttribute('src')).toBeNull(); |
| 97 | + expect(script.textContent).toEqual(rawScript); |
| 98 | + }); |
| 99 | + |
| 100 | + it('embeds defered scripts content', () => { |
| 101 | + const script = document.createElement('script'); |
| 102 | + script.setAttribute('defer', true); |
| 103 | + embedScript(script, 'script.js', rawScript); |
| 104 | + |
| 105 | + expect(script.getAttribute('defer')).toBe('true'); |
| 106 | + expect(script.getAttribute('src')).toBeNull(); |
| 107 | + expect(script.textContent).toContain(rawScript); |
| 108 | + }); |
| 109 | +}); |
0 commit comments