Skip to content

Commit

Permalink
Finished integration tests for async methods. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Jan 16, 2014
1 parent 3d3f6f5 commit 9777e5b
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 33 deletions.
52 changes: 30 additions & 22 deletions dist/angular-data.js
@@ -1,3 +1,12 @@
/**
* @author Jason Dobry <jason.dobry@gmail.com>
* @file angular-data.js
* @version 0.5.0 - Homepage <http://jmdobry.github.io/angular-data/>
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/angular-data>
* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
*
* @overview Data store for Angular.js.
*/
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var indexOf = require('./indexOf');

Expand Down Expand Up @@ -1953,7 +1962,6 @@ module.exports = destroy;
var utils = require('utils'),
errors = require('errors'),
services = require('services'),
GET = require('../../http').GET,
errorPrefix = 'DS.find(resourceName, id[, options]): ';

/**
Expand Down Expand Up @@ -2023,7 +2031,7 @@ function find(resourceName, id, options) {

if (!(id in resource.completedQueries)) {
if (!(id in resource.pendingQueries)) {
promise = resource.pendingQueries[id] = GET(utils.makePath(resource.baseUrl, resource.endpoint, id), null)
promise = resource.pendingQueries[id] = _this.GET(utils.makePath(resource.baseUrl, resource.endpoint, id), null)
.then(function (data) {
// Query is no longer pending
delete resource.pendingQueries[id];
Expand All @@ -2046,11 +2054,10 @@ function find(resourceName, id, options) {

module.exports = find;

},{"../../http":34,"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],30:[function(require,module,exports){
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],30:[function(require,module,exports){
var utils = require('utils'),
errors = require('errors'),
services = require('services'),
GET = require('../../http').GET,
errorPrefix = 'DS.findAll(resourceName, params[, options]): ';

function processResults(data, resourceName, queryHash) {
Expand All @@ -2068,7 +2075,7 @@ function processResults(data, resourceName, queryHash) {
}

// Update the data store's index for this resource
resource.index = utils.toLookup(resource.collection, resource.idAttribute || services.config.idAttribute || 'id');
resource.index = utils.toLookup(resource.collection, resource.idAttribute);

// Update modified timestamp of collection
resource.collectionModified = utils.updateTimestamp(resource.collectionModified);
Expand All @@ -2090,7 +2097,7 @@ function _findAll(resourceName, params, options) {
if (!(queryHash in resource.pendingQueries)) {

// This particular query has never even been made
resource.pendingQueries[queryHash] = GET(utils.makePath(resource.baseUrl, resource.endpoint), { params: params })
resource.pendingQueries[queryHash] = _this.GET(utils.makePath(resource.baseUrl, resource.endpoint), { params: params })
.then(function (data) {
try {
return processResults.apply(_this, [data, resourceName, queryHash]);
Expand Down Expand Up @@ -2202,7 +2209,7 @@ function findAll(resourceName, params, options) {

module.exports = findAll;

},{"../../http":34,"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],31:[function(require,module,exports){
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],31:[function(require,module,exports){
module.exports = {
/**
* @doc method
Expand Down Expand Up @@ -2269,8 +2276,7 @@ module.exports = {
var utils = require('utils'),
errors = require('errors'),
services = require('services'),
PUT = require('../../http').PUT,
errorPrefix = 'DS.refresh(resourceName, id): ';
errorPrefix = 'DS.refresh(resourceName, id[, options]): ';

/**
* @doc method
Expand Down Expand Up @@ -2329,21 +2335,23 @@ function refresh(resourceName, id, options) {
if (!services.store[resourceName]) {
throw new errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!');
} else if (!utils.isString(id) && !utils.isNumber(id)) {
throw new errors.IllegalArgumentError('DS.refresh(resourceName, id): id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
throw new errors.IllegalArgumentError(errorPrefix + 'id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
} else if (!utils.isObject(options)) {
throw new errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } });
}

if (id in services.store[resourceName].index) {
return this.find(resourceName, id, true);
} else {
return false;
options.bypassCache = true;

if (id in services.store[resourceName].index) {
return this.find(resourceName, id, options);
} else {
return false;
}
}
}

module.exports = refresh;

},{"../../http":34,"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],33:[function(require,module,exports){
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],33:[function(require,module,exports){
var utils = require('utils'),
errors = require('errors'),
services = require('services'),
Expand Down Expand Up @@ -2995,9 +3003,9 @@ Resource.prototype = services.config;
* idAttribute: '_id',
* endpoint: '/documents
* baseUrl: 'http://myapp.com/api',
* validate: function (attrs, options, cb) {
* beforeDestroy: function (resourceName attrs, cb) {
* console.log('looks good to me');
* cb(null);
* cb(null, attrs);
* }
* });
* ```
Expand Down Expand Up @@ -3926,7 +3934,9 @@ function previous(resourceName, id) {

module.exports = previous;

},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],"hIh4e1":[function(require,module,exports){
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],"errors":[function(require,module,exports){
module.exports=require('hIh4e1');
},{}],"hIh4e1":[function(require,module,exports){
/**
* @doc function
* @id errors.types:UnhandledError
Expand Down Expand Up @@ -4138,8 +4148,6 @@ module.exports = {
RuntimeError: RuntimeError
};

},{}],"errors":[function(require,module,exports){
module.exports=require('hIh4e1');
},{}],52:[function(require,module,exports){
(function (window, angular, undefined) {
'use strict';
Expand Down Expand Up @@ -4296,4 +4304,4 @@ module.exports = {

},{"mout/array/contains":1,"mout/array/filter":2,"mout/array/slice":5,"mout/array/sort":6,"mout/array/toLookup":7,"mout/lang/isEmpty":12,"mout/object/deepMixIn":19,"mout/object/forOwn":21,"mout/string/makePath":23,"mout/string/upperCase":24}],"utils":[function(require,module,exports){
module.exports=require('uE/lJt');
},{}]},{},[52])
},{}]},{},[52])
4 changes: 2 additions & 2 deletions dist/angular-data.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -22,7 +22,7 @@ module.exports = function (config) {
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'dist/angular-data.js',
'test/**/*.js',
'test/integration/**/*.js',
'karma.start.js'
],

Expand Down
2 changes: 1 addition & 1 deletion src/datastore/async_methods/findAll/index.js
Expand Up @@ -18,7 +18,7 @@ function processResults(data, resourceName, queryHash) {
}

// Update the data store's index for this resource
resource.index = utils.toLookup(resource.collection, resource.idAttribute || services.config.idAttribute || 'id');
resource.index = utils.toLookup(resource.collection, resource.idAttribute);

// Update modified timestamp of collection
resource.collectionModified = utils.updateTimestamp(resource.collectionModified);
Expand Down
14 changes: 8 additions & 6 deletions src/datastore/async_methods/refresh/index.js
@@ -1,7 +1,7 @@
var utils = require('utils'),
errors = require('errors'),
services = require('services'),
errorPrefix = 'DS.refresh(resourceName, id): ';
errorPrefix = 'DS.refresh(resourceName, id[, options]): ';

/**
* @doc method
Expand Down Expand Up @@ -63,12 +63,14 @@ function refresh(resourceName, id, options) {
throw new errors.IllegalArgumentError(errorPrefix + 'id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
} else if (!utils.isObject(options)) {
throw new errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } });
}

if (id in services.store[resourceName].index) {
return this.find(resourceName, id, { bypassCache: true });
} else {
return false;
options.bypassCache = true;

if (id in services.store[resourceName].index) {
return this.find(resourceName, id, options);
} else {
return false;
}
}
}

Expand Down
Expand Up @@ -67,7 +67,7 @@ describe('DS.find(resourceName, id[, options]): ', function () {

$httpBackend.expectGET('http://test.angular-cache.com/posts/5').respond(200, p1);

// Should make a request because loadFromServer is set to true
// Should make a request because bypassCache is set to true
DS.find('post', 5, { bypassCache: true }).then(function (post) {
assert.deepEqual(post, p1);
}, function (err) {
Expand Down
62 changes: 62 additions & 0 deletions test/integration/datastore/async_methods/refresh/index.test.js
@@ -0,0 +1,62 @@
describe('DS.refresh(resourceName, id[, options]): ', function () {
var errorPrefix = 'DS.refresh(resourceName, id[, options]): ';

it('should throw an error when method pre-conditions are not met', function (done) {
assert.throws(function () {
DS.refresh('does not exist', 5);
}, DS.errors.RuntimeError, errorPrefix + 'does not exist is not a registered resource!');

angular.forEach(TYPES_EXCEPT_STRING_OR_NUMBER, function (key) {
assert.throws(function () {
DS.refresh('post', key);
}, DS.errors.IllegalArgumentError, errorPrefix + 'id: Must be a string or a number!');
});

angular.forEach(TYPES_EXCEPT_OBJECT, function (key) {
if (key) {
assert.throws(function () {
DS.refresh('post', 5, key);
}, DS.errors.IllegalArgumentError, errorPrefix + 'options: Must be an object!');
}
});

done();
});
it('should get an item from the server', function (done) {

// Should do nothing because the data isn't in the store
assert.isFalse(DS.refresh('post', 5));

assert.isUndefined(DS.get('post', 5), 'The post should not be in the store yet');

DS.inject('post', p1);
assert.deepEqual(DS.get('post', 5), p1, 'The post is now in the store');

var initialLastModified = DS.lastModified('post', 5);

$httpBackend.expectGET('http://test.angular-cache.com/posts/5').respond(200, { author: 'John', age: 31, id: 5 });

// Should refresh the item that's in the store
DS.refresh('post', 5).then(function (post) {
assert.deepEqual(post, { author: 'John', age: 31, id: 5 });
}, function (err) {
console.error(err.stack);
fail('Should not have rejected!');
});

// Should have no effect because the request is already pending
DS.refresh('post', 5).then(function (post) {
assert.deepEqual(post, { author: 'John', age: 31, id: 5 });
}, function (err) {
console.error(err.stack);
fail('Should not have rejected!');
});

$httpBackend.flush();

assert.deepEqual(DS.get('post', 5), { author: 'John', age: 31, id: 5 }, 'The post has been refreshed');
assert.notEqual(DS.lastModified('post', 5), initialLastModified);

done();
});
});

0 comments on commit 9777e5b

Please sign in to comment.