Skip to content

Instantly share code, notes, and snippets.

@gnarargs
Created October 1, 2011 02:23
Show Gist options
  • Save gnarargs/1255504 to your computer and use it in GitHub Desktop.
Save gnarargs/1255504 to your computer and use it in GitHub Desktop.
Acceptance dsl for js
// lab_rat.js
var LabRat = {
container: '#jasmine-fixtures',
setContainer: function(newContainer) {
this.container = newContainer;
},
fillIn: function(selector, content) {
var $input = $(this.container + ' ' + selector);
this.ensureElement($input, selector);
$input.val(content);
},
clickButton: function(label) {
var $button = $('input[type=submit][value=' + label + ']', this.container);
this.ensureElement($button, label);
$button.click();
},
ensureElement: function(elem, matcher) {
if (elem.length == 0) {
throw new Error(matcher + ' was not found in ' + this.container);
}
}
};
LR = LabRat;
// Usage
LR.fillIn("#drink_name", "Large Drip");
LR.clickButton("Submit");
...etc
// in Jasmine
describe("Adding Drinks", function() {
beforeEach(function() {
CC.Form.init();
});
it("As a user, I want to add a drink and see it displayed", function() {
loadFixtures('drink_form.html');
LR.fillIn("#drink_name", "Large Drip");
LR.clickButton("Submit");
expect($("#results")).toHaveText("Large Drip");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment