Skip to content

Commit 4a642ec

Browse files
committed
Added DS.when, DSHttpAdapter.when and DSLocalStorageAdapter.when
1 parent e9d6d2e commit 4a642ec

5 files changed

Lines changed: 345 additions & 69 deletions

File tree

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.4.1",
5+
"version": "0.5.0",
66
"homepage": "https://github.com/jmdobry/angular-data-mocks/",
77
"repository": {
88
"type": "git",

dist/angular-data-mocks.js

Lines changed: 171 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @author Jason Dobry <jason.dobry@gmail.com>
33
* @file angular-data-mocks.js
4-
* @version 0.4.1 - Homepage <https://github.com/jmdobry/angular-data-mocks>
4+
* @version 0.5.0 - Homepage <https://github.com/jmdobry/angular-data-mocks>
55
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66
* @license MIT <https://github.com/jmdobry/angular-data-mocks/blob/master/LICENSE>
77
*
@@ -153,6 +153,7 @@
153153

154154
function DSHttpAdapterProvider() {
155155
var expectations = [];
156+
var definitions = [];
156157
var requests = [];
157158
var stubs = {};
158159
var asyncMethods = [
@@ -250,6 +251,51 @@
250251
$rootScope.$digest();
251252
},
252253

254+
/**
255+
* @doc method
256+
* @id DSHttpAdapter.methods:when
257+
* @name when
258+
* @description
259+
* Create a when expectation.
260+
*
261+
* ## Signature:
262+
* ```js
263+
* DSHttpAdapter.when(methodName[, ...args])
264+
* ```
265+
*
266+
* ## Example:
267+
* ```js
268+
* DSHttpAdapter.when('GET').respond({
269+
* author: 'John Anderson',
270+
* id: 1
271+
* });
272+
*
273+
* UserController.getUser();
274+
* $scope.$apply();
275+
*
276+
* DSHttpAdapter.flush();
277+
*
278+
* assert.equal(DSHttpAdapter.find.callCount, 1);
279+
* ```
280+
*
281+
* @param {string} name The name of the function to respond to.
282+
* @returns {object} expectation
283+
*/
284+
when: function (name) {
285+
var args = Array.prototype.slice.call(arguments, 1);
286+
if (args[0] === undefined) {
287+
throw new Error('Resource not defined for definition');
288+
}
289+
var definition = new MockDSExpectation(name, args);
290+
definitions.push(definition);
291+
292+
return {
293+
respond: function () {
294+
definition.response = Array.prototype.slice.call(arguments);
295+
}
296+
};
297+
},
298+
253299
/**
254300
* @doc method
255301
* @id DSHttpAdapter.methods:verifyNoOutstandingExpectation
@@ -556,6 +602,7 @@
556602

557603
function DSLocalStorageAdapterProvider() {
558604
var expectations = [];
605+
var definitions = [];
559606
var requests = [];
560607
var stubs = {};
561608
var asyncMethods = [
@@ -645,6 +692,51 @@
645692
$rootScope.$digest();
646693
},
647694

695+
/**
696+
* @doc method
697+
* @id DSLocalStorageAdapter.methods:when
698+
* @name when
699+
* @description
700+
* Create a when expectation.
701+
*
702+
* ## Signature:
703+
* ```js
704+
* DSLocalStorageAdapter.when(methodName[, ...args])
705+
* ```
706+
*
707+
* ## Example:
708+
* ```js
709+
* DSLocalStorageAdapter.when('find', { name: 'post', idAttribute: 'id' }, 1).respond({
710+
* author: 'John Anderson',
711+
* id: 1
712+
* });
713+
*
714+
* UserController.getUser();
715+
* $scope.$apply();
716+
*
717+
* DSLocalStorageAdapter.flush();
718+
*
719+
* assert.equal(DS.find.callCount, 1);
720+
* ```
721+
*
722+
* @param {string} name The name of the function to respond to.
723+
* @returns {object} expectation
724+
*/
725+
when: function (name) {
726+
var args = Array.prototype.slice.call(arguments, 1);
727+
if (args[0] === undefined) {
728+
throw new Error('Resource not defined for definition');
729+
}
730+
var definition = new MockDSExpectation(name, args);
731+
definitions.push(definition);
732+
733+
return {
734+
respond: function () {
735+
definition.response = Array.prototype.slice.call(arguments);
736+
}
737+
};
738+
},
739+
648740
/**
649741
* @doc method
650742
* @id DSLocalStorageAdapter.methods:verifyNoOutstandingExpectation
@@ -765,38 +857,39 @@
765857
}
766858

767859
function DSProvider() {
768-
var expectations = [],
769-
requests = [],
770-
stubs = {},
771-
asyncMethods = [
772-
'create',
773-
'destroy',
774-
'destroyAll',
775-
'find',
776-
'findAll',
777-
'loadRelations',
778-
'refresh',
779-
'save',
780-
'update',
781-
'updateAll'
782-
],
783-
syncMethods = [
784-
'bindAll',
785-
'bindOne',
786-
'changes',
787-
'defineResource',
788-
'createInstance',
789-
'digest',
790-
'eject',
791-
'ejectAll',
792-
'filter',
793-
'get',
794-
'hasChanges',
795-
'inject',
796-
'lastModified',
797-
'lastSaved',
798-
'previous'
799-
];
860+
var expectations = [];
861+
var definitions = [];
862+
var requests = [];
863+
var stubs = {};
864+
var asyncMethods = [
865+
'create',
866+
'destroy',
867+
'destroyAll',
868+
'find',
869+
'findAll',
870+
'loadRelations',
871+
'refresh',
872+
'save',
873+
'update',
874+
'updateAll'
875+
];
876+
var syncMethods = [
877+
'bindAll',
878+
'bindOne',
879+
'changes',
880+
'defineResource',
881+
'createInstance',
882+
'digest',
883+
'eject',
884+
'ejectAll',
885+
'filter',
886+
'get',
887+
'hasChanges',
888+
'inject',
889+
'lastModified',
890+
'lastSaved',
891+
'previous'
892+
];
800893

801894
angular.forEach(asyncMethods, function (name) {
802895
stubs[name] = mockAsync(name, 'DS', expectations, requests);
@@ -922,6 +1015,51 @@
9221015
$rootScope.$digest();
9231016
},
9241017

1018+
/**
1019+
* @doc method
1020+
* @id DS.methods:when
1021+
* @name when
1022+
* @description
1023+
* Create a when expectation.
1024+
*
1025+
* ## Signature:
1026+
* ```js
1027+
* DS.when(methodName[, ...args])
1028+
* ```
1029+
*
1030+
* ## Example:
1031+
* ```js
1032+
* DS.when('find', 'post', 1).respond({
1033+
* author: 'John Anderson',
1034+
* id: 1
1035+
* });
1036+
*
1037+
* UserController.getUser();
1038+
* $scope.$apply();
1039+
*
1040+
* DS.flush();
1041+
*
1042+
* assert.equal(DS.find.callCount, 1);
1043+
* ```
1044+
*
1045+
* @param {string} name The name of the function to respond to.
1046+
* @returns {object} expectation
1047+
*/
1048+
when: function (name) {
1049+
var args = Array.prototype.slice.call(arguments, 1);
1050+
if (args[0] === undefined) {
1051+
throw new Error('Resource not defined for definition');
1052+
}
1053+
var definition = new MockDSExpectation(name, args);
1054+
definitions.push(definition);
1055+
1056+
return {
1057+
respond: function () {
1058+
definition.response = Array.prototype.slice.call(arguments);
1059+
}
1060+
};
1061+
},
1062+
9251063
/**
9261064
* @doc method
9271065
* @id DS.methods:verifyNoOutstandingExpectation

dist/angular-data-mocks.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-data-mocks",
33
"description": "A mock of angular-data for testing purposes.",
4-
"version": "0.4.1",
4+
"version": "0.5.0",
55
"homepage": "https://github.com/jmdobry/angular-data-mocks",
66
"repository": {
77
"type": "git",

0 commit comments

Comments
 (0)