|
1 | 1 | const fs = require('fs') |
2 | 2 | const path = require('path') |
3 | | -const nuke = require('../lib/nuke') |
| 3 | +const nukecss = require('../lib/nuke') |
4 | 4 |
|
5 | 5 | describe('nuke.js', function () { |
6 | | - const htmlContent = fs.readFileSync(path.join(__dirname, '/fixtures/content.html'), 'utf8') |
7 | | - const cssContent = fs.readFileSync(path.join(__dirname, '/fixtures/content.css'), 'utf8') |
8 | | - |
9 | | - it('should remove unused rules', function () { |
10 | | - const result = nuke(htmlContent, cssContent) |
11 | | - expect(result).to.not.contain('foobar[something=x]') |
12 | | - expect(result).to.not.contain('.something3') |
13 | | - expect(result).to.not.contain('.foo-bar-3') |
14 | | - }) |
| 6 | + context('when content is basic', () => { |
| 7 | + const htmlContent = fs.readFileSync(path.join(__dirname, '/fixtures/content.html'), 'utf8') |
| 8 | + const cssContent = fs.readFileSync(path.join(__dirname, '/fixtures/content.css'), 'utf8') |
15 | 9 |
|
16 | | - it('should remove empty media sets', function () { |
17 | | - const result = nuke(htmlContent, cssContent) |
18 | | - expect(result).to.not.contain('@media') |
19 | | - }) |
| 10 | + it('should remove unused rules', function () { |
| 11 | + const result = nukecss(htmlContent, cssContent) |
| 12 | + expect(result).to.not.contain('foobar[something=x]') |
| 13 | + expect(result).to.not.contain('.something3') |
| 14 | + expect(result).to.not.contain('.foo-bar-3') |
| 15 | + }) |
| 16 | + |
| 17 | + it('should remove empty media sets', function () { |
| 18 | + const result = nukecss(htmlContent, cssContent) |
| 19 | + expect(result).to.not.contain('@media') |
| 20 | + }) |
20 | 21 |
|
21 | | - it('should respect nukecss:* comments', function () { |
22 | | - const result = nuke(htmlContent, cssContent) |
23 | | - expect(result).to.contain('.totally-unused') |
| 22 | + it('should respect nukecss:* comments', function () { |
| 23 | + const result = nukecss(htmlContent, cssContent) |
| 24 | + expect(result).to.contain('.totally-unused') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should support multiple sources', function () { |
| 28 | + const result = nukecss([htmlContent, '<div id="foo-bar-3"></div>'], cssContent) |
| 29 | + expect(result).to.contain('.foo-bar-3') |
| 30 | + }) |
24 | 31 | }) |
25 | 32 |
|
26 | | - it('should support multiple sources', function () { |
27 | | - const result = nuke([htmlContent, '<div id="foo-bar-3"></div>'], cssContent) |
28 | | - expect(result).to.contain('.foo-bar-3') |
| 33 | + context('when content is parseable', () => { |
| 34 | + const jsContent = 'const jsignored = "js-class other-class"' |
| 35 | + const moreJsContent = 'const woah = ["still", "works"].join("-")' |
| 36 | + const htmlContent = '<div id="primary" class="html-class">html-ignored</div>' |
| 37 | + const cssContent = ` |
| 38 | + .jsignored { color: white; } |
| 39 | + .html-ignored { color: white; } |
| 40 | + .js-class { color: white; } |
| 41 | + .other-class { color: white; } |
| 42 | + .still-works { color: white; } |
| 43 | + .html-class { color: white; } |
| 44 | + #primary { color: white; } |
| 45 | + #primary > .unused { color: white; } |
| 46 | + .also-unused { color: white; } |
| 47 | + `.replace(/\n\s*/g, '\n') |
| 48 | + |
| 49 | + const nuked = nukecss([ |
| 50 | + {content: jsContent, type: 'js'}, |
| 51 | + {content: moreJsContent, type: 'js'}, |
| 52 | + {content: htmlContent, type: 'html'}, |
| 53 | + ], cssContent) |
| 54 | + |
| 55 | + console.log(nuked) |
| 56 | + |
| 57 | + it('should remove unused rules', function () { |
| 58 | + expect(nuked).to.not.contain('#primary > .unused') |
| 59 | + expect(nuked).to.not.contain('.also-unused') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should not remove used rules', function () { |
| 63 | + expect(nuked).to.contain('.js-class') |
| 64 | + expect(nuked).to.contain('.other-class') |
| 65 | + expect(nuked).to.contain('.html-class') |
| 66 | + expect(nuked).to.contain('#primary') |
| 67 | + }) |
| 68 | + |
| 69 | + it('should remove unused rules mentioned in non-strings', function () { |
| 70 | + expect(nuked).to.not.contain('jsignored') |
| 71 | + }) |
| 72 | + |
| 73 | + it.skip('should remove unused rules mentioned in textnodes', function () { |
| 74 | + expect(nuked).to.not.contain('html-ignored') |
| 75 | + }) |
| 76 | + |
| 77 | + it('should not remove unused rules dynamically joined', function () { |
| 78 | + expect(nuked).to.contain('.still-works') |
| 79 | + }) |
29 | 80 | }) |
30 | 81 | }) |
0 commit comments