Skip to content

Commit 7f4bba7

Browse files
committed
DS.defineResource now returns a mock resource object which has the mocked proxy methods.
Stable Version 0.5.3.
1 parent 9a3bd15 commit 7f4bba7

File tree

7 files changed

+231
-11
lines changed

7 files changed

+231
-11
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Jason Dobry",
33
"name": "angular-data-mocks",
44
"description": "A mock of angular-data for testing purposes.",
5-
"version": "0.5.2",
5+
"version": "0.5.3",
66
"homepage": "https://github.com/jmdobry/angular-data-mocks/",
77
"repository": {
88
"type": "git",

dist/angular-data-mocks.js

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @author Jason Dobry <[email protected]>
44
* @file angular-data-mocks.js
5-
* @version 0.5.2 - Homepage <https://github.com/jmdobry/angular-data-mocks>
5+
* @version 0.5.3 - Homepage <https://github.com/jmdobry/angular-data-mocks>
66
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
77
* @license MIT <https://github.com/jmdobry/angular-data-mocks/blob/master/LICENSE>
88
*
@@ -716,6 +716,7 @@ function DSLocalStorageAdapterProvider() {
716716
module.exports = DSLocalStorageAdapterProvider;
717717

718718
},{}],3:[function(require,module,exports){
719+
/*jshint evil:true*/
719720
function DSProvider() {
720721
var expectations = [];
721722
var definitions = [];
@@ -752,7 +753,41 @@ function DSProvider() {
752753
'previous'
753754
];
754755

755-
this.$get = ['DSMockUtils', 'DSUtils', 'DSErrors', function (DSMockUtils, DSUtils, DSErrors) {
756+
var methodsToProxy = [
757+
'bindAll',
758+
'bindOne',
759+
'create',
760+
'createInstance',
761+
'destroy',
762+
'destroyAll',
763+
'filter',
764+
'find',
765+
'findAll',
766+
'get',
767+
'hasChanges',
768+
'inject',
769+
'lastModified',
770+
'lastSaved',
771+
'loadRelations',
772+
'previous',
773+
'refresh',
774+
'save',
775+
'update',
776+
'updateAll'
777+
];
778+
779+
function Resource(utils, options) {
780+
781+
utils.deepMixIn(this, options);
782+
783+
if ('endpoint' in options) {
784+
this.endpoint = options.endpoint;
785+
} else {
786+
this.endpoint = this.name;
787+
}
788+
}
789+
790+
this.$get = ['DSMockUtils', 'DSUtils', 'DSErrors', '$log', function (DSMockUtils, DSUtils, DSErrors, $log) {
756791

757792
var MockDSExpectation = DSMockUtils.MockDSExpectation;
758793
var defaults = {
@@ -784,6 +819,8 @@ function DSProvider() {
784819
* See the [testing guide](/documentation/guide/angular-data-mocks/index).
785820
*/
786821
var DS = {
822+
store: {},
823+
definitions: {},
787824

788825
defaults: defaults,
789826

@@ -1156,6 +1193,74 @@ function DSProvider() {
11561193

11571194
angular.extend(DS, stubs);
11581195

1196+
DS.defineResource = function (definition) {
1197+
var DS = this;
1198+
1199+
if (DSUtils.isString(definition)) {
1200+
definition = definition.replace(/\s/gi, '');
1201+
definition = {
1202+
name: definition
1203+
};
1204+
}
1205+
1206+
try {
1207+
// Inherit from global defaults
1208+
Resource.prototype = DS.defaults;
1209+
DS.definitions[definition.name] = new Resource(DSUtils, definition);
1210+
1211+
var def = DS.definitions[definition.name];
1212+
1213+
// Create the wrapper class for the new resource
1214+
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
1215+
eval('function ' + def.class + '() {}');
1216+
def[def.class] = eval(def.class);
1217+
1218+
// Apply developer-defined methods
1219+
if (def.methods) {
1220+
DSUtils.deepMixIn(def[def.class].prototype, def.methods);
1221+
}
1222+
1223+
// Initialize store data for the new resource
1224+
DS.store[def.name] = {
1225+
collection: [],
1226+
completedQueries: {},
1227+
pendingQueries: {},
1228+
index: {},
1229+
modified: {},
1230+
saved: {},
1231+
previousAttributes: {},
1232+
observers: {},
1233+
collectionModified: 0
1234+
};
1235+
1236+
// Proxy DS methods with shorthand ones
1237+
angular.forEach(methodsToProxy, function (name) {
1238+
if (name === 'bindOne' || name === 'bindAll') {
1239+
def[name] = function () {
1240+
var args = Array.prototype.slice.call(arguments);
1241+
args.splice(2, 0, def.name);
1242+
return DS[name].apply(DS, args);
1243+
};
1244+
} else {
1245+
def[name] = function () {
1246+
var args = Array.prototype.slice.call(arguments);
1247+
args.unshift(def.name);
1248+
return DS[name].apply(DS, args);
1249+
};
1250+
}
1251+
});
1252+
1253+
return def;
1254+
} catch (err) {
1255+
$log.error(err);
1256+
delete this.definitions[definition.name];
1257+
delete this.store[definition.name];
1258+
throw err;
1259+
}
1260+
};
1261+
1262+
sinon.spy(DS, 'defineResource');
1263+
11591264
return DS;
11601265
}];
11611266
}
@@ -1170,7 +1275,7 @@ module.exports = DSProvider;
11701275
* @description
11711276
* Fake angular-data implementation suitable for unit testing angular applications that use the `angular-data.DS` module.
11721277
*
1173-
* __Version:__ 0.5.2
1278+
* __Version:__ 0.5.3
11741279
*
11751280
* __angular-data-mocks requires SinonJS to be loaded in order to work.__
11761281
*
@@ -1355,7 +1460,7 @@ module.exports = DSProvider;
13551460
'angular-data.DSHttpAdapterMock',
13561461
'angular-data.DSLocalStorageAdapterMock'
13571462
])
1358-
.value('version', '0.5.2');
1463+
.value('version', '0.5.3');
13591464

13601465
})(window, window.angular);
13611466

0 commit comments

Comments
 (0)