|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { modulePreloadLinkRE } from '../index' |
| 3 | + |
| 4 | +describe('modulePreloadLinkRE', () => { |
| 5 | + const matches: Array<[string, string]> = [ |
| 6 | + ['rel first', '<link rel="modulepreload" crossorigin href="/assets/x.js">'], |
| 7 | + [ |
| 8 | + 'rel after other attributes', |
| 9 | + '<link href="/assets/x.js" rel="modulepreload">', |
| 10 | + ], |
| 11 | + ['rel only', '<link rel="modulepreload">'], |
| 12 | + ['self-closing', '<link rel="modulepreload"/>'], |
| 13 | + ['self-closing with space', '<link rel="modulepreload" />'], |
| 14 | + ['single quotes', "<link rel='modulepreload' href='/assets/x.js'>"], |
| 15 | + [ |
| 16 | + 'attributes across multiple lines', |
| 17 | + '<link\n rel="modulepreload"\n href="/assets/x.js"\n>', |
| 18 | + ], |
| 19 | + ] |
| 20 | + |
| 21 | + for (const [name, html] of matches) { |
| 22 | + test(`matches: ${name}`, () => { |
| 23 | + expect(html.replace(modulePreloadLinkRE, '')).toBe('') |
| 24 | + }) |
| 25 | + } |
| 26 | + |
| 27 | + const nonMatches: Array<[string, string]> = [ |
| 28 | + ['tag name with suffix', '<linkfoo rel="modulepreload">'], |
| 29 | + ['custom element with hyphen', '<link-preview rel="modulepreload">'], |
| 30 | + ['bare link tag', '<link>'], |
| 31 | + ['stylesheet link', '<link rel="stylesheet" href="/assets/x.css">'], |
| 32 | + ['preload (not modulepreload)', '<link rel="preload" href="/assets/x.js">'], |
| 33 | + [ |
| 34 | + 'attribute name ending in rel', |
| 35 | + '<link xrel="modulepreload" href="/assets/x.js">', |
| 36 | + ], |
| 37 | + ['mismatched quotes', `<link rel="modulepreload'>`], |
| 38 | + ] |
| 39 | + |
| 40 | + for (const [name, html] of nonMatches) { |
| 41 | + test(`does not match: ${name}`, () => { |
| 42 | + expect(html.replace(modulePreloadLinkRE, '')).toBe(html) |
| 43 | + }) |
| 44 | + } |
| 45 | +}) |
0 commit comments