Skip to content

Commit

Permalink
add option to test desired mocks only
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed May 12, 2021
1 parent a0ec7fe commit 2e8ce24
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 60 deletions.
124 changes: 65 additions & 59 deletions tasks/test_mock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var minimist = require('minimist');
var jsdom = require('jsdom');
var path = require('path');
var fs = require('fs');
Expand All @@ -14,15 +15,72 @@ plotlyServerDom.window.eval(plotlyJsSource);
var pathToRoot = path.join(__dirname, '..');
var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks');

function startsWithLowerCaseOrNumber(v) {
var c = v.charAt(0);
return c !== c.toUpperCase() || c === String(Number(c));
var list = [];

// command line options
var args = minimist(process.argv.slice(2), {});
if(args._.length) {
// test listed mock(s)
list = args._;
} else {
// no mock listed, test all excluding the black list
list = fs.readdirSync(pathToMocks)
.filter(function(e) { return e.indexOf('.json') !== -1; })
.map(function(e) { return e.replace('.json', ''); })
.filter(notBlackListed);
}

var fail;
var failedMocks = [];

for(var i = 0; i < list.length; i++) {
var name = list[i];
console.log('validating ' + name);

var filename = path.join(pathToMocks, name + '.json');
var fig = JSON.parse(fs.readFileSync(filename));
var out = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);

fail = false;
assert(name, out);
if(fail) failedMocks.push(name);
}

if(failedMocks.length) {
var error = 'Failed at ' + JSON.stringify({mocks: failedMocks}, null, 2);
throw error;
}

function expectToBe(actual, expected) {
if(actual !== expected) {
console.error('Expected ' + actual + ' to be ' + expected);
fail = true;
}
}

var list = fs.readdirSync(pathToMocks)
.filter(startsWithLowerCaseOrNumber)
.map(function(e) { return e.replace('.json', ''); })
.filter(notBlackListed);
function assert(name, v) {
var success = true;
if(!v) {
expectToBe(v, undefined);
if(v !== undefined) success = false;
} else {
v.forEach(function(e) {
var condition = (
e.code === 'invisible' ||
e.code === 'dynamic' ||
e.path[e.path.length - 1] === 'coloraxis'
);
expectToBe(condition, true); // we accept invisible, dynamic and coloraxis for now
if(!condition) {
console.log('file:', name);
console.log(JSON.stringify(v, null, 2));
success = false;
return success;
}
});
}
return success;
}

function notBlackListed(name) {
return [
Expand Down Expand Up @@ -200,55 +258,3 @@ function notBlackListed(name) {
'yiorrd_heatmap'
].indexOf(name) === -1;
}

var fail;
var failedMocks = [];

for(var i = 0; i < list.length; i++) {
var name = list[i];
console.log('validating ' + name);

var filename = path.join(pathToMocks, name + '.json');
var fig = JSON.parse(fs.readFileSync(filename));
var out = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);

fail = false;
assert(name, out);
if(fail) failedMocks.push(name);
}

if(failedMocks.length) {
var error = 'Failed at ' + JSON.stringify({mocks: failedMocks}, null, 2);
throw error;
}

function expectToBe(actual, expected) {
if(actual !== expected) {
console.error('Expected ' + actual + ' to be ' + expected);
fail = true;
}
}

function assert(name, v) {
var success = true;
if(!v) {
expectToBe(v, undefined);
if(v !== undefined) success = false;
} else {
v.forEach(function(e) {
var condition = (
e.code === 'invisible' ||
e.code === 'dynamic' ||
e.path[e.path.length - 1] === 'coloraxis'
);
expectToBe(condition, true); // we accept invisible, dynamic and coloraxis for now
if(!condition) {
console.log('file:', name);
console.log(JSON.stringify(v, null, 2));
success = false;
return success;
}
});
}
return success;
}
2 changes: 1 addition & 1 deletion test/image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ a separate tab to ensure that the most up-to-date code is used.
Also if you are adding a new mock, you may need to re-run `npm start` or `npm run watch`
to be able to find the new mock in the browser.
To help ensure valid attributes are used in your new mock(s), please run `npm run test-mock`
after adding new mocks or implementing any new attributes.
or `npm run test-mock mock_name(s)` after adding new mocks or implementing any new attributes.

##### A: Run image comparison tests

Expand Down

0 comments on commit 2e8ce24

Please sign in to comment.