From 888f713a6fb6d0d0b03732e28857d9a583cb0157 Mon Sep 17 00:00:00 2001 From: edgar calderon Date: Wed, 12 Nov 2014 17:53:41 -0500 Subject: [PATCH 1/9] update dependencies --- karma.conf.js | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 9452e31..8d915b0 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -4,7 +4,7 @@ module.exports = function(config) { frameworks: ['qunit'], files: [ - 'bower_components/jquery/jquery.js', + 'bower_components/jquery/dist/jquery.js', 'bower_components/handlebars/handlebars.js', 'bower_components/ember/ember.js', 'bower_components/sinon/index.js', diff --git a/package.json b/package.json index c988c00..2489737 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "karma-html2js-preprocessor": "^0.1.0", "karma-qunit": "^0.1.1", "karma-script-launcher": "^0.1.0", - "qunitjs": "~1.12.0", + "qunitjs": "~1.14.0", "bower": "^1.3.5", "rf-release": "^0.1.0", "broccoli-cli": "0.0.1", @@ -36,4 +36,4 @@ }, "homepage": "https://github.com/instructure/ic-ajax", "dependencies": {} -} \ No newline at end of file +} From ea815e1841e5c2979dec46e1572ac61d5a35f510 Mon Sep 17 00:00:00 2001 From: edgar calderon Date: Thu, 13 Nov 2014 12:42:53 -0500 Subject: [PATCH 2/9] adds onSend callback option to inspect xhr settings when using fixtures --- dist/amd/main.js | 3 +++ dist/cjs/main.js | 3 +++ dist/globals/main.js | 3 +++ dist/named-amd/main.js | 3 +++ lib/main.js | 3 +++ test/main.spec.js | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 47 insertions(+) diff --git a/dist/amd/main.js b/dist/amd/main.js index 57c5d01..9e6a285 100644 --- a/dist/amd/main.js +++ b/dist/amd/main.js @@ -76,6 +76,9 @@ define( return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { + if (fixture.onSend) { + fixture.onSend(settings); + } if (fixture.textStatus === 'success' || fixture.textStatus == null) { return Ember.run.later(null, resolve, fixture); } else { diff --git a/dist/cjs/main.js b/dist/cjs/main.js index 4b6b7ef..91703fa 100644 --- a/dist/cjs/main.js +++ b/dist/cjs/main.js @@ -73,6 +73,9 @@ exports.lookupFixture = lookupFixture;function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { + if (fixture.onSend) { + fixture.onSend(settings); + } if (fixture.textStatus === 'success' || fixture.textStatus == null) { return Ember.run.later(null, resolve, fixture); } else { diff --git a/dist/globals/main.js b/dist/globals/main.js index 0489c46..a3fca39 100644 --- a/dist/globals/main.js +++ b/dist/globals/main.js @@ -74,6 +74,9 @@ exports.lookupFixture = lookupFixture;function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { + if (fixture.onSend) { + fixture.onSend(settings); + } if (fixture.textStatus === 'success' || fixture.textStatus == null) { return Ember.run.later(null, resolve, fixture); } else { diff --git a/dist/named-amd/main.js b/dist/named-amd/main.js index c928c76..778de67 100644 --- a/dist/named-amd/main.js +++ b/dist/named-amd/main.js @@ -76,6 +76,9 @@ define("ic-ajax", return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { + if (fixture.onSend) { + fixture.onSend(settings); + } if (fixture.textStatus === 'success' || fixture.textStatus == null) { return Ember.run.later(null, resolve, fixture); } else { diff --git a/lib/main.js b/lib/main.js index 538d746..a397498 100644 --- a/lib/main.js +++ b/lib/main.js @@ -72,6 +72,9 @@ function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { + if (fixture.onSend) { + fixture.onSend(settings); + } if (fixture.textStatus === 'success' || fixture.textStatus == null) { return Ember.run.later(null, resolve, fixture); } else { diff --git a/test/main.spec.js b/test/main.spec.js index c32c9fe..c6f7b24 100644 --- a/test/main.spec.js +++ b/test/main.spec.js @@ -106,6 +106,38 @@ asyncTest('the fixture jqXHR survives the response copy', function() { ) }); +asyncTest('adds onSend callback option to inspect xhr settings', function() { + + var xhrRequests = []; + + ic.ajax.defineFixture('/post', { + response: {}, + textStatus: 'success', + jqXHR: {}, + onSend: function(settings) { + xhrRequests.push(settings); + } + }); + + var req = { + type: "POST", + contentType: "application/json", + dataType: "json", + url: "/post", + data: JSON.stringify({ + foo: "bar" + }) + }; + + Ember.RSVP.all([ic.ajax.request(req), ic.ajax.request(req)]) + .then(function() { + start(); + equal(xhrRequests.length, 2); + var payload = JSON.parse(xhrRequests[0].data); + equal(payload.foo, "bar"); + }); +}); + test('throws if success or error callbacks are used', function() { var k = function() {}; throws(function() { From a5c4ffcf4e101d089a0b48cc413e9e02589cd963 Mon Sep 17 00:00:00 2001 From: Rusty Toms Date: Mon, 2 Mar 2015 18:27:18 -0500 Subject: [PATCH 3/9] allow a fixture to match a url without considering the query string A fixture with a specific match to the query string will be matched first, if not found a fixture with the same path and no query string can be matched. --- lib/main.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index a397498..cfbb042 100644 --- a/lib/main.js +++ b/lib/main.js @@ -65,14 +65,20 @@ export function defineFixture(url, fixture) { */ export function lookupFixture (url) { - return __fixtures__ && __fixtures__[url]; + var fixture = __fixtures__ && __fixtures__[url]; + + if (!fixture && typeof url === "string" && url.match(/\?/)) { + fixture = __fixtures__ && __fixtures__[url.split("?")[0]]; + } + + return fixture; } function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { - if (fixture.onSend) { + if (fixture.onSend && typeof fixture.onSend === "function") { fixture.onSend(settings); } if (fixture.textStatus === 'success' || fixture.textStatus == null) { From aad78f41802b4d6c7e9231256b7f00172abe5158 Mon Sep 17 00:00:00 2001 From: Jonathan Toms Date: Tue, 3 Mar 2015 11:43:30 -0500 Subject: [PATCH 4/9] allow a fallback fixture to be defined explicitly --- lib/main.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index cfbb042..23f6e38 100644 --- a/lib/main.js +++ b/lib/main.js @@ -32,6 +32,7 @@ export function raw() { } export var __fixtures__ = {}; +export var __fallbackFixtures__ = {}; /* * Defines a fixture that will be used instead of an actual ajax @@ -45,17 +46,25 @@ export var __fixtures__ = {}; * response: { firstName: 'Ryan', lastName: 'Florence' }, * textStatus: 'success' * jqXHR: {} + * }, { + * fallback: true * }); * * @param {String} url * @param {Object} fixture + * @param {Object} [options] - options for the fixture + * @param {boolean} [options.fallback=false] - whether or not the fixture should be used for all routes with a matching path that do not have a fixture matching their query string */ -export function defineFixture(url, fixture) { +export function defineFixture(url, fixture, options) { if (fixture.response) { fixture.response = JSON.parse(JSON.stringify(fixture.response)); } __fixtures__[url] = fixture; + + if (options && options.fallback) { + __fallbackFixtures__[url] = fixture; + } } /* @@ -68,7 +77,7 @@ export function lookupFixture (url) { var fixture = __fixtures__ && __fixtures__[url]; if (!fixture && typeof url === "string" && url.match(/\?/)) { - fixture = __fixtures__ && __fixtures__[url.split("?")[0]]; + fixture = __fallbackFixtures__ && __fallbackFixtures__[url.split("?")[0]]; } return fixture; From fd6a30167e3415279fe88320b6eceac664d517e5 Mon Sep 17 00:00:00 2001 From: Jonathan Toms Date: Tue, 3 Mar 2015 12:15:21 -0500 Subject: [PATCH 5/9] add removeFixture and removeAllFixtures methods --- lib/main.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/main.js b/lib/main.js index 23f6e38..e6964b9 100644 --- a/lib/main.js +++ b/lib/main.js @@ -83,6 +83,24 @@ export function lookupFixture (url) { return fixture; } +/* + * Removes a fixture by url. + * + * @param {String} url + */ +export function removeFixture (url) { + delete __fixtures__[url]; + delete __fallbackFixtures__[url]; +} + +/* + * Removes all fixtures. + */ +export function removeAllFixtures () { + __fixtures__ = {}; + __fallbackFixtures__ = {}; +} + function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); From 9e3712ff40017308a50ad58162d27f6de25143d6 Mon Sep 17 00:00:00 2001 From: Jonathan Toms Date: Tue, 3 Mar 2015 12:15:58 -0500 Subject: [PATCH 6/9] add tests for removeFixture, removeAllFixtures, and fallbackFixture --- test/main.spec.js | 104 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/test/main.spec.js b/test/main.spec.js index c6f7b24..ad77ba8 100644 --- a/test/main.spec.js +++ b/test/main.spec.js @@ -1,9 +1,60 @@ -module('ic-ajax'); +module('ic-ajax', { + teardown: function() { + ic.ajax.removeAllFixtures(); + } +}); test('presence', function() { ok(ic.ajax, 'ic.ajax is defined'); }); +test('finds fixtures', function() { + ic.ajax.defineFixture('/get', { + response: { foo: 'bar' }, + textStatus: 'success', + jqXHR: {} + }); + + ok(ic.ajax.lookupFixture('/get')); +}); + +test('removes fixtures', function() { + ic.ajax.defineFixture('/get', { + response: { foo: 'bar' }, + textStatus: 'success', + jqXHR: {} + }); + ic.ajax.defineFixture('/post', { + errorThrown: 'Unprocessable Entity', + textStatus: 'error', + jqXHR: {} + }); + + ok(ic.ajax.lookupFixture('/get')); + ic.ajax.removeFixture('/get'); + ok(!ic.ajax.lookupFixture('/get')); + ok(ic.ajax.lookupFixture('/post')); +}); + +test('removes all fixtures', function() { + ic.ajax.defineFixture('/get', { + response: { foo: 'bar' }, + textStatus: 'success', + jqXHR: {} + }); + ic.ajax.defineFixture('/post', { + errorThrown: 'Unprocessable Entity', + textStatus: 'error', + jqXHR: {} + }); + + ok(ic.ajax.lookupFixture('/get')); + ok(ic.ajax.lookupFixture('/post')); + ic.ajax.removeAllFixtures(); + ok(!ic.ajax.lookupFixture('/get')); + ok(!ic.ajax.lookupFixture('/post')); +}); + asyncTest('pulls from fixtures', function() { ic.ajax.defineFixture('/get', { response: { foo: 'bar' }, @@ -17,6 +68,57 @@ asyncTest('pulls from fixtures', function() { }); }); +asyncTest('uses a designated fallback fixture if no fixture is found for a request with a query string', function() { + ic.ajax.defineFixture('/get', { + response: { foo: 'bar' }, + textStatus: 'success', + jqXHR: {} + }, { + fallback: true + }); + + ok(ic.ajax.lookupFixture('/get?this=that')); + ic.ajax.raw('/get?this=that').then(function(result) { + start(); + deepEqual(result, ic.ajax.lookupFixture('/get')); + }); +}); + +asyncTest('does not use a designated fallback fixture if a fixture is found for a request matching its query string', function() { + var queryStringResponse = { foo: 'baz'}; + + ic.ajax.defineFixture('/get', { + response: { foo: 'bar' }, + textStatus: 'success', + jqXHR: {} + }, { + fallback: true + }); + + ic.ajax.defineFixture('/get?this=that', { + response: queryStringResponse, + textStatus: 'success', + jqXHR: {} + }); + + ic.ajax.raw('/get?this=that').then(function(result) { + start(); + deepEqual(result.response, queryStringResponse); + }); +}); + +test('does not set a fixture as a fallback fixture if `fallback` is not set to true in the options', function() { + var queryStringResponse = { foo: 'baz'}; + + ic.ajax.defineFixture('/get', { + response: { foo: 'bar' }, + textStatus: 'success', + jqXHR: {} + }); + + ok(!ic.ajax.lookupFixture('/get?this=that')); +}); + asyncTest('rejects the promise when the textStatus of the fixture is not success', function() { ic.ajax.defineFixture('/post', { errorThrown: 'Unprocessable Entity', From 51cdaa983abf5d8b35152586c0175fb199f7120c Mon Sep 17 00:00:00 2001 From: Jonathan Toms Date: Tue, 3 Mar 2015 12:16:58 -0500 Subject: [PATCH 7/9] rebuild dist files --- dist/amd/main.js | 43 +++++++++++++++++++++++++++++++++++++----- dist/cjs/main.js | 43 +++++++++++++++++++++++++++++++++++++----- dist/globals/main.js | 43 +++++++++++++++++++++++++++++++++++++----- dist/named-amd/main.js | 43 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 152 insertions(+), 20 deletions(-) diff --git a/dist/amd/main.js b/dist/amd/main.js index 9e6a285..e708c26 100644 --- a/dist/amd/main.js +++ b/dist/amd/main.js @@ -36,7 +36,8 @@ define( } __exports__.raw = raw;var __fixtures__ = {}; - __exports__.__fixtures__ = __fixtures__; + __exports__.__fixtures__ = __fixtures__;var __fallbackFixtures__ = {}; + __exports__.__fallbackFixtures__ = __fallbackFixtures__; /* * Defines a fixture that will be used instead of an actual ajax * request to a given url. This is useful for testing, allowing you to @@ -49,17 +50,25 @@ define( * response: { firstName: 'Ryan', lastName: 'Florence' }, * textStatus: 'success' * jqXHR: {} + * }, { + * fallback: true * }); * * @param {String} url * @param {Object} fixture + * @param {Object} [options] - options for the fixture + * @param {boolean} [options.fallback=false] - whether or not the fixture should be used for all routes with a matching path that do not have a fixture matching their query string */ - function defineFixture(url, fixture) { + function defineFixture(url, fixture, options) { if (fixture.response) { fixture.response = JSON.parse(JSON.stringify(fixture.response)); } __fixtures__[url] = fixture; + + if (options && options.fallback) { + __fallbackFixtures__[url] = fixture; + } } __exports__.defineFixture = defineFixture;/* @@ -69,14 +78,38 @@ define( */ function lookupFixture (url) { - return __fixtures__ && __fixtures__[url]; + var fixture = __fixtures__ && __fixtures__[url]; + + if (!fixture && typeof url === "string" && url.match(/\?/)) { + fixture = __fallbackFixtures__ && __fallbackFixtures__[url.split("?")[0]]; + } + + return fixture; + } + + __exports__.lookupFixture = lookupFixture;/* + * Removes a fixture by url. + * + * @param {String} url + */ + function removeFixture (url) { + delete __fixtures__[url]; + delete __fallbackFixtures__[url]; + } + + __exports__.removeFixture = removeFixture;/* + * Removes all fixtures. + */ + function removeAllFixtures () { + __fixtures__ = {}; + __fallbackFixtures__ = {}; } - __exports__.lookupFixture = lookupFixture;function makePromise(settings) { + __exports__.removeAllFixtures = removeAllFixtures;function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { - if (fixture.onSend) { + if (fixture.onSend && typeof fixture.onSend === "function") { fixture.onSend(settings); } if (fixture.textStatus === 'success' || fixture.textStatus == null) { diff --git a/dist/cjs/main.js b/dist/cjs/main.js index 91703fa..6665346 100644 --- a/dist/cjs/main.js +++ b/dist/cjs/main.js @@ -33,7 +33,8 @@ function raw() { } exports.raw = raw;var __fixtures__ = {}; -exports.__fixtures__ = __fixtures__; +exports.__fixtures__ = __fixtures__;var __fallbackFixtures__ = {}; +exports.__fallbackFixtures__ = __fallbackFixtures__; /* * Defines a fixture that will be used instead of an actual ajax * request to a given url. This is useful for testing, allowing you to @@ -46,17 +47,25 @@ exports.__fixtures__ = __fixtures__; * response: { firstName: 'Ryan', lastName: 'Florence' }, * textStatus: 'success' * jqXHR: {} + * }, { + * fallback: true * }); * * @param {String} url * @param {Object} fixture + * @param {Object} [options] - options for the fixture + * @param {boolean} [options.fallback=false] - whether or not the fixture should be used for all routes with a matching path that do not have a fixture matching their query string */ -function defineFixture(url, fixture) { +function defineFixture(url, fixture, options) { if (fixture.response) { fixture.response = JSON.parse(JSON.stringify(fixture.response)); } __fixtures__[url] = fixture; + + if (options && options.fallback) { + __fallbackFixtures__[url] = fixture; + } } exports.defineFixture = defineFixture;/* @@ -66,14 +75,38 @@ exports.defineFixture = defineFixture;/* */ function lookupFixture (url) { - return __fixtures__ && __fixtures__[url]; + var fixture = __fixtures__ && __fixtures__[url]; + + if (!fixture && typeof url === "string" && url.match(/\?/)) { + fixture = __fallbackFixtures__ && __fallbackFixtures__[url.split("?")[0]]; + } + + return fixture; +} + +exports.lookupFixture = lookupFixture;/* + * Removes a fixture by url. + * + * @param {String} url + */ +function removeFixture (url) { + delete __fixtures__[url]; + delete __fallbackFixtures__[url]; +} + +exports.removeFixture = removeFixture;/* + * Removes all fixtures. + */ +function removeAllFixtures () { + __fixtures__ = {}; + __fallbackFixtures__ = {}; } -exports.lookupFixture = lookupFixture;function makePromise(settings) { +exports.removeAllFixtures = removeAllFixtures;function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { - if (fixture.onSend) { + if (fixture.onSend && typeof fixture.onSend === "function") { fixture.onSend(settings); } if (fixture.textStatus === 'success' || fixture.textStatus == null) { diff --git a/dist/globals/main.js b/dist/globals/main.js index a3fca39..f56d499 100644 --- a/dist/globals/main.js +++ b/dist/globals/main.js @@ -34,7 +34,8 @@ function raw() { } exports.raw = raw;var __fixtures__ = {}; -exports.__fixtures__ = __fixtures__; +exports.__fixtures__ = __fixtures__;var __fallbackFixtures__ = {}; +exports.__fallbackFixtures__ = __fallbackFixtures__; /* * Defines a fixture that will be used instead of an actual ajax * request to a given url. This is useful for testing, allowing you to @@ -47,17 +48,25 @@ exports.__fixtures__ = __fixtures__; * response: { firstName: 'Ryan', lastName: 'Florence' }, * textStatus: 'success' * jqXHR: {} + * }, { + * fallback: true * }); * * @param {String} url * @param {Object} fixture + * @param {Object} [options] - options for the fixture + * @param {boolean} [options.fallback=false] - whether or not the fixture should be used for all routes with a matching path that do not have a fixture matching their query string */ -function defineFixture(url, fixture) { +function defineFixture(url, fixture, options) { if (fixture.response) { fixture.response = JSON.parse(JSON.stringify(fixture.response)); } __fixtures__[url] = fixture; + + if (options && options.fallback) { + __fallbackFixtures__[url] = fixture; + } } exports.defineFixture = defineFixture;/* @@ -67,14 +76,38 @@ exports.defineFixture = defineFixture;/* */ function lookupFixture (url) { - return __fixtures__ && __fixtures__[url]; + var fixture = __fixtures__ && __fixtures__[url]; + + if (!fixture && typeof url === "string" && url.match(/\?/)) { + fixture = __fallbackFixtures__ && __fallbackFixtures__[url.split("?")[0]]; + } + + return fixture; +} + +exports.lookupFixture = lookupFixture;/* + * Removes a fixture by url. + * + * @param {String} url + */ +function removeFixture (url) { + delete __fixtures__[url]; + delete __fallbackFixtures__[url]; +} + +exports.removeFixture = removeFixture;/* + * Removes all fixtures. + */ +function removeAllFixtures () { + __fixtures__ = {}; + __fallbackFixtures__ = {}; } -exports.lookupFixture = lookupFixture;function makePromise(settings) { +exports.removeAllFixtures = removeAllFixtures;function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { - if (fixture.onSend) { + if (fixture.onSend && typeof fixture.onSend === "function") { fixture.onSend(settings); } if (fixture.textStatus === 'success' || fixture.textStatus == null) { diff --git a/dist/named-amd/main.js b/dist/named-amd/main.js index 778de67..e127df2 100644 --- a/dist/named-amd/main.js +++ b/dist/named-amd/main.js @@ -36,7 +36,8 @@ define("ic-ajax", } __exports__.raw = raw;var __fixtures__ = {}; - __exports__.__fixtures__ = __fixtures__; + __exports__.__fixtures__ = __fixtures__;var __fallbackFixtures__ = {}; + __exports__.__fallbackFixtures__ = __fallbackFixtures__; /* * Defines a fixture that will be used instead of an actual ajax * request to a given url. This is useful for testing, allowing you to @@ -49,17 +50,25 @@ define("ic-ajax", * response: { firstName: 'Ryan', lastName: 'Florence' }, * textStatus: 'success' * jqXHR: {} + * }, { + * fallback: true * }); * * @param {String} url * @param {Object} fixture + * @param {Object} [options] - options for the fixture + * @param {boolean} [options.fallback=false] - whether or not the fixture should be used for all routes with a matching path that do not have a fixture matching their query string */ - function defineFixture(url, fixture) { + function defineFixture(url, fixture, options) { if (fixture.response) { fixture.response = JSON.parse(JSON.stringify(fixture.response)); } __fixtures__[url] = fixture; + + if (options && options.fallback) { + __fallbackFixtures__[url] = fixture; + } } __exports__.defineFixture = defineFixture;/* @@ -69,14 +78,38 @@ define("ic-ajax", */ function lookupFixture (url) { - return __fixtures__ && __fixtures__[url]; + var fixture = __fixtures__ && __fixtures__[url]; + + if (!fixture && typeof url === "string" && url.match(/\?/)) { + fixture = __fallbackFixtures__ && __fallbackFixtures__[url.split("?")[0]]; + } + + return fixture; + } + + __exports__.lookupFixture = lookupFixture;/* + * Removes a fixture by url. + * + * @param {String} url + */ + function removeFixture (url) { + delete __fixtures__[url]; + delete __fallbackFixtures__[url]; + } + + __exports__.removeFixture = removeFixture;/* + * Removes all fixtures. + */ + function removeAllFixtures () { + __fixtures__ = {}; + __fallbackFixtures__ = {}; } - __exports__.lookupFixture = lookupFixture;function makePromise(settings) { + __exports__.removeAllFixtures = removeAllFixtures;function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { - if (fixture.onSend) { + if (fixture.onSend && typeof fixture.onSend === "function") { fixture.onSend(settings); } if (fixture.textStatus === 'success' || fixture.textStatus == null) { From edaff2875f2215408da775abf0639f44061c5cd0 Mon Sep 17 00:00:00 2001 From: Jonathan Toms Date: Tue, 3 Mar 2015 13:46:22 -0500 Subject: [PATCH 8/9] Update Readme --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index 9397d40..340464e 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ In order to test newly added code you must rebuild the distribution. broccoli build dist ``` +### defineFixture Adding fixtures with `defineFixture` tells ic-ajax to resolve the promise with the fixture matching a url instead of making a request. This allows you to test your app without creating fake servers with sinon, etc. @@ -97,6 +98,44 @@ ic.ajax.request('api/v1/courses').then(function(result) { To test failure paths, set the `textStatus` to anything but `success`. +To set a fixture that will match every url with a matching path, regardless of the query string, add an options object as a parameter to `defineFixture` with a property of `fallback` set to true. A fixture will be located for the specific url with a query string, and if no fixture is found, the fallback that matches the path (not considering the query string) will be used. + +Example: + +```js +ic.ajax.defineFixture('api/v1/courses', { + response: [{name: 'basket weaving'}], + jqXHR: {}, + textStatus: 'success' +}, { + fallback: true +}); + +ic.ajax.request('api/v1/courses?this=that').then(function(result) { + deepEqual(result, ic.ajax.lookupFixture('api/v1/courses').response); +}); +``` + +### lookupFixture +Lookup a fixture. If successful, the fixture will be returned, otherwise `undefined` will be returned. + +```js +var coursesFixture = ic.ajax.lookupFixture('api/v1/courses'); +``` + +### removeFixture +Remove a specific fixture. Pass in the url, the fixture that matches that url, if any, will be removed. + +```js +ic.ajax.removeFixture('api/v1/courses'); +``` + +### removeAllFixtures + +```js +ic.ajax.removeAllFixtures(); +``` + Contributing ------------ From 3b649adb06ccb2996e70a2cd96162de5e242a9c3 Mon Sep 17 00:00:00 2001 From: Jonathan Toms Date: Tue, 3 Mar 2015 15:11:20 -0500 Subject: [PATCH 9/9] fix removeAllFixtures to keep the __fixture__ closure memory reference --- dist/amd/main.js | 14 +++++++++++--- dist/cjs/main.js | 14 +++++++++++--- dist/globals/main.js | 14 +++++++++++--- dist/named-amd/main.js | 14 +++++++++++--- lib/main.js | 12 ++++++++++-- 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/dist/amd/main.js b/dist/amd/main.js index e708c26..97cf1f6 100644 --- a/dist/amd/main.js +++ b/dist/amd/main.js @@ -101,11 +101,19 @@ define( * Removes all fixtures. */ function removeAllFixtures () { - __fixtures__ = {}; - __fallbackFixtures__ = {}; + emptyObject(__fixtures__); + emptyObject(__fallbackFixtures__); } - __exports__.removeAllFixtures = removeAllFixtures;function makePromise(settings) { + __exports__.removeAllFixtures = removeAllFixtures;function emptyObject(obj) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + delete obj[i]; + } + } + } + + function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { diff --git a/dist/cjs/main.js b/dist/cjs/main.js index 6665346..6906f80 100644 --- a/dist/cjs/main.js +++ b/dist/cjs/main.js @@ -98,11 +98,19 @@ exports.removeFixture = removeFixture;/* * Removes all fixtures. */ function removeAllFixtures () { - __fixtures__ = {}; - __fallbackFixtures__ = {}; + emptyObject(__fixtures__); + emptyObject(__fallbackFixtures__); } -exports.removeAllFixtures = removeAllFixtures;function makePromise(settings) { +exports.removeAllFixtures = removeAllFixtures;function emptyObject(obj) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + delete obj[i]; + } + } +} + +function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { diff --git a/dist/globals/main.js b/dist/globals/main.js index f56d499..e0c4fc4 100644 --- a/dist/globals/main.js +++ b/dist/globals/main.js @@ -99,11 +99,19 @@ exports.removeFixture = removeFixture;/* * Removes all fixtures. */ function removeAllFixtures () { - __fixtures__ = {}; - __fallbackFixtures__ = {}; + emptyObject(__fixtures__); + emptyObject(__fallbackFixtures__); } -exports.removeAllFixtures = removeAllFixtures;function makePromise(settings) { +exports.removeAllFixtures = removeAllFixtures;function emptyObject(obj) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + delete obj[i]; + } + } +} + +function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { diff --git a/dist/named-amd/main.js b/dist/named-amd/main.js index e127df2..5d69c22 100644 --- a/dist/named-amd/main.js +++ b/dist/named-amd/main.js @@ -101,11 +101,19 @@ define("ic-ajax", * Removes all fixtures. */ function removeAllFixtures () { - __fixtures__ = {}; - __fallbackFixtures__ = {}; + emptyObject(__fixtures__); + emptyObject(__fallbackFixtures__); } - __exports__.removeAllFixtures = removeAllFixtures;function makePromise(settings) { + __exports__.removeAllFixtures = removeAllFixtures;function emptyObject(obj) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + delete obj[i]; + } + } + } + + function makePromise(settings) { return new Ember.RSVP.Promise(function(resolve, reject) { var fixture = lookupFixture(settings.url); if (fixture) { diff --git a/lib/main.js b/lib/main.js index e6964b9..35ac0a0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -97,8 +97,16 @@ export function removeFixture (url) { * Removes all fixtures. */ export function removeAllFixtures () { - __fixtures__ = {}; - __fallbackFixtures__ = {}; + emptyObject(__fixtures__); + emptyObject(__fallbackFixtures__); +} + +function emptyObject(obj) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + delete obj[i]; + } + } } function makePromise(settings) {