2
2
/**
3
3
* @author Jason Dobry <[email protected] >
4
4
* @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>
6
6
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
7
7
* @license MIT <https://github.com/jmdobry/angular-data-mocks/blob/master/LICENSE>
8
8
*
@@ -716,6 +716,7 @@ function DSLocalStorageAdapterProvider() {
716
716
module . exports = DSLocalStorageAdapterProvider ;
717
717
718
718
} , { } ] , 3 :[ function ( require , module , exports ) {
719
+ /*jshint evil:true*/
719
720
function DSProvider ( ) {
720
721
var expectations = [ ] ;
721
722
var definitions = [ ] ;
@@ -752,7 +753,41 @@ function DSProvider() {
752
753
'previous'
753
754
] ;
754
755
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 ) {
756
791
757
792
var MockDSExpectation = DSMockUtils . MockDSExpectation ;
758
793
var defaults = {
@@ -784,6 +819,8 @@ function DSProvider() {
784
819
* See the [testing guide](/documentation/guide/angular-data-mocks/index).
785
820
*/
786
821
var DS = {
822
+ store : { } ,
823
+ definitions : { } ,
787
824
788
825
defaults : defaults ,
789
826
@@ -1156,6 +1193,74 @@ function DSProvider() {
1156
1193
1157
1194
angular . extend ( DS , stubs ) ;
1158
1195
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
+
1159
1264
return DS ;
1160
1265
} ] ;
1161
1266
}
@@ -1170,7 +1275,7 @@ module.exports = DSProvider;
1170
1275
* @description
1171
1276
* Fake angular-data implementation suitable for unit testing angular applications that use the `angular-data.DS` module.
1172
1277
*
1173
- * __Version:__ 0.5.2
1278
+ * __Version:__ 0.5.3
1174
1279
*
1175
1280
* __angular-data-mocks requires SinonJS to be loaded in order to work.__
1176
1281
*
@@ -1355,7 +1460,7 @@ module.exports = DSProvider;
1355
1460
'angular-data.DSHttpAdapterMock' ,
1356
1461
'angular-data.DSLocalStorageAdapterMock'
1357
1462
] )
1358
- . value ( 'version' , '0.5.2 ' ) ;
1463
+ . value ( 'version' , '0.5.3 ' ) ;
1359
1464
1360
1465
} ) ( window , window . angular ) ;
1361
1466
0 commit comments