@@ -8,121 +8,60 @@ const extractCss = require('..')
88let server
99const fixture = readFileSync ( resolve ( __dirname , 'fixture.css' ) , 'utf8' )
1010
11+ function staticFile ( req , res ) {
12+ const fileContents = readFileSync ( resolve ( __dirname , req . path . slice ( 1 ) ) , 'utf8' )
13+ res . send ( fileContents )
14+ }
15+
1116test . before ( async ( ) => {
1217 server = await createTestServer ( )
1318
14- server . get ( '/fixture.css' , ( req , res ) => {
15- res . send ( fixture )
16- } )
19+ server . get ( '/fixture.css' , staticFile )
1720} )
1821
1922test . after ( async ( ) => {
2023 await server . close ( )
2124} )
2225
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' )
3729 const expected = fixture
38-
3930 t . is ( actual , expected )
4031} )
4132
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' )
6936 const expected = fixture
70-
7137 t . is ( actual , expected )
7238} )
7339
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; }'
8744 t . is ( actual , expected )
8845} )
8946
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; }'
10351 t . is ( actual , expected )
10452} )
10553
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 )
12359} )
12460
12561test ( '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+ } )
12665 const urlWith404 = server . url + '/404-page'
12766 await t . throwsAsync ( extractCss ( urlWith404 ) , {
12867 message : `There was an error retrieving CSS from ${ urlWith404 } .\n\tHTTP status code: 404 (Not Found)`
0 commit comments