| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2024 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import {readFileSync} from 'node:fs'; |
| 8 |
import {suite} from 'uvu'; |
| 9 |
// eslint-disable-next-line import/extensions |
| 10 |
import * as assert from 'uvu/assert'; |
| 11 |
import '@lit-labs/ssr-dom-shim'; |
| 12 |
|
| 13 |
// The CSS file is not copied/transpiled to the test directory. |
| 14 |
import example from '../src/test/example.css' with {type: 'css'}; |
| 15 |
|
| 16 |
const test = suite('CSSStyleSheet hook'); |
| 17 |
|
| 18 |
const textFromStyleSheet = (sheet: CSSStyleSheet) => { |
| 19 |
return Array.from({length: sheet.cssRules.length}) |
| 20 |
.map((_, i) => sheet.cssRules.item(i)?.cssText ?? '') |
| 21 |
.join('\n'); |
| 22 |
}; |
| 23 |
const rule = readFileSync( |
| 24 |
// The CSS file is not copied/transpiled to the test directory. |
| 25 |
new URL('../src/test/example.css', import.meta.url), |
| 26 |
'utf8' |
| 27 |
); |
| 28 |
|
| 29 |
test('load CSS file via dynamic import', async () => { |
| 30 |
// The CSS file is not copied/transpiled to the test directory. |
| 31 |
const sheet = await import('../src/test/example.css', { |
| 32 |
with: {type: 'css'}, |
| 33 |
}); |
| 34 |
|
| 35 |
assert.equal( |
| 36 |
textFromStyleSheet(sheet.default), |
| 37 |
rule, |
| 38 |
'Expected sheet to contain rule from referenced CSS file' |
| 39 |
); |
| 40 |
}); |
| 41 |
|
| 42 |
test('load CSS file via static import', async () => { |
| 43 |
assert.equal( |
| 44 |
textFromStyleSheet(example), |
| 45 |
rule, |
| 46 |
'Expected sheet to contain rule from referenced CSS file' |
| 47 |
); |
| 48 |
}); |
| 49 |
|
| 50 |
test('load CSS file with special characters', async () => { |
| 51 |
// The CSS file is not copied/transpiled to the test directory. |
| 52 |
const sheet = await import( |
| 53 |
'../src/test/example-with-special-characters.css', |
| 54 |
{ |
| 55 |
with: {type: 'css'}, |
| 56 |
} |
| 57 |
); |
| 58 |
const rule = readFileSync( |
| 59 |
new URL('../src/test/example-with-special-characters.css', import.meta.url), |
| 60 |
'utf8' |
| 61 |
); |
| 62 |
|
| 63 |
assert.equal( |
| 64 |
textFromStyleSheet(sheet.default), |
| 65 |
rule, |
| 66 |
'Expected sheet to contain rule from referenced CSS file' |
| 67 |
); |
| 68 |
}); |
| 69 |
|
| 70 |
test('should fail without import attributes', async () => { |
| 71 |
try { |
| 72 |
// The CSS file is not copied/transpiled to the test directory. |
| 73 |
await import('../src/test/example.css'); |
| 74 |
assert.unreachable( |
| 75 |
'The import without import attributes should have thrown' |
| 76 |
); |
| 77 |
} catch (e) { |
| 78 |
assert.ok('Threw as expected'); |
| 79 |
} |
| 80 |
}); |
| 81 |
|
| 82 |
test.run(); |