Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Commit 7decd4f

Browse files
committed
add unit tests
- add unit test for parse and seach - update CONTRIBUTING.md: add test commands - remove undefined value webpack integration test
1 parent 5a50359 commit 7decd4f

4 files changed

Lines changed: 163 additions & 4 deletions

File tree

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Purgecss use [SemVer](http://semver.org/) for versioning.
1616
and create your branch from `master`.
1717
2. If you've added code that should be tested, add tests!
1818
3. If you've changed APIs, update the documentation.
19-
<!-- 4. Ensure the test suite passes (`npm test`). -->
19+
4. Ensure the test suite passes (`npm test`).
2020
4. Make sure your code lints (`npm run lint`).
2121

2222
### Development Workflow
@@ -26,10 +26,10 @@ several commands:
2626

2727
* `npm run dev` will build cjs and es module of Purgecss in the `lib` folder and
2828
watch for changes.
29-
<!-- - `npm run lint` checks the code style.
29+
* `npm run lint` checks the code style.
3030
* `npm test` runs the complete test suite.
3131
* `npm test -- --watch` runs an interactive test watcher.
32-
* `npm test <pattern>` runs tests with matching filenames. -->
32+
* `npm test <pattern>` runs tests with matching filenames.
3333
* `npm run build` creates the cjs and es module of Purgecss in the `lib` folder.
3434

3535
Make sure that your pull request contains unit tests for any new functionality.

__tests__/parse.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { entryPaths, entries, flatten } from './../src/parse'
2+
3+
describe('Parse entry paths', () => {
4+
it('returns an empty array by default', () => {
5+
expect(entryPaths()).toEqual([])
6+
})
7+
8+
it('returns an object as itself', () => {
9+
const o = { a: ['a', 'b', 'c'] }
10+
expect(entryPaths(o)).toEqual(o)
11+
})
12+
13+
it('puts a string inside an array', () => {
14+
const str = 'foobar'
15+
expect(entryPaths(str)).toEqual([str])
16+
})
17+
})
18+
19+
describe('Flatten entry paths', () => {
20+
it('returns an array as itself', () => {
21+
const a = ['a', 'b', 'c']
22+
expect(flatten(a)).toEqual(a)
23+
})
24+
25+
it('returns an object of arrays as one flat array', () => {
26+
const o = { a: ['a', 'b'], b: ['c', 'd'] }
27+
expect(flatten(o)).toEqual(['a', 'b', 'c', 'd'])
28+
})
29+
})
30+
31+
describe('Parse entries', () => {
32+
it('returns paths if there is no chunk name', () => {
33+
const paths = ['a', 'b', 'c']
34+
expect(entries(paths)).toEqual(paths)
35+
})
36+
37+
it('returns paths if paths are an array already', () => {
38+
const paths = ['a', 'b', 'c']
39+
expect(entries(paths, 'foobar')).toEqual(paths)
40+
})
41+
42+
it('returns chunk paths', () => {
43+
const entryPaths = ['a', 'b', 'c']
44+
const paths = {
45+
foobar: entryPaths
46+
}
47+
expect(entries(paths, 'foobar')).toEqual(entryPaths)
48+
})
49+
50+
it('returns chunk path wrapped in an array', () => {
51+
const entryPaths = 'a'
52+
const paths = {
53+
foobar: entryPaths
54+
}
55+
expect(entries(paths, 'foobar')).toEqual([entryPaths])
56+
})
57+
58+
it('returns an empty array if failed to find entry', () => {
59+
const paths = {
60+
foobar: 'a'
61+
}
62+
expect(entries(paths, 'barbar')).toEqual([])
63+
})
64+
65+
it('returns an empty array if failed to find entry with multiple paths', () => {
66+
const paths = {
67+
foobar: 'a',
68+
barbar: 'b'
69+
}
70+
expect(entries(paths, 'foofoo')).toEqual([])
71+
})
72+
})

__tests__/search.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { assets, files } from './../src/search'
2+
3+
describe('Search assets', () => {
4+
it('returns nothing if nothing is passed', () => {
5+
expect(assets()).toEqual([])
6+
})
7+
8+
it('returns matches based on a pattern', () => {
9+
const modules = {
10+
'foobar.txt': {},
11+
'barbar.css': {}
12+
}
13+
const extensions = ['.txt']
14+
const matches = [{ name: 'foobar.txt', asset: {} }]
15+
16+
expect(assets(modules, extensions)).toEqual(matches)
17+
})
18+
19+
it('returns matches if they have query', () => {
20+
const modules = {
21+
'foobar.txt?123': {},
22+
'barbar.css': {}
23+
}
24+
const extensions = ['.txt']
25+
const matches = [{ name: 'foobar.txt?123', asset: {} }]
26+
27+
expect(assets(modules, extensions)).toEqual(matches)
28+
})
29+
})
30+
31+
describe('Search files', () => {
32+
let chunk
33+
beforeEach(() => {
34+
chunk = {
35+
mapModules: function(cb) {
36+
return Array.from(this.modules, cb)
37+
}
38+
}
39+
})
40+
41+
it('returns matches based on extension', () => {
42+
chunk.modules = ['foobar.txt', 'barbar.css']
43+
const extensions = ['.txt']
44+
const matches = ['foobar.txt']
45+
46+
expect(files(chunk, extensions)).toEqual(matches)
47+
})
48+
49+
it('does not fail with missing modules', () => {
50+
chunk.modules = ['foobar.txt', '', 'barbar.css']
51+
const extensions = ['.txt']
52+
const matches = ['foobar.txt']
53+
54+
expect(files(chunk, extensions)).toEqual(matches)
55+
})
56+
57+
it('returns matches based on extension with a customized getter', () => {
58+
chunk.modules = [
59+
{
60+
resource: 'foobar.txt'
61+
},
62+
{
63+
resource: 'barbar.css'
64+
}
65+
]
66+
const extensions = ['.txt']
67+
const matches = ['foobar.txt']
68+
69+
expect(files(chunk, extensions, file => file.resource)).toEqual(matches)
70+
})
71+
72+
it('does not fail with missing modules when a getter fails', () => {
73+
chunk.modules = [
74+
{
75+
resource: 'foobar.txt'
76+
},
77+
{},
78+
{
79+
resource: 'barbar.css'
80+
}
81+
]
82+
const extensions = ['.txt']
83+
const matches = ['foobar.txt']
84+
85+
expect(files(chunk, extensions, file => file.resource)).toEqual(matches)
86+
})
87+
})

__tests__/webpack-integration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Webpack Integration Tests', () => {
3636
if (stats.hasErrors()) return done(new Error(stats.toString()))
3737
const testFile = path.join(outputDirectory, 'test.js')
3838
if (fs.existsSync(testFile)) {
39-
require(testFile)(suite)
39+
require(testFile)()
4040
}
4141
const expectedDirectory = path.join(testDirectory, 'expected')
4242
fs.readdirSync(expectedDirectory).forEach(file => {

0 commit comments

Comments
 (0)