forked from webpack-contrib/extract-text-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest.js
132 lines (114 loc) · 4.66 KB
/
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Created by jandersen on 3/23/15.
*/
var assert = require("assert");
var StringReplacePlugin = require("./index.js");
describe('StringReplacePlugin', function(){
describe('#replace()', function(){
it('should throw with invalid options', function(){
assert.throws(function() {
StringReplacePlugin.replace({
replacements: []
})
},
/Invalid options/);
assert.throws(function() {
StringReplacePlugin.replace({})
},
/Invalid options/);
});
it('should not throw with valid options', function(){
assert.doesNotThrow(function() {
var loaderStr = StringReplacePlugin.replace({
replacements: [{
pattern: /<!-- @secret (\w*?) -->/ig,
replacement: function (match, p1, offset, string) {
return secrets.web[p1];
}
}]
});
assert.ok(loaderStr.indexOf("!") === -1, 'No chained loaders expected');
});
});
it('should add next loaders', function(){
var loaderStr = StringReplacePlugin.replace('html-loader', {
replacements: [{
pattern: /<!-- @secret (\w*?) -->/ig,
replacement: function (match, p1, offset, string) {
return secrets.web[p1];
}
}]
});
assert.ok(loaderStr !== null);
assert.ok(loaderStr.indexOf('html-loader!') === 0);
});
});
describe('#apply()', function(){
var plugin = new StringReplacePlugin(),
loader = require("./loader.js"),
replInst = {
replacements: [{
pattern: /<!-- @secret (\w*?) -->/ig,
replacement: function (match, p1, offset, string) {
return 'replaced ==>' + p1 + '<==';
}
}]
},
id = null,
query = null,
replaced = null;
var callback = function() {
return function(_, source) {
replaced = source;
};
};
var mockConfig = {
options: {},
emitWarning: console.log,
async: callback
};
beforeEach(function(){
// runs before each test in this block
var loaderStr = StringReplacePlugin.replace('html-loader', replInst);
var matches = loaderStr.match(/\?id=(\w*)(?=($|!))/);
assert.ok(matches.length === 3);
query = matches[0];
id = matches[1];
});
it('should set replace options', function(){
plugin.apply(mockConfig);
var replOpts = mockConfig.options[StringReplacePlugin.REPLACE_OPTIONS];
assert.ok(replOpts !== null, 'replace options should be present');
assert.ok(replOpts[id] === replInst, 'replace options should contain id from loader');
});
it('should replace strings in source', function(){
plugin.apply(mockConfig);
mockConfig.query = query;
loader.call(mockConfig, "some string");
assert(replaced === "some string", "doesn't modify when there are no matches");
loader.call(mockConfig, "some <!-- @secret stuff --> string");
assert.equal(replaced, "some replaced ==>stuff<== string", "replaces matches");
});
it('should replace strings in source via options', function(){
mockConfig.options.replacement = {
before: 'replaced ==>',
after: '<=='
};
plugin.apply(mockConfig);
var replOpts = mockConfig.options[StringReplacePlugin.REPLACE_OPTIONS];
replOpts[id] = {
replacements: [{
pattern: /<!-- @secret (\w*?) -->/ig,
replacement: function (match, p1) {
return this.options.replacement.before + p1 + this.options.replacement.after;
}
}]
};
mockConfig.query = query;
loader.call(mockConfig, "some string");
assert(replaced === "some string", "doesn't modify when there are no matches");
loader.call(mockConfig, "some <!-- @secret stuff --> string");
assert.equal(replaced, "some replaced ==>stuff<== string", "replaces matches");
});
})
});