Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
------------
Expand Down
52 changes: 48 additions & 4 deletions dist/amd/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;/*
Expand All @@ -69,13 +78,48 @@ 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__.lookupFixture = lookupFixture;function makePromise(settings) {
__exports__.removeFixture = removeFixture;/*
* Removes all fixtures.
*/
function removeAllFixtures () {
emptyObject(__fixtures__);
emptyObject(__fallbackFixtures__);
}

__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) {
if (fixture.onSend && typeof fixture.onSend === "function") {
fixture.onSend(settings);
}
if (fixture.textStatus === 'success' || fixture.textStatus == null) {
return Ember.run.later(null, resolve, fixture);
} else {
Expand Down
52 changes: 48 additions & 4 deletions dist/cjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;/*
Expand All @@ -66,13 +75,48 @@ 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.lookupFixture = lookupFixture;function makePromise(settings) {
exports.removeFixture = removeFixture;/*
* Removes all fixtures.
*/
function removeAllFixtures () {
emptyObject(__fixtures__);
emptyObject(__fallbackFixtures__);
}

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) {
if (fixture.onSend && typeof fixture.onSend === "function") {
fixture.onSend(settings);
}
if (fixture.textStatus === 'success' || fixture.textStatus == null) {
return Ember.run.later(null, resolve, fixture);
} else {
Expand Down
52 changes: 48 additions & 4 deletions dist/globals/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;/*
Expand All @@ -67,13 +76,48 @@ 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.lookupFixture = lookupFixture;function makePromise(settings) {
exports.removeFixture = removeFixture;/*
* Removes all fixtures.
*/
function removeAllFixtures () {
emptyObject(__fixtures__);
emptyObject(__fallbackFixtures__);
}

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) {
if (fixture.onSend && typeof fixture.onSend === "function") {
fixture.onSend(settings);
}
if (fixture.textStatus === 'success' || fixture.textStatus == null) {
return Ember.run.later(null, resolve, fixture);
} else {
Expand Down
Loading