This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
set-collection.test.js
163 lines (131 loc) Β· 5.06 KB
/
set-collection.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
const _ = require('lodash');
const q = require('q');
const globExtra = require('glob-extra');
const SetCollection = require('lib/test-reader/set-collection');
const Set = require('lib/test-reader/set');
describe('set-collection', () => {
const sandbox = sinon.sandbox.create();
const mkSetStub = () => {
const set = sandbox.stub();
set.useFiles = sandbox.stub();
return set;
};
const mkConfigStub = (opts) => {
return _.defaults(opts || {}, {
sets: opts.sets || {},
getBrowserIds: opts.getBrowserIds,
system: {
projectRoot: '/root'
}
});
};
beforeEach(() => {
sandbox.stub(globExtra, 'expandPaths').returns(q([]));
});
afterEach(() => sandbox.restore());
describe('sets are not specified in config', () => {
it('should throw an error if an unknown set was passed', () => {
assert.throws(() => SetCollection.create(mkConfigStub(), ['set3'], /set3/));
});
it('should create new set with empty files and browsers form config', () => {
sandbox.stub(Set, 'create').returns(mkSetStub());
const getBrowserIds = sandbox.stub().returns(['b1', 'b2']);
return SetCollection.create(mkConfigStub({getBrowserIds}))
.then(() => assert.calledWith(Set.create, {files: [], browsers: ['b1', 'b2']}));
});
});
it('should create collection for specified sets', () => {
const config = mkConfigStub({
sets: {
set1: {files: ['some/files']},
set2: {files: ['other/files']}
}
});
sandbox.stub(Set, 'create').returns(mkSetStub());
globExtra.expandPaths.withArgs(['some/files']).returns(q(['some/files/file.js']));
return SetCollection.create(config, ['set1'])
.then(() => {
assert.calledOnce(Set.create);
assert.calledWith(Set.create, {files: ['some/files/file.js']});
});
});
it('should expand paths using project root', () => {
return SetCollection.create(mkConfigStub({
sets: {
set: mkSetStub()
},
system: {
projectRoot: '/root'
}
}))
.then(() => {
assert.calledWithMatch(globExtra.expandPaths, sinon.match.any, {root: '/root'});
});
});
it('should create collection for config sets by default', () => {
const config = mkConfigStub({
sets: {
set1: {files: ['some/files']},
set2: {files: ['other/files']}
}
});
globExtra.expandPaths
.withArgs(['some/files']).returns(q(['some/files/file1.js']))
.withArgs(['other/files']).returns(q(['other/files/file2.js']));
sandbox.stub(Set, 'create').returns(mkSetStub());
return SetCollection.create(config)
.then(() => {
assert.calledWith(Set.create, {files: ['some/files/file1.js']});
assert.calledWith(Set.create, {files: ['other/files/file2.js']});
});
});
it('should throw an error if an unknown set was passed', () => {
const config = mkConfigStub({
sets: {
set1: {files: ['some/files']},
set2: {files: ['other/files']}
}
});
assert.throws(() => SetCollection.create(config, ['set3']), /set3(.+) set1, set2/);
});
it('should use passed files if sets are specified in config', () => {
const sets = {
set1: mkSetStub(),
set2: mkSetStub()
};
sandbox.stub(Set, 'create')
.onFirstCall().returns(sets.set1)
.onSecondCall().returns(sets.set2);
return SetCollection.create(mkConfigStub({sets}))
.then((setCollection) => {
setCollection.useFiles(['some/files/file.js']);
assert.calledWith(sets.set1.useFiles, ['some/files/file.js']);
assert.calledWith(sets.set2.useFiles, ['some/files/file.js']);
});
});
it('should apply function to each sets files', () => {
const config = mkConfigStub({
sets: {
set1: {
files: ['some/files'],
browsers: ['bro1']
},
set2: {
files: ['other/files'],
browsers: ['bro2']
}
}
});
globExtra.expandPaths
.withArgs(['some/files']).returns(q(['some/files/file1.js']))
.withArgs(['other/files']).returns(q(['other/files/file2.js']));
return SetCollection.create(config)
.then((setCollection) => {
const callback = sandbox.stub();
setCollection.forEachFile(callback);
assert.calledWith(callback, 'some/files/file1.js', ['bro1']);
assert.calledWith(callback, 'other/files/file2.js', ['bro2']);
});
});
});