Skip to content

Commit 9777e5b

Browse files
committed
Finished integration tests for async methods. #15
1 parent 3d3f6f5 commit 9777e5b

File tree

12 files changed

+105
-33
lines changed

12 files changed

+105
-33
lines changed

dist/angular-data.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* @author Jason Dobry <[email protected]>
3+
* @file angular-data.js
4+
* @version 0.5.0 - Homepage <http://jmdobry.github.io/angular-data/>
5+
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/angular-data>
6+
* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
7+
*
8+
* @overview Data store for Angular.js.
9+
*/
110
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){
211
var indexOf = require('./indexOf');
312

@@ -1953,7 +1962,6 @@ module.exports = destroy;
19531962
var utils = require('utils'),
19541963
errors = require('errors'),
19551964
services = require('services'),
1956-
GET = require('../../http').GET,
19571965
errorPrefix = 'DS.find(resourceName, id[, options]): ';
19581966

19591967
/**
@@ -2023,7 +2031,7 @@ function find(resourceName, id, options) {
20232031

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

20472055
module.exports = find;
20482056

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

20562063
function processResults(data, resourceName, queryHash) {
@@ -2068,7 +2075,7 @@ function processResults(data, resourceName, queryHash) {
20682075
}
20692076

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

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

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

22032210
module.exports = findAll;
22042211

2205-
},{"../../http":34,"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],31:[function(require,module,exports){
2212+
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],31:[function(require,module,exports){
22062213
module.exports = {
22072214
/**
22082215
* @doc method
@@ -2269,8 +2276,7 @@ module.exports = {
22692276
var utils = require('utils'),
22702277
errors = require('errors'),
22712278
services = require('services'),
2272-
PUT = require('../../http').PUT,
2273-
errorPrefix = 'DS.refresh(resourceName, id): ';
2279+
errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
22742280

22752281
/**
22762282
* @doc method
@@ -2329,21 +2335,23 @@ function refresh(resourceName, id, options) {
23292335
if (!services.store[resourceName]) {
23302336
throw new errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!');
23312337
} else if (!utils.isString(id) && !utils.isNumber(id)) {
2332-
throw new errors.IllegalArgumentError('DS.refresh(resourceName, id): id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
2338+
throw new errors.IllegalArgumentError(errorPrefix + 'id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
23332339
} else if (!utils.isObject(options)) {
23342340
throw new errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } });
2335-
}
2336-
2337-
if (id in services.store[resourceName].index) {
2338-
return this.find(resourceName, id, true);
23392341
} else {
2340-
return false;
2342+
options.bypassCache = true;
2343+
2344+
if (id in services.store[resourceName].index) {
2345+
return this.find(resourceName, id, options);
2346+
} else {
2347+
return false;
2348+
}
23412349
}
23422350
}
23432351

23442352
module.exports = refresh;
23452353

2346-
},{"../../http":34,"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],33:[function(require,module,exports){
2354+
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],33:[function(require,module,exports){
23472355
var utils = require('utils'),
23482356
errors = require('errors'),
23492357
services = require('services'),
@@ -2995,9 +3003,9 @@ Resource.prototype = services.config;
29953003
* idAttribute: '_id',
29963004
* endpoint: '/documents
29973005
* baseUrl: 'http://myapp.com/api',
2998-
* validate: function (attrs, options, cb) {
3006+
* beforeDestroy: function (resourceName attrs, cb) {
29993007
* console.log('looks good to me');
3000-
* cb(null);
3008+
* cb(null, attrs);
30013009
* }
30023010
* });
30033011
* ```
@@ -3926,7 +3934,9 @@ function previous(resourceName, id) {
39263934

39273935
module.exports = previous;
39283936

3929-
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],"hIh4e1":[function(require,module,exports){
3937+
},{"errors":"hIh4e1","services":"cX8q+p","utils":"uE/lJt"}],"errors":[function(require,module,exports){
3938+
module.exports=require('hIh4e1');
3939+
},{}],"hIh4e1":[function(require,module,exports){
39303940
/**
39313941
* @doc function
39323942
* @id errors.types:UnhandledError
@@ -4138,8 +4148,6 @@ module.exports = {
41384148
RuntimeError: RuntimeError
41394149
};
41404150

4141-
},{}],"errors":[function(require,module,exports){
4142-
module.exports=require('hIh4e1');
41434151
},{}],52:[function(require,module,exports){
41444152
(function (window, angular, undefined) {
41454153
'use strict';
@@ -4296,4 +4304,4 @@ module.exports = {
42964304

42974305
},{"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){
42984306
module.exports=require('uE/lJt');
4299-
},{}]},{},[52])
4307+
},{}]},{},[52])

dist/angular-data.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

karma.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function (config) {
2222
'bower_components/angular/angular.js',
2323
'bower_components/angular-mocks/angular-mocks.js',
2424
'dist/angular-data.js',
25-
'test/**/*.js',
25+
'test/integration/**/*.js',
2626
'karma.start.js'
2727
],
2828

src/datastore/async_methods/findAll/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function processResults(data, resourceName, queryHash) {
1818
}
1919

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

2323
// Update modified timestamp of collection
2424
resource.collectionModified = utils.updateTimestamp(resource.collectionModified);

src/datastore/async_methods/refresh/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var utils = require('utils'),
22
errors = require('errors'),
33
services = require('services'),
4-
errorPrefix = 'DS.refresh(resourceName, id): ';
4+
errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
55

66
/**
77
* @doc method
@@ -63,12 +63,14 @@ function refresh(resourceName, id, options) {
6363
throw new errors.IllegalArgumentError(errorPrefix + 'id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
6464
} else if (!utils.isObject(options)) {
6565
throw new errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } });
66-
}
67-
68-
if (id in services.store[resourceName].index) {
69-
return this.find(resourceName, id, { bypassCache: true });
7066
} else {
71-
return false;
67+
options.bypassCache = true;
68+
69+
if (id in services.store[resourceName].index) {
70+
return this.find(resourceName, id, options);
71+
} else {
72+
return false;
73+
}
7274
}
7375
}
7476

test/unit/datastore/async_methods/find/index.test.js renamed to test/integration/datastore/async_methods/find/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('DS.find(resourceName, id[, options]): ', function () {
6767

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

70-
// Should make a request because loadFromServer is set to true
70+
// Should make a request because bypassCache is set to true
7171
DS.find('post', 5, { bypassCache: true }).then(function (post) {
7272
assert.deepEqual(post, p1);
7373
}, function (err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
describe('DS.refresh(resourceName, id[, options]): ', function () {
2+
var errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
3+
4+
it('should throw an error when method pre-conditions are not met', function (done) {
5+
assert.throws(function () {
6+
DS.refresh('does not exist', 5);
7+
}, DS.errors.RuntimeError, errorPrefix + 'does not exist is not a registered resource!');
8+
9+
angular.forEach(TYPES_EXCEPT_STRING_OR_NUMBER, function (key) {
10+
assert.throws(function () {
11+
DS.refresh('post', key);
12+
}, DS.errors.IllegalArgumentError, errorPrefix + 'id: Must be a string or a number!');
13+
});
14+
15+
angular.forEach(TYPES_EXCEPT_OBJECT, function (key) {
16+
if (key) {
17+
assert.throws(function () {
18+
DS.refresh('post', 5, key);
19+
}, DS.errors.IllegalArgumentError, errorPrefix + 'options: Must be an object!');
20+
}
21+
});
22+
23+
done();
24+
});
25+
it('should get an item from the server', function (done) {
26+
27+
// Should do nothing because the data isn't in the store
28+
assert.isFalse(DS.refresh('post', 5));
29+
30+
assert.isUndefined(DS.get('post', 5), 'The post should not be in the store yet');
31+
32+
DS.inject('post', p1);
33+
assert.deepEqual(DS.get('post', 5), p1, 'The post is now in the store');
34+
35+
var initialLastModified = DS.lastModified('post', 5);
36+
37+
$httpBackend.expectGET('http://test.angular-cache.com/posts/5').respond(200, { author: 'John', age: 31, id: 5 });
38+
39+
// Should refresh the item that's in the store
40+
DS.refresh('post', 5).then(function (post) {
41+
assert.deepEqual(post, { author: 'John', age: 31, id: 5 });
42+
}, function (err) {
43+
console.error(err.stack);
44+
fail('Should not have rejected!');
45+
});
46+
47+
// Should have no effect because the request is already pending
48+
DS.refresh('post', 5).then(function (post) {
49+
assert.deepEqual(post, { author: 'John', age: 31, id: 5 });
50+
}, function (err) {
51+
console.error(err.stack);
52+
fail('Should not have rejected!');
53+
});
54+
55+
$httpBackend.flush();
56+
57+
assert.deepEqual(DS.get('post', 5), { author: 'John', age: 31, id: 5 }, 'The post has been refreshed');
58+
assert.notEqual(DS.lastModified('post', 5), initialLastModified);
59+
60+
done();
61+
});
62+
});

0 commit comments

Comments
 (0)