Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mileskin/jasmine-fake-ajax
base: 0a7883f5c3
...
head fork: mileskin/jasmine-fake-ajax
compare: 1aff16eac9
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 11, 2012
@mileskin 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.
ba4a215
@mileskin Other options are not always fake (could be bypassable) so be a littl…
…e bit less specific
7f2225f
Commits on Jan 12, 2012
@mileskin Document .useRealAjax 1aff16e
Showing with 56 additions and 14 deletions.
  1. +1 −0  README.md
  2. +22 −12 lib/jasmine-fake-ajax.js
  3. +33 −2 spec/fake-ajax-spec.js
View
1  README.md
@@ -22,6 +22,7 @@ See `spec/fake-ajax-spec.js` for executable specification and many examples.
* Function or array of functions (success/error) callbacks (equivalent in jQuery)
* Registering single/multiple fake Ajax callback rules through `registerFakeAjax({...})` `fakeAjax({registrations: {...}})`
* Support for inlining the fake data (e.g. json, xml or html) or loading the fake data from external files using `loadTestData`, works nicely with [jasmine-jquery](https://github.com/velesin/jasmine-jquery) as long as you use `loadTestData` and not equivalent fixture loading functions in jasmine-jquery
+* All ajax calls are faked by default but you can delegate any ajax call to real ajax by using `.useRealAjaxFor(options)`, e.g. `useRealAjaxFor({url: '/example', type: 'post'})`
* Real jQuery Ajax can be used anytime through `realAjax` global convenience function
* Descriptive and helpful warning/error messages logged to console
* Works nicely with Firefox and Chrome
View
34 lib/jasmine-fake-ajax.js
@@ -12,9 +12,12 @@
$.ajax = function(realOptions) {
jasmine.FakeAjax.recordedSession.addRealOptions(realOptions)
+ var allBypassableOptions = jasmine.FakeAjax.recordedSession.allBypassableOptions
var allFakeOptions = jasmine.FakeAjax.recordedSession.allFakeOptions
- if (!_.isEmpty(allFakeOptions)) {
- var fakeOptions = findMatchingFakeOptions(realOptions, allFakeOptions)
+ if (!_.isEmpty(findMatchingOptions(realOptions, allBypassableOptions))) {
+ return jasmine.FakeAjax.realAjax(realOptions)
+ } else if (!_.isEmpty(allFakeOptions)) {
+ var fakeOptions = findMatchingOptions(realOptions, allFakeOptions)
if (_.isEmpty(fakeOptions)) {
jasmine.FakeAjax.log.warn(messageWithContextInfo("No matching fake ajax options was found", realOptions))
} else {
@@ -28,6 +31,8 @@
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 = []
@@ -35,6 +40,10 @@
this.allFakeOptions.push(options)
}
+ this.addBypassableOptions = function(options) {
+ this.allBypassableOptions.push(options)
+ }
+
this.addRealOptions = function(options) {
this.allRealOptions.push(options)
}
@@ -124,25 +133,25 @@
throw errorMessage
}
- function findMatchingFakeOptions(realOptions, allFakeOptions) {
+ function findMatchingOptions(realOptions, allOtherOptions) {
var comparableFields = ['url', 'type', 'data', 'dataType', 'async']
- var allMatchingFakeOptions = _.select(allFakeOptions, function(fakeOptions) {
+ var allMatchingOptions = _.select(allOtherOptions, function(otherOptions) {
var real = _.clone(realOptions)
- var fake = _.clone(fakeOptions)
- _.each([real, fake], function(o) {
+ var other = _.clone(otherOptions)
+ _.each([real, other], function(o) {
o.type = (o.type || 'get').toLowerCase()
})
return _.all(comparableFields, function(field) {
- return _.isUndefined(fake[field]) ? true : _.isEqual(fake[field], real[field])
+ return _.isUndefined(other[field]) ? true : _.isEqual(other[field], real[field])
})
})
- if (allMatchingFakeOptions.length > 1) {
- logAndThrow("Multiple matching fake ajax options found, not able to decide which callbacks to use " +
+ if (allMatchingOptions.length > 1) {
+ logAndThrow("Multiple matching ajax options found, not able to decide which callbacks to use " +
"because the result was ambiguous. Real ajax options: " + JSON.stringify(realOptions) + ". " +
- "All matching (and thus conflicting) fake options: " +
- _.map(allMatchingFakeOptions, function(fakeOptions){ return JSON.stringify(fakeOptions) }))
+ "All matching (and thus conflicting) options: " +
+ _.map(allMatchingOptions, function(options){ return JSON.stringify(options) }))
}
- return _.first(allMatchingFakeOptions)
+ return _.first(allMatchingOptions)
}
function callAvailableCallbackHandlers(real, fake) {
@@ -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
35 spec/fake-ajax-spec.js
@@ -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() {
@@ -557,9 +588,9 @@ describe('logging', function() {
]})
expect(function() {
$.get('a')
- }).toLogAndThrow('Multiple matching fake ajax options found, not able to decide which callbacks to use ' +
+ }).toLogAndThrow('Multiple matching ajax options found, not able to decide which callbacks to use ' +
'because the result was ambiguous. Real ajax options: {"type":"get","url":"a"}. ' +
- 'All matching (and thus conflicting) fake options: {"url":"a"},{"url":"a","type":"get"}')
+ 'All matching (and thus conflicting) options: {"url":"a"},{"url":"a","type":"get"}')
})
})

No commit comments for this range

Something went wrong with that request. Please try again.