This repository was archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapp.js
More file actions
261 lines (195 loc) · 8.23 KB
/
Copy pathapp.js
File metadata and controls
261 lines (195 loc) · 8.23 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict';
const _ = require('lodash');
const proxyquire = require('proxyquire');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs-extra'));
const RunnerFactory = require('../lib/runner');
const AllSuitesRunner = require('../lib/runner/all-suites-runner');
const mkDummyCollection = require('./utils').mkDummyCollection;
describe('App', () => {
const sandbox = sinon.sandbox.create();
let suiteCollection;
let Gemini;
let App;
let app;
let runner;
const stubFs_ = () => {
sandbox.stub(fs, 'removeAsync').returns(Promise.resolve());
sandbox.stub(fs, 'mkdirpAsync').returns(Promise.resolve());
sandbox.stub(fs, 'copyAsync').returns(Promise.resolve());
};
const mkApp_ = (config) => new App(config || {});
beforeEach(() => {
suiteCollection = mkDummyCollection();
runner = {emit: sandbox.spy()};
Gemini = sandbox.stub();
Gemini.prototype.browserIds = [];
Gemini.prototype.readTests = sandbox.stub().returns(Promise.resolve(suiteCollection));
Gemini.prototype.test = sandbox.stub().returns(Promise.resolve());
Gemini.prototype.on = sandbox.stub().yields(runner);
App = proxyquire('../lib/app', {
'./find-gemini': sandbox.stub().returns(Gemini)
});
app = mkApp_();
});
afterEach(() => sandbox.restore());
describe('initialize', () => {
beforeEach(() => stubFs_());
it('should remove old fs tree for current images dir if it exists', () => {
app.currentDir = 'current_dir';
return app.initialize()
.then(() => assert.calledWith(fs.removeAsync, 'current_dir'));
});
it('should remove old fs tree for diff images dir if it exists', () => {
app.diffDir = 'diff_dir';
return app.initialize()
.then(() => assert.calledWith(fs.removeAsync, 'diff_dir'));
});
it('should create new tree for current images dir', () => {
app.currentDir = 'current_dir';
return app.initialize()
.then(() => assert.calledWith(fs.mkdirpAsync, 'current_dir'));
});
it('should create new tree for diff images dir', () => {
app.currentDir = 'diff_dir';
return app.initialize()
.then(() => assert.calledWith(fs.mkdirpAsync, 'diff_dir'));
});
it('should read tests', () => {
const app = mkApp_({
testFiles: ['test_file', 'another_test_file']
});
return app.initialize()
.then(() => {
assert.calledWith(Gemini.prototype.readTests,
['test_file', 'another_test_file']);
});
});
it('should pass options from cli to Gemini', () => {
const app = mkApp_({
testFiles: ['test_file'],
grep: 'grep',
set: ['set']
});
return app.initialize()
.then(() => {
assert.calledWith(Gemini.prototype.readTests,
['test_file'], {grep: 'grep', sets: ['set']});
});
});
});
describe('run', () => {
it('should create and execute runner', () => {
const runnerInstance = sinon.createStubInstance(AllSuitesRunner);
sandbox.stub(RunnerFactory, 'create').returns(runnerInstance);
app.run();
assert.called(runnerInstance.run);
});
it('should pass run handler to runner which will execute gemeni', () => {
const runnerInstance = sinon.createStubInstance(AllSuitesRunner);
runnerInstance.run.yields();
sandbox.stub(RunnerFactory, 'create').returns(runnerInstance);
app.run();
assert.called(Gemini.prototype.test);
});
});
describe('addNoReferenceTest', () => {
beforeEach(() => sandbox.stub(app, 'addFailedTest'));
it('should add to test reference image path', () => {
const test = {
suite: {id: 1},
state: {name: 'state'},
browserId: 'browser'
};
sandbox.stub(app, 'getScreenshotPath').returns('some_screenshot_path');
app.addNoReferenceTest(test);
assert.equal(test.referencePath, 'some_screenshot_path');
});
it('should add test with no reference error to failed tests', () => {
const test = {
suite: {id: 1},
state: {name: 'state'},
browserId: 'browser'
};
sandbox.stub(app, 'getScreenshotPath');
app.addNoReferenceTest(test);
assert.calledWith(app.addFailedTest, test);
});
});
describe('updateReferenceImage', () => {
const mkDummyTest_ = (params) => {
return _.defaults(params || {}, {
suite: {path: 'default_suite_path'},
state: 'default_state',
browserId: 'default_browser',
referencePath: 'default/reference/path',
currentPath: 'default/current/path'
});
};
beforeEach(() => {
stubFs_();
sandbox.stub(app, 'refPathToURL');
});
it('should reject reference update if no failed test registered', () => {
const test = mkDummyTest_();
return assert.isRejected(app.updateReferenceImage(test), 'No such test failed');
});
it('should create directory tree for reference image before saving', () => {
const test = mkDummyTest_({referencePath: 'path/to/reference/image.png'});
app.addFailedTest(test);
return app.updateReferenceImage(test)
.then(() => assert.calledWith(fs.mkdirpAsync, 'path/to/reference'));
});
it('should copy current image to reference folder', () => {
const referencePath = 'path/to/reference/image.png';
const currentPath = 'path/to/current/image.png';
const test = mkDummyTest_({referencePath, currentPath});
app.addFailedTest(test);
return app.updateReferenceImage(test)
.then(() => assert.calledWith(fs.copyAsync, currentPath, referencePath));
});
it('should emit updateResult event with result argument by emit', () => {
const test = mkDummyTest_({referencePath: 'path/to/reference.png'});
const result = {
imagePath: 'path/to/reference.png',
updated: true,
suite: test.state.suite,
state: test.state,
browserId: test.browserId
};
app.addFailedTest(test);
return app.updateReferenceImage(test)
.then(() => assert.calledWithExactly(runner.emit, 'updateResult', result));
});
it('should emit updateResult event only after copy current image to reference folder', () => {
const test = mkDummyTest_();
app.addFailedTest(test);
return app.updateReferenceImage(test)
.then(() => assert.isTrue(runner.emit.calledAfter(fs.copyAsync)));
});
it('should be resolved with URL to updated reference', () => {
const test = mkDummyTest_();
app.refPathToURL.returns(Promise.resolve('http://dummy_ref.url'));
app.addFailedTest(test);
return app.updateReferenceImage(test)
.then((result) => assert.equal(result, 'http://dummy_ref.url'));
});
});
describe('refPathToURL', () => {
beforeEach(() => {
app.referenceDirs = {
'browser_id': 'browser_reference_dir'
};
});
it('should append timestamp to resulting URL', () => {
const result = app.refPathToURL('full_path', 'browser_id');
return assert.match(result, /\?t=\d+/);
});
});
describe('currentPathToURL', () => {
it('should append timestamp to resulting URL', () => {
const result = app.currentPathToURL('full_path');
return assert.match(result, /\?t=\d+/);
});
});
});