@@ -6,58 +6,87 @@ const {resolve} = require('path')
6
6
const extractCss = require ( '..' )
7
7
8
8
let server
9
- const fixture = readFileSync ( resolve ( __dirname , 'fixture.css' ) , 'utf8' )
10
9
11
- function staticFile ( req , res ) {
10
+ function serveStatic ( req , res ) {
12
11
const fileContents = readFileSync ( resolve ( __dirname , req . path . slice ( 1 ) ) , 'utf8' )
13
12
res . send ( fileContents )
14
13
}
15
14
16
15
test . before ( async ( ) => {
17
16
server = await createTestServer ( )
18
17
19
- server . get ( '/fixture.css' , staticFile )
18
+ server . get ( '/fixture.css' , serveStatic )
19
+ server . get ( '/imported.css' , serveStatic )
20
20
} )
21
21
22
22
test . after ( async ( ) => {
23
23
await server . close ( )
24
24
} )
25
25
26
26
test ( 'it finds css in a <link> tag - HTML' , async t => {
27
- server . get ( '/link-tag-html.html' , staticFile )
27
+ // @TODO : during tests, it doesn't find the imported CSS file contents
28
+ // but it does work outside of test scope
29
+ server . get ( '/link-tag-html.html' , serveStatic )
28
30
const actual = await extractCss ( server . url + '/link-tag-html.html' )
29
- const expected = fixture
30
- t . is ( actual , expected )
31
+
32
+ t . true ( actual . includes ( '@import url("imported.css");' ) )
33
+ t . true ( actual . includes ( '.fixture { color: red; }' ) )
34
+ t . snapshot ( actual )
31
35
} )
32
36
33
37
test ( 'it finds css in a <link> tag - JS' , async t => {
34
- server . get ( '/link-tag-js.html' , staticFile )
38
+ // @TODO : during tests, it doesn't find the imported CSS file contents
39
+ // but it does work outside of test scope
40
+ server . get ( '/link-tag-js.html' , serveStatic )
35
41
const actual = await extractCss ( server . url + '/link-tag-js.html' )
36
- const expected = fixture
37
- t . is ( actual , expected )
42
+
43
+ t . true ( actual . includes ( '@import url("imported.css");' ) )
44
+ t . true ( actual . includes ( '.fixture { color: red; }' ) )
45
+ t . snapshot ( actual )
38
46
} )
39
47
40
48
test ( 'it finds css in a <style> tag - HTML' , async t => {
41
- server . get ( '/style-tag-html.html' , staticFile )
49
+ server . get ( '/style-tag-html.html' , serveStatic )
42
50
const actual = await extractCss ( server . url + '/style-tag-html.html' )
43
- const expected = '.fixture { color: red; }'
44
- t . is ( actual , expected )
51
+
52
+ t . true ( actual . includes ( '@import url("imported.css");' ) )
53
+ t . true ( actual . includes ( '.fixture { color: red; }' ) )
54
+ t . true ( actual . includes ( '.imported { color: blue; }' ) )
55
+ t . snapshot ( actual )
45
56
} )
46
57
47
58
test ( 'it finds css in a <style> tag - JS' , async t => {
48
- server . get ( '/style-tag-js.html' , staticFile )
59
+ server . get ( '/style-tag-js.html' , serveStatic )
49
60
const actual = await extractCss ( server . url + '/style-tag-js.html' )
50
- const expected = '.fixture { color: red; }'
51
- t . is ( actual , expected )
61
+
62
+ t . true ( actual . includes ( '@import url("imported.css");' ) )
63
+ t . true ( actual . includes ( '.fixture { color: red; }' ) )
64
+ t . true ( actual . includes ( '.imported { color: blue; }' ) )
65
+ t . snapshot ( actual )
52
66
} )
53
67
54
68
test ( 'it finds css-in-js' , async t => {
55
- server . get ( '/css-in-js.html' , staticFile )
69
+ server . get ( '/css-in-js.html' , serveStatic )
56
70
const actual = await extractCss ( server . url + '/css-in-js.html' )
57
71
const expected = '.bcMPWx { color: blue; }'
72
+
58
73
t . is ( actual , expected )
59
74
} )
60
75
76
+ test ( 'it does not report the same CSS twice' , async t => {
77
+ // @TODO : during tests, it doesn't find the imported CSS file contents
78
+ // but it does work outside of test scope
79
+ server . get ( '/kitchen-sink.html' , serveStatic )
80
+ const actual = await extractCss ( server . url + '/kitchen-sink.html' )
81
+
82
+ t . true ( actual . includes ( '@import url("imported.css");' ) )
83
+ t . true ( actual . includes ( '.fixture { color: red; }' ) )
84
+ t . true ( actual . includes ( '.style-tag-fixture-js { color: yellow; }' ) )
85
+ t . true ( actual . includes ( '.style-tag-fixture-html { color: green; }' ) )
86
+
87
+ t . snapshot ( actual )
88
+ } )
89
+
61
90
test ( 'it rejects if the url has an HTTP error status' , async t => {
62
91
server . get ( '/404-page' , ( req , res ) => {
63
92
res . status ( 404 ) . send ( )
0 commit comments