Skip to content

Commit 2e8ce24

Browse files
committed
add option to test desired mocks only
1 parent a0ec7fe commit 2e8ce24

File tree

2 files changed

+66
-60
lines changed

2 files changed

+66
-60
lines changed

tasks/test_mock.js

+65-59
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var minimist = require('minimist');
12
var jsdom = require('jsdom');
23
var path = require('path');
34
var fs = require('fs');
@@ -14,15 +15,72 @@ plotlyServerDom.window.eval(plotlyJsSource);
1415
var pathToRoot = path.join(__dirname, '..');
1516
var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks');
1617

17-
function startsWithLowerCaseOrNumber(v) {
18-
var c = v.charAt(0);
19-
return c !== c.toUpperCase() || c === String(Number(c));
18+
var list = [];
19+
20+
// command line options
21+
var args = minimist(process.argv.slice(2), {});
22+
if(args._.length) {
23+
// test listed mock(s)
24+
list = args._;
25+
} else {
26+
// no mock listed, test all excluding the black list
27+
list = fs.readdirSync(pathToMocks)
28+
.filter(function(e) { return e.indexOf('.json') !== -1; })
29+
.map(function(e) { return e.replace('.json', ''); })
30+
.filter(notBlackListed);
31+
}
32+
33+
var fail;
34+
var failedMocks = [];
35+
36+
for(var i = 0; i < list.length; i++) {
37+
var name = list[i];
38+
console.log('validating ' + name);
39+
40+
var filename = path.join(pathToMocks, name + '.json');
41+
var fig = JSON.parse(fs.readFileSync(filename));
42+
var out = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);
43+
44+
fail = false;
45+
assert(name, out);
46+
if(fail) failedMocks.push(name);
47+
}
48+
49+
if(failedMocks.length) {
50+
var error = 'Failed at ' + JSON.stringify({mocks: failedMocks}, null, 2);
51+
throw error;
52+
}
53+
54+
function expectToBe(actual, expected) {
55+
if(actual !== expected) {
56+
console.error('Expected ' + actual + ' to be ' + expected);
57+
fail = true;
58+
}
2059
}
2160

22-
var list = fs.readdirSync(pathToMocks)
23-
.filter(startsWithLowerCaseOrNumber)
24-
.map(function(e) { return e.replace('.json', ''); })
25-
.filter(notBlackListed);
61+
function assert(name, v) {
62+
var success = true;
63+
if(!v) {
64+
expectToBe(v, undefined);
65+
if(v !== undefined) success = false;
66+
} else {
67+
v.forEach(function(e) {
68+
var condition = (
69+
e.code === 'invisible' ||
70+
e.code === 'dynamic' ||
71+
e.path[e.path.length - 1] === 'coloraxis'
72+
);
73+
expectToBe(condition, true); // we accept invisible, dynamic and coloraxis for now
74+
if(!condition) {
75+
console.log('file:', name);
76+
console.log(JSON.stringify(v, null, 2));
77+
success = false;
78+
return success;
79+
}
80+
});
81+
}
82+
return success;
83+
}
2684

2785
function notBlackListed(name) {
2886
return [
@@ -200,55 +258,3 @@ function notBlackListed(name) {
200258
'yiorrd_heatmap'
201259
].indexOf(name) === -1;
202260
}
203-
204-
var fail;
205-
var failedMocks = [];
206-
207-
for(var i = 0; i < list.length; i++) {
208-
var name = list[i];
209-
console.log('validating ' + name);
210-
211-
var filename = path.join(pathToMocks, name + '.json');
212-
var fig = JSON.parse(fs.readFileSync(filename));
213-
var out = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);
214-
215-
fail = false;
216-
assert(name, out);
217-
if(fail) failedMocks.push(name);
218-
}
219-
220-
if(failedMocks.length) {
221-
var error = 'Failed at ' + JSON.stringify({mocks: failedMocks}, null, 2);
222-
throw error;
223-
}
224-
225-
function expectToBe(actual, expected) {
226-
if(actual !== expected) {
227-
console.error('Expected ' + actual + ' to be ' + expected);
228-
fail = true;
229-
}
230-
}
231-
232-
function assert(name, v) {
233-
var success = true;
234-
if(!v) {
235-
expectToBe(v, undefined);
236-
if(v !== undefined) success = false;
237-
} else {
238-
v.forEach(function(e) {
239-
var condition = (
240-
e.code === 'invisible' ||
241-
e.code === 'dynamic' ||
242-
e.path[e.path.length - 1] === 'coloraxis'
243-
);
244-
expectToBe(condition, true); // we accept invisible, dynamic and coloraxis for now
245-
if(!condition) {
246-
console.log('file:', name);
247-
console.log(JSON.stringify(v, null, 2));
248-
success = false;
249-
return success;
250-
}
251-
});
252-
}
253-
return success;
254-
}

test/image/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ a separate tab to ensure that the most up-to-date code is used.
6868
Also if you are adding a new mock, you may need to re-run `npm start` or `npm run watch`
6969
to be able to find the new mock in the browser.
7070
To help ensure valid attributes are used in your new mock(s), please run `npm run test-mock`
71-
after adding new mocks or implementing any new attributes.
71+
or `npm run test-mock mock_name(s)` after adding new mocks or implementing any new attributes.
7272

7373
##### A: Run image comparison tests
7474

0 commit comments

Comments
 (0)