account.controller.spec.js 14 KB

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