Skip to content

Instantly share code, notes, and snippets.

@ClayShentrup
Last active September 20, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClayShentrup/68e4ac26a543a286bab1d1d0dfbaf5f6 to your computer and use it in GitHub Desktop.
Save ClayShentrup/68e4ac26a543a286bab1d1d0dfbaf5f6 to your computer and use it in GitHub Desktop.
Jasmine spec helper basics
App.testHelpers.stubAjaxRequestWithData = function (requestURL, data, status) {
jasmine.Ajax.stubRequest(requestURL)
.andReturn({
status: status,
responseText: data,
contentType: 'text/html',
});
};
App.testHelpers.stubSuccessfulAjaxRequestWithData =
function (requestURL, data) {
App.testHelpers.stubAjaxRequestWithData(requestURL, data, 200);
};
App.testHelpers.dataForFixturePath = function (fixturePath) {
return App.testHelpers.withoutMockAjax(function () {
return readFixtures(fixturePath);
});
};
App.testHelpers.stubAjaxRequestNoFixture = function (requestURL) {
App.testHelpers.stubSuccessfulAjaxRequestWithData(requestURL, '');
};
App.testHelpers.stubAjaxRequestWithStatus =
function (requestURL, fixturePath, status) {
App.testHelpers.stubAjaxRequestWithData(
requestURL,
App.testHelpers.dataForFixturePath(fixturePath),
status
);
};
App.testHelpers.stubAjaxRequest = function (requestURL, fixturePath) {
App.testHelpers.stubSuccessfulAjaxRequestWithData(
requestURL,
App.testHelpers.dataForFixturePath(fixturePath)
);
};
/*
jQuery's AJAX handler normally fires the callback in a setTimeout
in order to make it occur in the next tick of the event loop. This
forces us to use `jasmine.clock().tick()` in tests. By making AJAX
synchronous, the callbacks fire immediately.
*/
$.ajaxSetup({ async: false });
* {
transition: none !important; // the only time I will *EVER* use !important, I promise.
}
jasmine.getFixtures().containerId = 'jasmine_content';
App.testHelpers.loadFixture = function (fixturePath) {
App.testHelpers.withoutMockAjax(function () {
loadFixtures(fixturePath + '.html');
});
};
// Get this from http://github.com/jasmine/jasmine-ajax
/**
* This ensures that our JavaScript doesn't use unstubbed AJAX calls
*/
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
beforeEach(function () {
jasmine.clock().install();
});
afterEach(function () {
jasmine.clock().uninstall();
});
beforeEach(function () {
jasmine.clock().mockDate(
new Date(2015, 4, 20)
);
spyOn(jQuery, 'now').and.callFake(Date.now);
});
/**
* This ensures that our JavaScript doesn't set global variables.
*/
beforeEach(function () {
this.windowKeys = _.keys(window);
});
afterEach(function () {
expect(_.difference(
_.keys(window),
this.windowKeys
)).toEqual([]);
});
App.testHelpers.withoutMockAjax = function (callback) {
try {
jasmine.Ajax.uninstall();
return callback();
}finally {
jasmine.Ajax.install();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment