Skip to content

Commit

Permalink
Mocha based client unit testing of rule tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Apr 13, 2018
1 parent f62a25d commit 79a8d3e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ client-format: node-deps ## Reformat client code
client-watch: node-deps ## A useful target for parallel development building.
cd client && yarn run watch

client-test: client ## Run qunit tests via Karma
cd client && yarn run test
_client-test-mocha: ## Run mocha tests via karma
cd client && GALAXY_TEST_FRAMEWORK=mocha yarn run test

_client-test-qunit: ## Run qunit tests via karma
cd client && GALAXY_TEST_FRAMEWORK=qunit yarn run test

client-test: client _client-test-mocha _client-test-qunit ## Run JS unit tests via Karma

client-test-watch: client ## Watch and run qunit tests on changes via Karma
cd client && yarn run test-watch
Expand Down
1 change: 1 addition & 0 deletions client/galaxy/scripts/mocha/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import rules from "mocha/tests/rules_tests";
1 change: 1 addition & 0 deletions client/galaxy/scripts/mocha/tests/rules_dsl_spec.yml
58 changes: 58 additions & 0 deletions client/galaxy/scripts/mocha/tests/rules_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import chai from "chai";
import RuleDefs from "mvc/rules/rule-definitions";
import SPEC_TEST_CASES from "json-loader!yaml-loader!./rules_dsl_spec.yml";

const RULES = RuleDefs.RULES;

function applyRules(rules, data, sources) {
let columns = [];
if(data[0]) {
for(const index in data[0]) {
columns.push("new");
}
}
for (var ruleIndex in rules) {
const rule = rules[ruleIndex];
rule.error = null;
rule.warn = null;

var ruleType = rule.type;
const ruleDef = RULES[ruleType];
const res = ruleDef.apply(rule, data, sources, columns);
if (res.error) {
throw res.error;
} else {
if (res.warn) {
rule.warn = res.warn;
}
data = res.data || data;
sources = res.sources || sources;
columns = res.columns || columns;
}
}
return { data, sources, columns };
}

function itShouldConform(specTestCase, i) {
it("should pass conformance test case " + i, function() {
chai.assert.property(specTestCase, "rules");
chai.assert.property(specTestCase, "initial");
chai.assert.property(specTestCase, "final");

const rules = specTestCase.rules;
const initial = specTestCase.initial;
const expectedFinal = specTestCase.final;

const final = applyRules(rules, initial.data, initial.sources);
const finalData = final.data;
const finalSources = final.sources;
chai.assert.deepEqual(finalData, expectedFinal.data);
if(expectedFinal.sources !== undefined) {
chai.assert.deepEqual(finalSources, expectedFinal.sources);
}
});
}

describe("Rules DSL Spec", function() {
SPEC_TEST_CASES.forEach(itShouldConform);
});
26 changes: 20 additions & 6 deletions client/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ var single_pack_mode = function(){
return process.env.GALAXY_TEST_AS_SINGLE_PACK || false;
};

var TESTS_SEPARATE_PACKS = [
var test_framework = function() {
return process.env.GALAXY_TEST_FRAMEWORK || "qunit";
}

var QUNIT_TESTS_SEPARATE_PACKS = [
{pattern: 'galaxy/scripts/qunit/tests/galaxy_app_base_tests.js', watched: false},
// Something is funky with form_tests.js - needs to come before all other tests.
{pattern: 'galaxy/scripts/qunit/tests/form_tests.js', watched: false},
Expand All @@ -41,22 +45,31 @@ var TESTS_SEPARATE_PACKS = [
// {pattern: 'galaxy/scripts/qunit/tests/ui_tests.js', watched: false},
];

var TESTS_AS_SINGLE_PACK = [
var QUNIT_TESTS_AS_SINGLE_PACK = [
{pattern: 'galaxy/scripts/qunit/test.js', watched: false},
];


QUNIT_TEST_FILES = single_pack_mode() ? QUNIT_TESTS_AS_SINGLE_PACK : QUNIT_TESTS_SEPARATE_PACKS;
MOCHA_TEST_FILES = [
{pattern: 'galaxy/scripts/mocha/test.js', watched: false},
];
TEST_FILES = test_framework() == "qunit" ? QUNIT_TEST_FILES : MOCHA_TEST_FILES;
FRAMEWORK_PLUGIN = test_framework() == "qunit" ? "karma-qunit" : "karma-mocha";

// karma.conf.js
module.exports = function(config) {
config.set({
basepath: '.',
files: (single_pack_mode() ? TESTS_AS_SINGLE_PACK : TESTS_SEPARATE_PACKS).concat([
files: TEST_FILES.concat([
// Non-test assets that will be served by web server.
// CSS needed by tests.
'galaxy/scripts/qunit/assets/*.css',
]),
plugins: [
'karma-webpack',
'karma-qunit',
FRAMEWORK_PLUGIN,
'karma-mocha',
'karma-polyfill',
'karma-phantomjs-launcher',
],
Expand All @@ -69,10 +82,11 @@ module.exports = function(config) {
// that we have a working setup.
preprocessors: {
// add webpack as preprocessor
'galaxy/scripts/qunit/**/*.js': ['webpack']
'galaxy/scripts/qunit/**/*.js': ['webpack'],
'galaxy/scripts/mocha/**/*.js': ['webpack']
},

frameworks: ['polyfill', 'qunit'],
frameworks: ['polyfill', test_framework()],

webpack: webpackConfig,
webpackMiddleware: { noInfo: false }
Expand Down
6 changes: 5 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"babel-loader": "^7.1.1",
"babel-plugin-transform-vue-template": "^0.3.1",
"babel-preset-env": "^1.6.1",
"chai": "^4.1.2",
"concurrently": "^3.5.1",
"css-loader": "^0.28.7",
"del": "^3.0.0",
Expand All @@ -83,17 +84,20 @@
"gulp-watch": "^4.3.11",
"jshint": "^2.9.4",
"karma": "^1.7.1",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-polyfill": "^1.0.0",
"karma-qunit": "^1.2.1",
"karma-webpack": "^2.0.6",
"mocha": "^5.0.5",
"phantomjs-polyfill": "0.0.2",
"phantomjs-prebuilt": "^2.1.7",
"prettier": "^1.10.1",
"qunitjs": "^2.4.1",
"sinon": "^4.1.2",
"vue-loader": "^13.5.0",
"vue-template-compiler": "^2.5.13",
"webpack": "^3.10.0"
"webpack": "^3.10.0",
"yaml-loader": "^0.5.0"
}
}

0 comments on commit 79a8d3e

Please sign in to comment.