@@ -8,13 +8,13 @@ const puppeteerCore = require('puppeteer-core')
88const extractCss = require ( '..' )
99
1010let server
11- const expected = readFileSync ( resolve ( __dirname , 'fixture.css' ) , 'utf8' )
11+ const fixture = readFileSync ( resolve ( __dirname , 'fixture.css' ) , 'utf8' )
1212
1313test . before ( async ( ) => {
1414 server = await createTestServer ( )
1515
1616 server . get ( '/fixture.css' , ( req , res ) => {
17- res . send ( expected )
17+ res . send ( fixture )
1818 } )
1919} )
2020
@@ -27,11 +27,16 @@ test('it fetches css from a page with CSS in a server generated <link> inside th
2727 server . get ( url , ( req , res ) => {
2828 res . send ( `
2929 <!doctype html>
30- <link rel="stylesheet" href="fixture.css" />
30+ <html>
31+ <head>
32+ <link rel="stylesheet" href="fixture.css" />
33+ </head>
34+ </html>
3135 ` )
3236 } )
3337
3438 const actual = await extractCss ( server . url + url )
39+ const expected = fixture
3540
3641 t . is ( actual , expected )
3742} )
@@ -41,13 +46,14 @@ test('it fetches css from a page with CSS in server generated <style> inside the
4146 server . get ( url , ( req , res ) => {
4247 res . send ( `
4348 <!doctype html>
44- <style>${ expected . trim ( ) } </style>
49+ <style>${ fixture } </style>
4550 ` )
4651 } )
4752
4853 const actual = await extractCss ( server . url + url )
54+ const expected = 'body { color: teal; }'
4955
50- t . is ( actual , expected . trim ( ) )
56+ t . is ( actual , expected )
5157} )
5258
5359test ( 'it finds JS generated <link /> CSS' , async t => {
@@ -62,6 +68,7 @@ test('it finds JS generated <link /> CSS', async t => {
6268 } )
6369
6470 const actual = await extractCss ( server . url + path )
71+ const expected = fixture
6572
6673 t . is ( actual , expected )
6774} )
@@ -77,7 +84,26 @@ test('it finds JS generated <style /> CSS', async t => {
7784 } )
7885
7986 const actual = await extractCss ( server . url + url , { waitUntil : 'load' } )
80- const expected = `body { color: teal; }`
87+ const expected = 'body { color: teal; }'
88+
89+ t . is ( actual , expected )
90+ } )
91+
92+ test ( 'it finds css-in-js, like Styled Components' , async t => {
93+ const url = '/css-in-js'
94+ const cssInJsExampleHtml = readFileSync (
95+ resolve ( __dirname , 'css-in-js.html' ) ,
96+ 'utf8'
97+ )
98+ server . get ( url , ( req , res ) => {
99+ res . send ( cssInJsExampleHtml )
100+ } )
101+
102+ const actual = await extractCss ( server . url + url , { waitUntil : 'load' } )
103+ // Color is RGB instead of Hex, because of serialization:
104+ // https://www.w3.org/TR/cssom-1/#serializing-css-values
105+ const expected =
106+ 'html { color: rgb(255, 0, 0); }.hJHBhT { color: blue; font-family: sans-serif; font-size: 3em; }'
81107
82108 t . is ( actual , expected )
83109} )
@@ -94,9 +120,11 @@ test('it combines server generated <link> and <style> tags with client side crea
94120
95121 const actual = await extractCss ( server . url + path )
96122
123+ t . true ( actual . includes ( 'content: "js-style";' ) )
124+ t . true ( actual . includes ( 'content: "server-style";' ) )
125+ t . true ( actual . includes ( `body {` ) )
126+ t . true ( actual . includes ( `color: teal;` ) )
97127 t . snapshot ( actual )
98- t . true ( actual . includes ( 'counter-increment: 2;' ) )
99- t . true ( actual . includes ( 'counter-increment: 3;' ) )
100128} )
101129
102130test ( 'it rejects if the url has an HTTP error status' , async t => {
@@ -116,20 +144,17 @@ test('it accepts a browser override for usage with other browsers', async t => {
116144 res . send ( `
117145 <!doctype html>
118146 <style>
119- body::before {
120- content: ${ req . headers [ 'user-agent' ] } ;
121- }
147+ body::before { content: "${ req . headers [ 'user-agent' ] } "; }
122148 </style>
123149 ` )
124150 } )
125151 const customBrowser = await puppeteerCore . launch ( {
126152 executablePath : chromium . path ,
127- args : [ " --user-agent=' Extract CSS Core'" ]
153+ args : [ ' --user-agent=Extract CSS Core']
128154 } )
129155 const actual = await extractCss ( server . url + path , { customBrowser} )
130156
131- t . snapshot ( actual )
132- t . true ( actual . includes ( "content: 'Extract CSS Core';" ) )
157+ t . is ( actual , 'body::before { content: "Extract CSS Core"; }' )
133158} )
134159
135160test ( 'it rejects on an invalid customBrowser option' , async t => {
0 commit comments