Skip to content

Commit 9424df3

Browse files
committed
test: 'Tapable'
1 parent 57f15ee commit 9424df3

File tree

6 files changed

+603
-18
lines changed

6 files changed

+603
-18
lines changed

Diff for: package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
"packages/docs"
77
],
88
"description": "Minimalistic doc generator with Vue component based layout system",
9-
"main": "lib/index.js",
10-
"bin": {
11-
"vuepress": "bin/vuepress.js"
12-
},
139
"scripts": {
1410
"boot": "node scripts/bootstrap.js",
1511
"dev": "lerna run --scope docs dev",
@@ -45,6 +41,8 @@
4541
},
4642
"devDependencies": {
4743
"@vue/test-utils": "^1.0.0-beta.16",
44+
"@babel/core": "^7.0.0-beta.47",
45+
"@babel/preset-env": "^7.0.0-beta.47",
4846
"babel-core": "^7.0.0-0",
4947
"babel-jest": "^23.0.0",
5048
"conventional-changelog-cli": "^1.3.22",
+68-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
1-
test('two plus two is four', () => {
2-
expect(2 + 2).toBe(4)
1+
// TODO change to ES6 import
2+
// https://github.com/facebook/jest/issues/6835
3+
4+
const Tapable = require('../../lib/plugin-api/core/Tapable')
5+
6+
describe('Tapable', () => {
7+
test('shoould tapable record the key', () => {
8+
const tapable = new Tapable('option')
9+
expect(tapable.key).toBe('option')
10+
})
11+
12+
test('should \'tap\' work', () => {
13+
const tapable = new Tapable('option')
14+
tapable.tap('plugin-a', 'a')
15+
tapable.tap('plugin-b', 'b')
16+
expect(tapable.items).toEqual([
17+
{ value: 'a', name: 'plugin-a' },
18+
{ value: 'b', name: 'plugin-b' }
19+
])
20+
expect(tapable.values).toEqual(['a', 'b'])
21+
})
22+
23+
test('should \'tap\' resolve array value', () => {
24+
const tapable = new Tapable('option')
25+
tapable.tap('plugin-a', ['a-1', 'a-2'])
26+
tapable.tap('plugin-b', 'b')
27+
expect(tapable.items).toEqual([
28+
{ value: 'a-1', name: 'plugin-a' },
29+
{ value: 'a-2', name: 'plugin-a' },
30+
{ value: 'b', name: 'plugin-b' }
31+
])
32+
expect(tapable.values).toEqual(['a-1', 'a-2', 'b'])
33+
})
34+
35+
test('should \'run\' work', async () => {
36+
const tapable = new Tapable('option')
37+
const handler1 = jest.fn()
38+
const handler2 = jest.fn()
39+
40+
tapable.tap('plugin-a', handler1)
41+
tapable.tap('plugin-b', handler2)
42+
43+
await tapable.run(1, 2)
44+
expect(handler1.mock.calls).toHaveLength(1)
45+
expect(handler2.mock.calls).toHaveLength(1)
46+
expect(handler1.mock.calls[0][0]).toBe(1)
47+
expect(handler1.mock.calls[0][1]).toBe(2)
48+
expect(handler2.mock.calls[0][0]).toBe(1)
49+
expect(handler2.mock.calls[0][1]).toBe(2)
50+
})
51+
52+
test('should \'parallelRun\' work', async () => {
53+
const tapable = new Tapable('option')
54+
const handler1 = jest.fn()
55+
const handler2 = jest.fn()
56+
57+
tapable.tap('plugin-a', handler1)
58+
tapable.tap('plugin-b', handler2)
59+
60+
await tapable.parallelRun(1, 2)
61+
expect(handler1.mock.calls).toHaveLength(1)
62+
expect(handler2.mock.calls).toHaveLength(1)
63+
expect(handler1.mock.calls[0][0]).toBe(1)
64+
expect(handler1.mock.calls[0][1]).toBe(2)
65+
expect(handler2.mock.calls[0][0]).toBe(1)
66+
expect(handler2.mock.calls[0][1]).toBe(2)
67+
})
368
})
69+

Diff for: packages/@vuepress/core/lib/plugin-api/core/Tapable.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = class Tapable {
55
}
66

77
/**
8-
* set value with name.
8+
* Set value with name.
99
* @param {string} name
1010
* @param {any} value
1111
*/
@@ -17,15 +17,15 @@ module.exports = class Tapable {
1717
}
1818

1919
/**
20-
* get values.
20+
* Get values.
2121
* @returns {Array<T>}
2222
*/
2323
get values () {
2424
return this.items.map(item => item.value)
2525
}
2626

2727
/**
28-
* execute in serial
28+
* When T is function, eecute all functions in serial
2929
* @param args
3030
* @returns {Promise.<void>}
3131
*/
@@ -36,7 +36,7 @@ module.exports = class Tapable {
3636
}
3737

3838
/**
39-
* execute in parallel
39+
* When T is function, eecute all functions in parallel
4040
* @param args
4141
* @returns {Promise.<void>}
4242
*/

Diff for: scripts/jest.config.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ module.exports = {
1515
'test.js',
1616
path.resolve(__dirname, '../test')
1717
],
18+
// projects: [
19+
// '<rootDir>/packages/@vuepress/*',
20+
// {
21+
// displayName: 'test',
22+
// verbose: true,
23+
// runner: 'jest',
24+
// testURL: 'http://localhost/',
25+
// testMatch: [
26+
// '<rootDir>/packages/@vuepress/*.js'
27+
// ]
28+
// }
29+
// ],
1830
moduleNameMapper: {
19-
'^@/(.*)$': '<rootDir>/lib/$1'
31+
'^@/(.*)$': '<rootDir>/$1'
2032
},
2133
transform: {
2234
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',

Diff for: scripts/test.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ if (args.p) {
1212
rawArgs.splice(i, 2)
1313
}
1414

15-
console.log(regex)
16-
1715
;(async () => {
1816
const jestArgs = [
1917
'--env', 'node',

0 commit comments

Comments
 (0)