Skip to content

Instantly share code, notes, and snippets.

@blakehaswell
Created September 11, 2012 04:36
Show Gist options
  • Save blakehaswell/3695990 to your computer and use it in GitHub Desktop.
Save blakehaswell/3695990 to your computer and use it in GitHub Desktop.
Jasmine Test Suite Abstraction
TestSuite.add({
name: "List transformer",
getHtml: function () {
var html = [];
html.push("<ul class=\"unorderedList\">");
html.push("<li>A list item</li>");
html.push("<li>A second list item</li>");
html.push("</ul>");
html.push("<ol class=\"orderedList\">");
html.push("<li>A list item</li>");
html.push("<li>A second list item</li>");
html.push("</ol>");
return html.join("");
},
tests: function () {
beforeEach(function () {
this.$(".unorderedList, .orderedList").transformList();
});
it("Transforms unordered lists into ordered lists", function () {
expect(this.$("ol.unorderedList").length).toBe(1);
});
it("Transforms ordered lists into unordered lists", function () {
expect(this.$("ul.orderedList").length).toBe(1);
});
}
});
$.each(TestSuite.suites, function (index, suite) {
suite.run();
});
var TestSuite = function (name, html, tests) {
this.name = name;
this.html = html;
this.tests = tests;
};
TestSuite.prototype = {
run: function () {
describe(this.name, function () {
beforeEach(function () {
this.$el = $("<div></div>").append(this.html);
this.$el.appendTo("body");
});
afterEach(function () {
this.$el.remove();
});
this.tests.call(this);
});
},
$: function (selector) {
return $(selector, this.$el);
}
};
TestSuite.suites = [];
TestSuite.add = function (obj) {
var name = obj.name;
var html = obj.getHtml();
var tests = obj.tests;
TestSuite.suites.push(new TestSuite(name, html, tests));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment