account.controller.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. describe('AccountController', function() {
  2. var $location,
  3. $rootScope,
  4. $scope,
  5. $timeout,
  6. $httpBackend,
  7. AccountService,
  8. FlashService,
  9. createController,
  10. apiRoutes,
  11. shouldPass,
  12. DEFAULT_ACCOUNT = {
  13. "name": "test",
  14. "reference": "1234567890",
  15. "user_id": "55b78934d2a706265ea28e9c",
  16. "_id": "560aa0e79633cd7c1495ff21",
  17. "categories": [{
  18. "key": "alimony_payments",
  19. "label": "Alimony Payments",
  20. "_id": "560a84058812ad8d0ff200ef",
  21. "sub_categories": []
  22. }, {
  23. "key": "automobile_expenses",
  24. "label": "Automobile Expenses",
  25. "_id": "560a84058812ad8d0ff200f0",
  26. "sub_categories": [{
  27. "label": "Car Payment",
  28. "key": "car_payment",
  29. "_id": "560a84058812ad8d0ff200f3"
  30. }, {
  31. "label": "Gasoline",
  32. "key": "gasoline",
  33. "_id": "560a84058812ad8d0ff200f2"
  34. }, {
  35. "label": "Maintenance",
  36. "key": "maintenance",
  37. "_id": "560a84058812ad8d0ff200f1"
  38. }]
  39. }]
  40. },
  41. DEFAULT_ENTRY = {
  42. "_id": "561280789f3c83904adcf41b",
  43. "account_id": "560a84058812ad8d0ff200ee",
  44. "amount": 100,
  45. "date": "2015-09-29T22:00:00.000Z",
  46. "type": "DEPOSIT",
  47. "category": "560a84058812ad8d0ff200f0",
  48. "sub_category": "560a84058812ad8d0ff200f3"
  49. };
  50. beforeEach(module('cloudbudget'));
  51. beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, $routeParams, _$timeout_, _AccountService_, _FlashService_, _apiRoutes_) {
  52. $location = _$location_;
  53. $httpBackend = $httpBackend;
  54. $rootScope = _$rootScope_.$new();
  55. $scope = _$rootScope_.$new();
  56. $scope.form = {
  57. $valid: true,
  58. $setPristine: function() {}
  59. };
  60. $timeout = _$timeout_;
  61. AccountService = _AccountService_;
  62. FlashService = _FlashService_;
  63. apiRoutes = _apiRoutes_;
  64. createController = function() {
  65. return $controller('AccountController', {
  66. '$scope': $scope,
  67. '$location': $location,
  68. '$rootScope': $rootScope,
  69. '$routeParams': {account_id: DEFAULT_ACCOUNT._id},
  70. FlashService: _FlashService_,
  71. AccountService: _AccountService_,
  72. });
  73. };
  74. }));
  75. describe('init()', function() {
  76. it('should init successfully', inject(function($httpBackend) {
  77. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  78. .respond(DEFAULT_ACCOUNT);
  79. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  80. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  81. var accountController = createController();
  82. $httpBackend.flush();
  83. $timeout.flush();
  84. should.exist(accountController.account);
  85. accountController.account._id.should.be.equal(DEFAULT_ACCOUNT._id);
  86. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
  87. }));
  88. it('should fail to init', inject(function($httpBackend) {
  89. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  90. .respond(400);
  91. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  92. .respond(400);
  93. var accountController = createController();
  94. $httpBackend.flush();
  95. $timeout.flush();
  96. should.not.exist(accountController.account);
  97. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  98. }));
  99. });
  100. describe('* create()', function() {
  101. it('should create successfully', inject(function($httpBackend) {
  102. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  103. .respond(DEFAULT_ACCOUNT);
  104. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  105. .respond({entry: null, entries:[], balance: 0});
  106. var accountController = createController();
  107. $httpBackend.flush();
  108. $timeout.flush();
  109. $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  110. .respond({entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100});
  111. accountController.entry = DEFAULT_ENTRY;
  112. accountController.create();
  113. $httpBackend.flush();
  114. $timeout.flush();
  115. var entry = accountController.entries[0];
  116. entry.amount.should.be.equal(DEFAULT_ENTRY.amount);
  117. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  118. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  119. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  120. should.exist(entry._id);
  121. }));
  122. it('should fail to create entry', inject(function($httpBackend) {
  123. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  124. .respond(DEFAULT_ACCOUNT);
  125. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  126. .respond({entry: null, entries:[], balance: 0});
  127. var accountController = createController();
  128. $httpBackend.flush();
  129. $timeout.flush();
  130. $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  131. .respond(400, [{"field":"amount","rule":"required","message":"Path `amount` is required."}]);
  132. accountController.entry = {
  133. date: DEFAULT_ENTRY.date
  134. };
  135. accountController.create();
  136. $httpBackend.flush();
  137. $timeout.flush();
  138. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  139. }));
  140. });
  141. describe('* delete()', function() {
  142. it('should delete successfully', inject(function($httpBackend) {
  143. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  144. .respond(DEFAULT_ACCOUNT);
  145. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  146. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  147. var accountController = createController();
  148. $httpBackend.flush();
  149. $timeout.flush();
  150. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  151. .respond(204, {entry: null, entries:[], balance: 0});
  152. accountController.drop( accountController.entries[0] );
  153. $httpBackend.flush();
  154. $timeout.flush();
  155. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  156. }));
  157. it('should fail to delete unknown entry', inject(function($httpBackend) {
  158. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  159. .respond(DEFAULT_ACCOUNT);
  160. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  161. .respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100});
  162. var accountController = createController();
  163. $httpBackend.flush();
  164. $timeout.flush();
  165. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/fake_id')
  166. .respond(200, {entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  167. accountController.drop({_id: 'fake_id'});
  168. $httpBackend.flush();
  169. $timeout.flush();
  170. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
  171. accountController.entries[0]._id.should.be.equal(DEFAULT_ENTRY._id);
  172. }));
  173. });
  174. describe('* edit()', function() {
  175. it('should edit successfully', inject(function($httpBackend) {
  176. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  177. .respond(DEFAULT_ACCOUNT);
  178. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  179. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  180. var accountController = createController();
  181. $httpBackend.flush();
  182. $timeout.flush();
  183. var altered_entry = {
  184. "_id": "561280789f3c83904adcf41b",
  185. "account_id": "560a84058812ad8d0ff200ee",
  186. "amount": 120,
  187. "date": "2015-09-29T22:00:00.000Z",
  188. "type": "DEPOSIT",
  189. "category": "560a84058812ad8d0ff200f0",
  190. "sub_category": "560a84058812ad8d0ff200f3"
  191. };
  192. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  193. .respond({entry: altered_entry, entries:[altered_entry], balance: 120});
  194. accountController.edit(altered_entry, DEFAULT_ENTRY);
  195. $httpBackend.flush();
  196. $timeout.flush();
  197. var entry = accountController.entries[0];
  198. entry.amount.should.be.equal(altered_entry.amount);
  199. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  200. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  201. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  202. }));
  203. it('should fail to edit unknown entry', inject(function($httpBackend) {
  204. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  205. .respond(DEFAULT_ACCOUNT);
  206. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  207. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  208. var accountController = createController();
  209. $httpBackend.flush();
  210. $timeout.flush();
  211. var altered_entry = {
  212. "_id": "561280789f3c83904adcf41b",
  213. "account_id": "560a84058812ad8d0ff200ee",
  214. "amount": 120,
  215. "date": "2015-09-29T22:00:00.000Z",
  216. "type": "DEPOSIT",
  217. "category": "560a84058812ad8d0ff200f0",
  218. "sub_category": "560a84058812ad8d0ff200f3"
  219. };
  220. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  221. .respond(400, {entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100});
  222. accountController.edit(altered_entry, DEFAULT_ENTRY);
  223. $httpBackend.flush();
  224. $timeout.flush();
  225. var entry = accountController.entries[0];
  226. entry.amount.should.be.equal(DEFAULT_ENTRY.amount);
  227. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  228. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  229. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  230. }));
  231. });
  232. });