Last active
September 20, 2016 17:53
-
-
Save ClayShentrup/68e4ac26a543a286bab1d1d0dfbaf5f6 to your computer and use it in GitHub Desktop.
Jasmine spec helper basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* { | |
transition: none !important; // the only time I will *EVER* use !important, I promise. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasmine.getFixtures().containerId = 'jasmine_content'; | |
App.testHelpers.loadFixture = function (fixturePath) { | |
App.testHelpers.withoutMockAjax(function () { | |
loadFixtures(fixturePath + '.html'); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get this from http://github.com/jasmine/jasmine-ajax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This ensures that our JavaScript doesn't use unstubbed AJAX calls | |
*/ | |
beforeEach(function () { | |
jasmine.Ajax.install(); | |
}); | |
afterEach(function () { | |
jasmine.Ajax.uninstall(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(function () { | |
jasmine.clock().install(); | |
}); | |
afterEach(function () { | |
jasmine.clock().uninstall(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(function () { | |
jasmine.clock().mockDate( | |
new Date(2015, 4, 20) | |
); | |
spyOn(jQuery, 'now').and.callFake(Date.now); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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([]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(function () { | |
spyOn(Turbolinks, 'visit'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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