|
1 | 1 | /** |
2 | 2 | * @author Jason Dobry <jason.dobry@gmail.com> |
3 | 3 | * @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> |
5 | 5 | * @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/> |
6 | 6 | * @license MIT <https://github.com/jmdobry/angular-data-mocks/blob/master/LICENSE> |
7 | 7 | * |
|
153 | 153 |
|
154 | 154 | function DSHttpAdapterProvider() { |
155 | 155 | var expectations = []; |
| 156 | + var definitions = []; |
156 | 157 | var requests = []; |
157 | 158 | var stubs = {}; |
158 | 159 | var asyncMethods = [ |
|
250 | 251 | $rootScope.$digest(); |
251 | 252 | }, |
252 | 253 |
|
| 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 | + |
253 | 299 | /** |
254 | 300 | * @doc method |
255 | 301 | * @id DSHttpAdapter.methods:verifyNoOutstandingExpectation |
|
556 | 602 |
|
557 | 603 | function DSLocalStorageAdapterProvider() { |
558 | 604 | var expectations = []; |
| 605 | + var definitions = []; |
559 | 606 | var requests = []; |
560 | 607 | var stubs = {}; |
561 | 608 | var asyncMethods = [ |
|
645 | 692 | $rootScope.$digest(); |
646 | 693 | }, |
647 | 694 |
|
| 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 | + |
648 | 740 | /** |
649 | 741 | * @doc method |
650 | 742 | * @id DSLocalStorageAdapter.methods:verifyNoOutstandingExpectation |
|
765 | 857 | } |
766 | 858 |
|
767 | 859 | 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 | + ]; |
800 | 893 |
|
801 | 894 | angular.forEach(asyncMethods, function (name) { |
802 | 895 | stubs[name] = mockAsync(name, 'DS', expectations, requests); |
|
922 | 1015 | $rootScope.$digest(); |
923 | 1016 | }, |
924 | 1017 |
|
| 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 | + |
925 | 1063 | /** |
926 | 1064 | * @doc method |
927 | 1065 | * @id DS.methods:verifyNoOutstandingExpectation |
|
0 commit comments