1
1
import { transformCSS } from '../../src/transform.js' ;
2
2
3
3
describe ( 'transformCSS' , ( ) => {
4
- beforeAll ( ( ) => {
5
- global . URL . createObjectURL = vi . fn ( ) . mockReturnValue ( '/updated.css' ) ;
6
- } ) ;
7
-
8
4
it ( 'parses and removes new anchor positioning CSS after transformation to JS' , async ( ) => {
9
5
document . head . innerHTML = `
10
6
<link type="text/css" href="/sample.css" data-link="true" crossorigin="anonymous" />
@@ -16,7 +12,7 @@ describe('transformCSS', () => {
16
12
<div id="div" data-has-inline-styles="key" style="--foo: var(--bar); color: red;" />
17
13
<div id="div2" data-has-inline-styles="key2" style="color: red;" />
18
14
` ;
19
- let link = document . querySelector ( 'link' ) as HTMLLinkElement ;
15
+ const link = document . querySelector ( 'link' ) as HTMLLinkElement ;
20
16
const style = document . querySelector ( 'style' ) as HTMLStyleElement ;
21
17
const div = document . getElementById ( 'div' ) as HTMLDivElement ;
22
18
const div2 = document . getElementById ( 'div2' ) as HTMLDivElement ;
@@ -36,14 +32,16 @@ describe('transformCSS', () => {
36
32
] ;
37
33
const inlineStyles = new Map ( ) ;
38
34
inlineStyles . set ( div , { '--foo' : '--bar' } ) ;
39
- const promise = transformCSS ( styleData , inlineStyles , true ) ;
40
- link = document . querySelector ( 'link' ) as HTMLLinkElement ;
41
- link . dispatchEvent ( new Event ( 'load' ) ) ;
42
- await promise ;
35
+ await transformCSS ( styleData , inlineStyles , true ) ;
36
+
37
+ expect ( link . isConnected ) . toBe ( false ) ;
38
+ const newLink = document . querySelector (
39
+ 'style[data-original-href]' ,
40
+ ) as HTMLStyleElement ;
41
+ expect ( newLink . getAttribute ( 'data-link' ) ) . toBe ( 'true' ) ;
42
+ expect ( newLink . getAttribute ( 'crossorigin' ) ) . toBeNull ( ) ;
43
+ expect ( newLink . textContent ) . toBe ( 'html { margin: 0; }' ) ;
43
44
44
- expect ( link . href ) . toContain ( '/updated.css' ) ;
45
- expect ( link . getAttribute ( 'data-link' ) ) . toBe ( 'true' ) ;
46
- expect ( link . getAttribute ( 'crossorigin' ) ) . toBeNull ( ) ;
47
45
expect ( style . innerHTML ) . toBe ( 'html { padding: 0; }' ) ;
48
46
expect ( div . getAttribute ( 'style' ) ) . toBe ( '--foo: var(--bar); color:blue;' ) ;
49
47
expect ( div2 . getAttribute ( 'style' ) ) . toBe ( 'color: red;' ) ;
@@ -55,17 +53,42 @@ describe('transformCSS', () => {
55
53
document . head . innerHTML = `
56
54
<link id="the-link" media="screen" title="stylish" rel="stylesheet" href="/sample.css"/>
57
55
` ;
58
- let link = document . querySelector ( 'link' ) as HTMLLinkElement ;
56
+ const link = document . querySelector ( 'link' ) as HTMLLinkElement ;
59
57
const styleData = [ { el : link , css : 'html { margin: 0; }' , changed : true } ] ;
60
58
const inlineStyles = new Map ( ) ;
61
- const promise = transformCSS ( styleData , inlineStyles , true ) ;
62
- link = document . querySelector ( 'link' ) as HTMLLinkElement ;
63
- link . dispatchEvent ( new Event ( 'load' ) ) ;
64
- await promise ;
59
+ const initialStyleElement = document . querySelector ( 'style' ) ;
60
+ expect ( initialStyleElement ) . toBe ( null ) ;
61
+ await transformCSS ( styleData , inlineStyles , true ) ;
62
+ const transformedStyleElement = document . querySelector (
63
+ 'style' ,
64
+ ) as HTMLStyleElement ;
65
+ expect ( transformedStyleElement . id ) . toBe ( 'the-link' ) ;
66
+ expect ( transformedStyleElement . media ) . toBe ( 'screen' ) ;
67
+ expect ( transformedStyleElement . title ) . toBe ( 'stylish' ) ;
68
+
69
+ const transformedLink = document . querySelector ( 'link' ) as HTMLLinkElement ;
70
+ expect ( transformedLink ) . toBe ( null ) ;
71
+ } ) ;
72
+
73
+ it ( 'creates new style elements for created styles' , async ( ) => {
74
+ document . head . innerHTML = `` ;
75
+ const styleData = [
76
+ {
77
+ el : document . createElement ( 'link' ) ,
78
+ css : 'html { margin: 0; }' ,
79
+ changed : true ,
80
+ created : true ,
81
+ } ,
82
+ ] ;
83
+ await transformCSS ( styleData , undefined , true ) ;
65
84
66
- expect ( link . href ) . toContain ( '/updated.css' ) ;
67
- expect ( link . id ) . toBe ( 'the-link' ) ;
68
- expect ( link . media ) . toBe ( 'screen' ) ;
69
- expect ( link . title ) . toBe ( 'stylish' ) ;
85
+ const createdStyleElement = document . querySelector (
86
+ 'style' ,
87
+ ) as HTMLStyleElement ;
88
+ expect ( createdStyleElement . hasAttribute ( 'data-original-href' ) ) . toBe ( false ) ;
89
+ expect ( createdStyleElement . hasAttribute ( 'data-generated-by-polyfill' ) ) . toBe (
90
+ true ,
91
+ ) ;
92
+ expect ( createdStyleElement . textContent ) . toBe ( 'html { margin: 0; }' ) ;
70
93
} ) ;
71
94
} ) ;
0 commit comments