From 384f376b33f4820f6798c627124a6b0f6bb51962 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Sun, 16 Feb 2020 20:59:27 +0100 Subject: [PATCH 1/2] improve test file structure --- test/css-in-js.html | 5 -- test/fixture.css | 4 +- test/index.js | 118 ++++++++---------------------- test/inline-style-html.html | 11 +++ test/inline-style-js.html | 24 ++++++ test/js-create-link-element.html | 14 ---- test/js-create-style-element.html | 13 ---- test/kitchen-sink.html | 31 -------- test/link-tag-html.html | 12 +++ test/link-tag-js.html | 19 +++++ test/style-tag-html.html | 14 ++++ test/style-tag-js.html | 16 ++++ 12 files changed, 127 insertions(+), 154 deletions(-) create mode 100644 test/inline-style-html.html create mode 100644 test/inline-style-js.html delete mode 100644 test/js-create-link-element.html delete mode 100644 test/js-create-style-element.html delete mode 100644 test/kitchen-sink.html create mode 100644 test/link-tag-html.html create mode 100644 test/link-tag-js.html create mode 100644 test/style-tag-html.html create mode 100644 test/style-tag-js.html diff --git a/test/css-in-js.html b/test/css-in-js.html index 2cdf011..c6a1c19 100644 --- a/test/css-in-js.html +++ b/test/css-in-js.html @@ -7,11 +7,6 @@ content="width=device-width,initial-scale=1,shrink-to-fit=no" /> React App - diff --git a/test/fixture.css b/test/fixture.css index 1fa6a8f..7c88d8e 100644 --- a/test/fixture.css +++ b/test/fixture.css @@ -1,3 +1 @@ -body { - color: teal; -} +.fixture { color: red; } diff --git a/test/index.js b/test/index.js index a5edb31..86fdd7d 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,6 @@ const test = require('ava') const createTestServer = require('create-test-server') -const {readFileSync} = require('fs') +const {readFileSync, existsSync} = require('fs') const {resolve} = require('path') const extractCss = require('..') @@ -10,119 +10,61 @@ const fixture = readFileSync(resolve(__dirname, 'fixture.css'), 'utf8') test.before(async () => { server = await createTestServer() - server.get('/fixture.css', (req, res) => { res.send(fixture) }) + function staticFile(req, res) { + const fileContents = readFileSync(resolve(__dirname, req.path.slice(1)), 'utf8') + res.send(fileContents) + } + + server.get('/css-in-js.html', staticFile) + server.get('/inline-style-html.html', staticFile) + server.get('/inline-style-js.html', staticFile) + server.get('/link-tag-html.html', staticFile) + server.get('/link-tag-js.html', staticFile) + server.get('/style-tag-html.html', staticFile) + server.get('/style-tag-js.html', staticFile) }) test.after(async () => { await server.close() }) -test('it fetches css from a page with CSS in a server generated inside the ', async t => { - const url = '/server-link-head' - server.get(url, (req, res) => { - res.send(` - - - - - - - `) - }) - - const actual = await extractCss(server.url + url) +test('it finds css in a tag - HTML', async t => { + const actual = await extractCss(server.url + '/link-tag-html.html') const expected = fixture - t.is(actual, expected) }) -test('it fetches css from a page with CSS in server generated - `) - }) - - const actual = await extractCss(server.url + url) - const expected = 'body { color: teal; }' - - t.is(actual, expected) -}) - -test('it finds JS generated CSS', async t => { - const path = '/js-generated-link' - const cssInJsExampleHtml = readFileSync( - resolve(__dirname, 'js-create-link-element.html'), - 'utf8' - ) - - server.get(path, (req, res) => { - res.send(cssInJsExampleHtml) - }) - - const actual = await extractCss(server.url + path) +test('it finds css in a tag - JS', async t => { + const actual = await extractCss(server.url + '/link-tag-js.html') const expected = fixture - t.is(actual, expected) }) -test('it finds JS generated - -

Title

-
server-style:
- - diff --git a/test/link-tag-html.html b/test/link-tag-html.html new file mode 100644 index 0000000..7f5fb70 --- /dev/null +++ b/test/link-tag-html.html @@ -0,0 +1,12 @@ + + + + + + Document + + + +

<link> tag in HTML

+ + \ No newline at end of file diff --git a/test/link-tag-js.html b/test/link-tag-js.html new file mode 100644 index 0000000..1170980 --- /dev/null +++ b/test/link-tag-js.html @@ -0,0 +1,19 @@ + + + + + + + Document + + + +

<link> tag in JS

+ + + \ No newline at end of file diff --git a/test/style-tag-html.html b/test/style-tag-html.html new file mode 100644 index 0000000..e3a446b --- /dev/null +++ b/test/style-tag-html.html @@ -0,0 +1,14 @@ + + + + + + Document + + + +

<style> tag in HTML

+ + \ No newline at end of file diff --git a/test/style-tag-js.html b/test/style-tag-js.html new file mode 100644 index 0000000..ce161b2 --- /dev/null +++ b/test/style-tag-js.html @@ -0,0 +1,16 @@ + + + + + + Document + + +

<style> tag in JS

+ + + \ No newline at end of file From 6c7ceeb9e95ab7187da74b9c8282984c1757f1f3 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Sun, 16 Feb 2020 21:03:57 +0100 Subject: [PATCH 2/2] improve test file structure --- test/index.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/test/index.js b/test/index.js index 86fdd7d..462062d 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,6 @@ const test = require('ava') const createTestServer = require('create-test-server') -const {readFileSync, existsSync} = require('fs') +const {readFileSync} = require('fs') const {resolve} = require('path') const extractCss = require('..') @@ -8,23 +8,15 @@ const extractCss = require('..') let server const fixture = readFileSync(resolve(__dirname, 'fixture.css'), 'utf8') +function staticFile(req, res) { + const fileContents = readFileSync(resolve(__dirname, req.path.slice(1)), 'utf8') + res.send(fileContents) +} + test.before(async () => { server = await createTestServer() - server.get('/fixture.css', (req, res) => { - res.send(fixture) - }) - function staticFile(req, res) { - const fileContents = readFileSync(resolve(__dirname, req.path.slice(1)), 'utf8') - res.send(fileContents) - } - server.get('/css-in-js.html', staticFile) - server.get('/inline-style-html.html', staticFile) - server.get('/inline-style-js.html', staticFile) - server.get('/link-tag-html.html', staticFile) - server.get('/link-tag-js.html', staticFile) - server.get('/style-tag-html.html', staticFile) - server.get('/style-tag-js.html', staticFile) + server.get('/fixture.css', staticFile) }) test.after(async () => { @@ -32,30 +24,35 @@ test.after(async () => { }) test('it finds css in a tag - HTML', async t => { + server.get('/link-tag-html.html', staticFile) const actual = await extractCss(server.url + '/link-tag-html.html') const expected = fixture t.is(actual, expected) }) test('it finds css in a tag - JS', async t => { + server.get('/link-tag-js.html', staticFile) const actual = await extractCss(server.url + '/link-tag-js.html') const expected = fixture t.is(actual, expected) }) test('it finds css in a