Skip to content

Instantly share code, notes, and snippets.

@dmgarland
Last active December 29, 2015 10:19
Show Gist options
  • Select an option

  • Save dmgarland/7656211 to your computer and use it in GitHub Desktop.

Select an option

Save dmgarland/7656211 to your computer and use it in GitHub Desktop.
My latest TaskSpec.js
describe("Tasks", function() {
var task;
var last_task_id;
beforeEach(function() {
task = new app.models.Task("Buy the milk");
});
describe("makeId", function() {
it("just glue a number onto the end of the cool_beans", function() {
expect(task.makeId(1)).toBe("cool_beans_1");
});
});
describe("when creating a Task on a real rails server", function() {
it("should give me the newly created Task as JSON", function() {
var callback = jasmine.createSpy();
// spyOn(task, 'getBar').andCallThrough();
task.save(callback);
waitsFor(function() {
return callback.callCount > 0;
});
runs(function() {
var dataFromRails = callback.mostRecentCall.args[0];
expect(dataFromRails.statusText).toNotBe("error");
expect(dataFromRails["title"]).toBe("Buy the milk");
last_task_id = dataFromRails.id
});
});
});
describe("when calling the dummy ajax method", function() {
it("should call the correct URL", function() {
spyOn($, "ajax");
task.save();
var lastRequest = $.ajax.mostRecentCall.args[0];
expect(lastRequest["url"]).toEqual("http://localhost:3000/tasks/");
expect(lastRequest.data.task.title).toEqual("Buy the milk");
});
});
describe("Finding tasks", function() {
it("set the Task info from Rails", function() {
var new_task = new app.models.Task();
new_task.id = last_task_id;
var callback = jasmine.createSpy();
new_task.find(callback);
waitsFor(function() {
return callback.callCount > 0;
});
runs(function() {
var lastRequest = callback.mostRecentCall.args[0];
var url = "http://localhost:3000/tasks/" + new_task.id;
expect(new_task.id).toEqual(new_task.id);
expect(new_task.title).toEqual("Buy the milk");
});
});
it("should send right parameters to AJAX", function() {
var new_task = new app.models.Task();
new_task.title = "Buy the milk";
new_task.id = last_task_id;
spyOn($, "ajax");
new_task.find();
var lastRequest = $.ajax.mostRecentCall.args[0];
expect(lastRequest.url).toEqual("http://localhost:3000/tasks/" + new_task.id);
expect(lastRequest.type).toEqual("GET");
expect(lastRequest.data.task.title).toEqual("Buy the milk");
});
})
describe("real delete to destroy", function() {
it("it should work", function() {
var callback = jasmine.createSpy();
task.id = last_task_id;
task.destroy(callback);
waitsFor(function() {
return callback.callCount > 0;
});
runs(function() {
var dataFromRails = callback.mostRecentCall.args[0];
expect(dataFromRails.statusText).toNotBe("error");
expect(dataFromRails["message"]).toBe("Task Destroyed");
});
});
});
describe("test ajax stubbed version", function() {
it("should work", function() {
spyOn($, "ajax");
task.destroy();
var lastRequest = $.ajax.mostRecentCall.args[0];
expect(lastRequest["url"]).toEqual("http://localhost:3000/tasks/" + task.id);
expect(lastRequest["type"]).toEqual("POST");
});
});
it("should validate the title", function() {
task.title = null;
expect(task.valid()).toBeFalsy();
task.title = undefined;
expect(task.valid()).toBeFalsy();
task.title = "";
expect(task.valid()).toBeFalsy();
task.title = "Cool Beans";
expect(task.valid()).toBeTruthy();
});
describe("save without a title", function() {
it("should do nothing", function() {
spyOn($, "ajax");
task.title = null;
task.save();
expect($.ajax.mostRecentCall).toEqual({});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment