-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.test.js
99 lines (84 loc) · 2.22 KB
/
plugin.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var rewire = require("rewire");
var plugin = rewire('../src/plugin');
describe('plugin', function(){
describe('getOptionsSync', function(){
var xxx;
plugin.bindHook(
'page',{
fn : function(){
xxx = this.options.xxx;
},
options: {xxx: 1}
}
)
it('testOptionSync', function(){
plugin.emitHook('page');
assert.equal(xxx, 1)
})
})
describe('getOptionsAsync', function(){
var xxx;
plugin.bindHook(
'page',{
fn : function(){
return new Promise((resolve)=>{
setTimeout(()=>{
xxx = this.options.xxx;
resolve(true)
}, 200)
})
},
options: {xxx: 2}
}
)
it('testOptionAsync', async function(){
await plugin.emitHook('page');
assert.equal(xxx, 2)
})
})
describe('handleAssets', function(){
let handleAssets = plugin.__get__('handleAssets');
var _ensureDirSync, _copySync;
var assets = [];
plugin.__set__("_importAsset", function(filepath, type, pluginAssetPath){
assets.push({
filepath
})
})
let ydoc = plugin.__get__('ydoc');
ydoc.config.buildPath = '/var1/www/build';
plugin.__set__("ydoc", ydoc)
var config = {
dir: 'assets',
js: 'a.js',
css: ['b.css', 'c.css']
}
var dirpath = '/var1/www/ydoc';
var pluginName = 'test';
var pluginPath, assetsPath;
before(function(){
let FsMock = plugin.__get__('fs');
assets = [];
FsMock.ensureDirSync = function(filepath){
pluginPath = filepath;
};
FsMock.copySync = function(filepath){
assetsPath = filepath;
};
plugin.__set__('fs', FsMock);
})
after(function(){
plugin.__set__('fs', fs)
})
it('dir-js-css', function(){
handleAssets(config, dirpath, pluginName);
assert.equal(pluginPath, path.resolve(ydoc.config.dist, 'ydoc/ydoc-plugin-' + pluginName))
assert.equal(assetsPath, path.resolve(dirpath, config.dir))
assert.equal(assets.length, 3)
assert.deepEqual(assets[2],{filepath: 'c.css'})
})
})
})