Skip to content

Instantly share code, notes, and snippets.

@jboulhous
Created March 19, 2018 06:49
Show Gist options
  • Save jboulhous/de3e7848544f3db6ba541713117634a5 to your computer and use it in GitHub Desktop.
Save jboulhous/de3e7848544f3db6ba541713117634a5 to your computer and use it in GitHub Desktop.
Backbone small test library
class Test extends Backbone.Model {
constructor(model = {}, klass = {}) {
var args = {};
if (model instanceof Backbone.Model) {
args = model;
} else if (_.isFunction(model)) {
args = {name: _.result(model, "name"), fn: model};
} else {
args = model;
}
console.log('Test constructor', args);
super(args, klass)
}
defaults () {
return {
name: "Test case",
pass: false,
fn: async function () { return new Promise((resolve,reject) => reject("No test function was provided"))},
done: false
}
}
async run (suite = null) {
const fn = this.get('fn');
const ret = await fn(this, suite);
return ret;
}
}
class TestSuite extends Backbone.Collection {
add (test) {
if (_.isFunction(test) && !(test instanceof Test)) return this.add(new Test(test))
return super.add(test);
}
async run () {
const results = [];
for (var i = 0; i < this.models.length; i++) {
const model = this.models[i];
this.trigger("test:start", model, this);
var pass = await model.run(this);
model.set("pass", pass);
this.trigger("test:result", pass, model, this);
model.set("done", true);
this.trigger("test:finish", model, this);
results.push(pass);
};
return _.all(results)
}
}
async function createEts (test, suite) {
console.log("\n create etablissement \n ");
return new Promise(resolve => setTimeout(() => resolve(true), 1000));
}
async function updateEts (test, suite) {
console.log("\n update etablissement \n ");
return new Promise(resolve => setTimeout(() => resolve(false), 1000));
}
// tests = [
// createEts,
// updateEts,
// inviteAdmin
// ];
test = new Test({name: "create etablissement", fn: createEts});
test2 = new Test({name: "update etablissement", fn: updateEts});
// testSuite = new TestSuite([test, test2]);
testSuite = new TestSuite();
testSuite.add([test, test2]);
testSuite.add(new Test({name: "create etablissement", fn: createEts}));
testSuite.add(new Test({name: "update etablissement", fn: updateEts}));
testSuite.add([test.clone(), test2.clone()]);
testSuite.add(createEts);
testSuite.on('test:finish', test => console.log(`Test <${test.get("name")}> : ${test.get("pass") ? "pass" : "fail" }`));
// await test.run(testSuite);
await testSuite.run();
console.log('test', test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment