Permalink
Browse files

Use real ajax when options are registered through .useRealAjaxFor

This is most convenient when you need to use real ajax for some
ajax calls executed in production code. By default all ajax calls are
faked out but now it's possible to make an exception.
  • Loading branch information...
1 parent 0a7883f commit ba4a215afe4edc74b061c76d47558aa217eba183 @mileskin committed Jan 11, 2012
Showing with 42 additions and 1 deletion.
  1. +11 −1 lib/jasmine-fake-ajax.js
  2. +31 −0 spec/fake-ajax-spec.js
View
@@ -12,8 +12,11 @@
$.ajax = function(realOptions) {
jasmine.FakeAjax.recordedSession.addRealOptions(realOptions)
+ var allBypassableOptions = jasmine.FakeAjax.recordedSession.allBypassableOptions
var allFakeOptions = jasmine.FakeAjax.recordedSession.allFakeOptions
- if (!_.isEmpty(allFakeOptions)) {
+ if (!_.isEmpty(findMatchingFakeOptions(realOptions, allBypassableOptions))) {
+ return jasmine.FakeAjax.realAjax(realOptions)
+ } else if (!_.isEmpty(allFakeOptions)) {
var fakeOptions = findMatchingFakeOptions(realOptions, allFakeOptions)
if (_.isEmpty(fakeOptions)) {
jasmine.FakeAjax.log.warn(messageWithContextInfo("No matching fake ajax options was found", realOptions))
@@ -28,13 +31,19 @@
function RecordedSession() {
// All registered fake ajax options.
this.allFakeOptions = []
+ // All registered ajax options that should be bypassed and delegated to real ajax.
+ this.allBypassableOptions = []
// All real ajax calls triggered by sut.
this.allRealOptions = []
this.registerFakeAjax = function(options) {
this.allFakeOptions.push(options)
}
+ this.addBypassableOptions = function(options) {
+ this.allBypassableOptions.push(options)
+ }
+
this.addRealOptions = function(options) {
this.allRealOptions.push(options)
}
@@ -230,6 +239,7 @@
function realAjax(options) { jasmine.FakeAjax.realAjax(options) }
function fakeAjax(options) { jasmine.FakeAjax.initContext(options) }
function registerFakeAjax(options) { jasmine.FakeAjax.recordedSession.registerFakeAjax(options) }
+function useRealAjaxFor(options) { jasmine.FakeAjax.recordedSession.addBypassableOptions(options) }
function loadTestData(selector, url) { return jasmine.FakeAjax.loadTestData(selector, url) }
function latestAjax() { return jasmine.FakeAjax.latestAjax() }
function latestAjaxWithUrlMatching(partialUrl) { return jasmine.FakeAjax.latestAjaxWithUrlMatching(partialUrl) }
View
@@ -120,6 +120,37 @@ describe('registering fake ajax options', function() {
})
})
+describe('using real ajax for some ajax calls', function() {
+ var expectedData
+ var realOptions = {
+ url: 'example-fixture.html',
+ async: false,
+ dataType: 'html',
+ success: function(data) {
+ expectedData = data
+ }
+ }
+
+ beforeEach(function() {
+ expectedData = 'initial'
+ })
+
+ it('delegates registered ajax options (that should be bypassed) to real ajax', function() {
+ useRealAjaxFor({url: 'example-fixture.html'})
+ $.ajax(realOptions)
+ expect($(expectedData)).toHaveText('example')
+ })
+
+ it('delegates to real ajax only if options match', function() {
+ var exampleOptions = {url: 'example-fixture.html'}
+ _.each([{async: true}, {async: false, dataType: 'xml'}], function(nonMatchingFields) {
+ useRealAjaxFor($.extend(exampleOptions, nonMatchingFields))
+ $.ajax(realOptions)
+ expect(expectedData).toEqual('initial')
+ })
+ })
+})
+
describe('loading test data', function() {
describe('json', function() {
it('json object from external file', function() {

0 comments on commit ba4a215

Please sign in to comment.