@@ -8,121 +8,60 @@ const extractCss = require('..')
8
8
let server
9
9
const fixture = readFileSync ( resolve ( __dirname , 'fixture.css' ) , 'utf8' )
10
10
11
+ function staticFile ( req , res ) {
12
+ const fileContents = readFileSync ( resolve ( __dirname , req . path . slice ( 1 ) ) , 'utf8' )
13
+ res . send ( fileContents )
14
+ }
15
+
11
16
test . before ( async ( ) => {
12
17
server = await createTestServer ( )
13
18
14
- server . get ( '/fixture.css' , ( req , res ) => {
15
- res . send ( fixture )
16
- } )
19
+ server . get ( '/fixture.css' , staticFile )
17
20
} )
18
21
19
22
test . after ( async ( ) => {
20
23
await server . close ( )
21
24
} )
22
25
23
- test ( 'it fetches css from a page with CSS in a server generated <link> inside the <head>' , async t => {
24
- const url = '/server-link-head'
25
- server . get ( url , ( req , res ) => {
26
- res . send ( `
27
- <!doctype html>
28
- <html>
29
- <head>
30
- <link rel="stylesheet" href="fixture.css" />
31
- </head>
32
- </html>
33
- ` )
34
- } )
35
-
36
- const actual = await extractCss ( server . url + url )
26
+ test ( 'it finds css in a <link> tag - HTML' , async t => {
27
+ server . get ( '/link-tag-html.html' , staticFile )
28
+ const actual = await extractCss ( server . url + '/link-tag-html.html' )
37
29
const expected = fixture
38
-
39
30
t . is ( actual , expected )
40
31
} )
41
32
42
- test ( 'it fetches css from a page with CSS in server generated <style> inside the <head>' , async t => {
43
- const url = '/server-style-head'
44
- server . get ( url , ( req , res ) => {
45
- res . send ( `
46
- <!doctype html>
47
- <style>${ fixture } </style>
48
- ` )
49
- } )
50
-
51
- const actual = await extractCss ( server . url + url )
52
- const expected = 'body { color: teal; }'
53
-
54
- t . is ( actual , expected )
55
- } )
56
-
57
- test ( 'it finds JS generated <link /> CSS' , async t => {
58
- const path = '/js-generated-link'
59
- const cssInJsExampleHtml = readFileSync (
60
- resolve ( __dirname , 'js-create-link-element.html' ) ,
61
- 'utf8'
62
- )
63
-
64
- server . get ( path , ( req , res ) => {
65
- res . send ( cssInJsExampleHtml )
66
- } )
67
-
68
- const actual = await extractCss ( server . url + path )
33
+ test ( 'it finds css in a <link> tag - JS' , async t => {
34
+ server . get ( '/link-tag-js.html' , staticFile )
35
+ const actual = await extractCss ( server . url + '/link-tag-js.html' )
69
36
const expected = fixture
70
-
71
37
t . is ( actual , expected )
72
38
} )
73
39
74
- test ( 'it finds JS generated <style /> CSS' , async t => {
75
- const url = '/js-generated-js-style-tag'
76
- const cssInJsExampleHtml = readFileSync (
77
- resolve ( __dirname , 'js-create-style-element.html' ) ,
78
- 'utf8'
79
- )
80
- server . get ( url , ( req , res ) => {
81
- res . send ( cssInJsExampleHtml )
82
- } )
83
-
84
- const actual = await extractCss ( server . url + url , { waitUntil : 'load' } )
85
- const expected = 'body { color: teal; }'
86
-
40
+ test ( 'it finds css in a <style> tag - HTML' , async t => {
41
+ server . get ( '/style-tag-html.html' , staticFile )
42
+ const actual = await extractCss ( server . url + '/style-tag-html.html' )
43
+ const expected = '.fixture { color: red; }'
87
44
t . is ( actual , expected )
88
45
} )
89
46
90
- test ( 'it finds css-in-js, like Styled Components' , async t => {
91
- const url = '/css-in-js'
92
- const cssInJsExampleHtml = readFileSync (
93
- resolve ( __dirname , 'css-in-js.html' ) ,
94
- 'utf8'
95
- )
96
- server . get ( url , ( req , res ) => {
97
- res . send ( cssInJsExampleHtml )
98
- } )
99
-
100
- const actual = await extractCss ( server . url + url , { waitUntil : 'load' } )
101
- const expected = 'body { color: red; }.bcMPWx { color: blue; }'
102
-
47
+ test ( 'it finds css in a <style> tag - JS' , async t => {
48
+ server . get ( '/style-tag-js.html' , staticFile )
49
+ const actual = await extractCss ( server . url + '/style-tag-js.html' )
50
+ const expected = '.fixture { color: red; }'
103
51
t . is ( actual , expected )
104
52
} )
105
53
106
- test ( 'it combines server generated <link> and <style> tags with client side created <link> and <style> tags' , async t => {
107
- const path = '/kitchen-sink'
108
- const kitchenSinkExample = readFileSync (
109
- resolve ( __dirname , 'kitchen-sink.html' ) ,
110
- 'utf8'
111
- )
112
- server . get ( path , ( req , res ) => {
113
- res . send ( kitchenSinkExample )
114
- } )
115
-
116
- const actual = await extractCss ( server . url + path )
117
-
118
- t . true ( actual . includes ( 'content: "js-style";' ) )
119
- t . true ( actual . includes ( 'content: "server-style";' ) )
120
- t . true ( actual . includes ( 'body {' ) )
121
- t . true ( actual . includes ( 'color: teal;' ) )
122
- t . snapshot ( actual )
54
+ test ( 'it finds css-in-js' , async t => {
55
+ server . get ( '/css-in-js.html' , staticFile )
56
+ const actual = await extractCss ( server . url + '/css-in-js.html' )
57
+ const expected = '.bcMPWx { color: blue; }'
58
+ t . is ( actual , expected )
123
59
} )
124
60
125
61
test ( 'it rejects if the url has an HTTP error status' , async t => {
62
+ server . get ( '/404-page' , ( req , res ) => {
63
+ res . status ( 404 ) . send ( )
64
+ } )
126
65
const urlWith404 = server . url + '/404-page'
127
66
await t . throwsAsync ( extractCss ( urlWith404 ) , {
128
67
message : `There was an error retrieving CSS from ${ urlWith404 } .\n\tHTTP status code: 404 (Not Found)`
0 commit comments