account.controller.spec.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. DEFAULT_ACCOUNTS = [
  52. {
  53. "_id": "560a84058812ad8d0ff200ee",
  54. "name": "foo",
  55. "reference": "baz",
  56. "user_id": "55b78934d2a706265ea28e9c"
  57. }, {
  58. "_id": "560a7ad08812ad8d0ff20068",
  59. "name": "bar",
  60. "user_id": "55b78934d2a706265ea28e9c"
  61. }
  62. ];
  63. beforeEach(module('cloudbudget'));
  64. beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) {
  65. $location = _$location_;
  66. $httpBackend = _$httpBackend_;
  67. $rootScope = _$rootScope_;
  68. $scope = _$rootScope_.$new();
  69. $scope.form = {
  70. $valid: true,
  71. $setPristine: function() {}
  72. };
  73. $timeout = _$timeout_;
  74. $filter = _$filter_;
  75. AccountService = _AccountService_;
  76. FlashService = _FlashService_;
  77. apiRoutes = _apiRoutes_;
  78. createController = function() {
  79. return $controller('AccountController', {
  80. '$scope': $scope,
  81. '$location': $location,
  82. '$rootScope': $rootScope,
  83. '$routeParams': {account_id: DEFAULT_ACCOUNT._id},
  84. FlashService: _FlashService_,
  85. AccountService: _AccountService_
  86. });
  87. };
  88. }));
  89. describe('init()', function() {
  90. it('should init successfully', inject(function($httpBackend, $rootScope) {
  91. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  92. .respond(DEFAULT_ACCOUNT);
  93. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  94. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  95. $httpBackend.expect('GET', apiRoutes.accounts )
  96. .respond(DEFAULT_ACCOUNTS);
  97. var accountController = createController();
  98. $httpBackend.flush();
  99. $timeout.flush();
  100. should.exist(accountController.account);
  101. accountController.account._id.should.be.equal(DEFAULT_ACCOUNT._id);
  102. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
  103. accountController.balance.should.be.equal(100);
  104. $rootScope.accounts.should.be.instanceof(Array).and.have.lengthOf(2);
  105. }));
  106. it('should fail to init', inject(function($httpBackend, $rootScope) {
  107. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  108. .respond(400);
  109. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  110. .respond(400);
  111. $httpBackend.expect('GET', apiRoutes.accounts )
  112. .respond(400);
  113. var accountController = createController();
  114. $httpBackend.flush();
  115. $timeout.flush();
  116. should.not.exist(accountController.account);
  117. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  118. should.not.exist(accountController.balance);
  119. should.not.exist($rootScope.accounts);
  120. }));
  121. });
  122. describe('* create()', function() {
  123. it('should create successfully', inject(function($httpBackend) {
  124. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  125. .respond(DEFAULT_ACCOUNT);
  126. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  127. .respond({entry: null, entries:[], balance: 0});
  128. $httpBackend.expect('GET', apiRoutes.accounts )
  129. .respond(DEFAULT_ACCOUNTS);
  130. var accountController = createController();
  131. $httpBackend.flush();
  132. $timeout.flush();
  133. $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  134. .respond({entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100});
  135. accountController.entry = DEFAULT_ENTRY;
  136. accountController.create();
  137. $httpBackend.flush();
  138. $timeout.flush();
  139. var entry = accountController.entries[0];
  140. entry.amount.should.be.equal(DEFAULT_ENTRY.amount);
  141. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  142. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  143. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  144. should.exist(entry._id);
  145. accountController.balance.should.be.equal(100);
  146. }));
  147. it('should fail to create entry', inject(function($httpBackend) {
  148. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  149. .respond(DEFAULT_ACCOUNT);
  150. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  151. .respond({entry: null, entries:[], balance: 0});
  152. $httpBackend.expect('GET', apiRoutes.accounts )
  153. .respond(DEFAULT_ACCOUNTS);
  154. var accountController = createController();
  155. $httpBackend.flush();
  156. $timeout.flush();
  157. $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  158. .respond(400, [{"field":"amount","rule":"required","message":"Path `amount` is required."}]);
  159. accountController.entry = {
  160. date: DEFAULT_ENTRY.date
  161. };
  162. accountController.create();
  163. $httpBackend.flush();
  164. $timeout.flush();
  165. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  166. accountController.balance.should.be.equal(0);
  167. }));
  168. });
  169. describe('* delete()', function() {
  170. it('should delete successfully', inject(function($httpBackend) {
  171. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  172. .respond(DEFAULT_ACCOUNT);
  173. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  174. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  175. $httpBackend.expect('GET', apiRoutes.accounts )
  176. .respond(DEFAULT_ACCOUNTS);
  177. var accountController = createController();
  178. $httpBackend.flush();
  179. $timeout.flush();
  180. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  181. .respond(204, {entry: null, entries:[], balance: 0});
  182. accountController.drop( accountController.entries[0] );
  183. $httpBackend.flush();
  184. $timeout.flush();
  185. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  186. accountController.balance.should.be.equal(0);
  187. }));
  188. it('should fail to delete unknown entry', inject(function($httpBackend) {
  189. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  190. .respond(DEFAULT_ACCOUNT);
  191. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  192. .respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100});
  193. $httpBackend.expect('GET', apiRoutes.accounts )
  194. .respond(DEFAULT_ACCOUNTS);
  195. var accountController = createController();
  196. $httpBackend.flush();
  197. $timeout.flush();
  198. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/fake_id')
  199. .respond(200, {entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  200. accountController.drop({_id: 'fake_id'});
  201. $httpBackend.flush();
  202. $timeout.flush();
  203. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
  204. accountController.entries[0]._id.should.be.equal(DEFAULT_ENTRY._id);
  205. accountController.balance.should.be.equal(100);
  206. }));
  207. });
  208. describe('* edit()', function() {
  209. it('should edit successfully', inject(function($httpBackend) {
  210. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  211. .respond(DEFAULT_ACCOUNT);
  212. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  213. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  214. $httpBackend.expect('GET', apiRoutes.accounts )
  215. .respond(DEFAULT_ACCOUNTS);
  216. var accountController = createController();
  217. $httpBackend.flush();
  218. $timeout.flush();
  219. var altered_entry = {
  220. "_id": "561280789f3c83904adcf41b",
  221. "account_id": "560a84058812ad8d0ff200ee",
  222. "amount": 120,
  223. "date": "2015-09-29T22:00:00.000Z",
  224. "type": "DEPOSIT",
  225. "category": "560a84058812ad8d0ff200f0",
  226. "sub_category": "560a84058812ad8d0ff200f3"
  227. };
  228. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  229. .respond({entry: altered_entry, entries:[altered_entry], balance: 120});
  230. accountController.edit(altered_entry, DEFAULT_ENTRY);
  231. $httpBackend.flush();
  232. $timeout.flush();
  233. var entry = accountController.entries[0];
  234. entry.amount.should.be.equal(altered_entry.amount);
  235. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  236. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  237. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  238. accountController.balance.should.be.equal(120);
  239. }));
  240. it('should fail to edit unknown entry', inject(function($httpBackend) {
  241. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  242. .respond(DEFAULT_ACCOUNT);
  243. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  244. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  245. $httpBackend.expect('GET', apiRoutes.accounts )
  246. .respond(DEFAULT_ACCOUNTS);
  247. var accountController = createController();
  248. $httpBackend.flush();
  249. $timeout.flush();
  250. var altered_entry = {
  251. "_id": "561280789f3c83904adcf41b",
  252. "account_id": "560a84058812ad8d0ff200ee",
  253. "amount": 120,
  254. "date": "2015-09-29T22:00:00.000Z",
  255. "type": "DEPOSIT",
  256. "category": "560a84058812ad8d0ff200f0",
  257. "sub_category": "560a84058812ad8d0ff200f3"
  258. };
  259. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  260. .respond(400, {entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100});
  261. accountController.edit(altered_entry, DEFAULT_ENTRY);
  262. $httpBackend.flush();
  263. $timeout.flush();
  264. var entry = accountController.entries[0];
  265. entry.amount.should.be.equal(DEFAULT_ENTRY.amount);
  266. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  267. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  268. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  269. accountController.balance.should.be.equal(100);
  270. }));
  271. });
  272. describe('Filters', function() {
  273. describe('* CategoryFilter', function() {
  274. it('should return empty without inputs', function() {
  275. var result = $filter('category')(undefined, DEFAULT_ACCOUNT.categories);
  276. result.should.be.equal('');
  277. });
  278. it('should return empty for invalid input', function() {
  279. var result = $filter('category')('fake_id', DEFAULT_ACCOUNT.categories);
  280. result.should.be.equal('');
  281. });
  282. it('should return label successfully', function() {
  283. var result = $filter('category')('560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  284. result.should.be.equal('Automobile Expenses');
  285. });
  286. });
  287. describe('* SubcategoryFilter', function() {
  288. it('should return empty without input', function() {
  289. var result = $filter('sub_category')(undefined, undefined, DEFAULT_ACCOUNT.categories);
  290. result.should.be.equal('');
  291. });
  292. it('should return empty without category_id', function() {
  293. var result = $filter('sub_category')('560a84058812ad8d0ff200f3', undefined, DEFAULT_ACCOUNT.categories);
  294. result.should.be.equal('');
  295. });
  296. it('should return empty without sub_category_id', function() {
  297. var result = $filter('sub_category')(undefined, '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  298. result.should.be.equal('');
  299. });
  300. it('should return empty with invalid category_id', function() {
  301. var result = $filter('sub_category')('560a84058812ad8d0ff200f3', 'fake_id', DEFAULT_ACCOUNT.categories);
  302. result.should.be.equal('');
  303. });
  304. it('should return empty with invalid sub_category_id', function() {
  305. var result = $filter('sub_category')('fake_id', '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  306. result.should.be.equal('');
  307. });
  308. it('should return label successfully', function() {
  309. var result = $filter('sub_category')('560a84058812ad8d0ff200f3', '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  310. result.should.be.equal('Car Payment');
  311. });
  312. });
  313. });
  314. describe('UI', function() {
  315. describe('* update subcategory for main form', function() {
  316. it('should return subcategory successfully', inject(function($httpBackend) {
  317. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  318. .respond(DEFAULT_ACCOUNT);
  319. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  320. .respond({entry: null, entries:[], balance: 0});
  321. $httpBackend.expect('GET', apiRoutes.accounts )
  322. .respond(DEFAULT_ACCOUNTS);
  323. var accountController = createController();
  324. $httpBackend.flush();
  325. $timeout.flush();
  326. accountController.entry = {category: '560a84058812ad8d0ff200f0'};
  327. accountController.updateSubCategory();
  328. accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(3);
  329. }));
  330. it('should return empty subcategory list successfully', inject(function($httpBackend) {
  331. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  332. .respond(DEFAULT_ACCOUNT);
  333. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  334. .respond({entry: null, entries:[], balance: 0});
  335. $httpBackend.expect('GET', apiRoutes.accounts )
  336. .respond(DEFAULT_ACCOUNTS);
  337. var accountController = createController();
  338. $httpBackend.flush();
  339. $timeout.flush();
  340. accountController.entry = {category: '560a84058812ad8d0ff200ef'};
  341. accountController.updateSubCategory();
  342. accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  343. }));
  344. it('should return empty subcategory list for unknown category', inject(function($httpBackend) {
  345. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  346. .respond(DEFAULT_ACCOUNT);
  347. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  348. .respond({entry: null, entries:[], balance: 0});
  349. $httpBackend.expect('GET', apiRoutes.accounts )
  350. .respond(DEFAULT_ACCOUNTS);
  351. var accountController = createController();
  352. $httpBackend.flush();
  353. $timeout.flush();
  354. accountController.entry = {category: 'fake_id'};
  355. accountController.updateSubCategory();
  356. accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  357. }));
  358. });
  359. describe('* update subcategory for inplace editor form', function() {
  360. it('should return subcategory successfully', inject(function($httpBackend) {
  361. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  362. .respond(DEFAULT_ACCOUNT);
  363. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  364. .respond({entry: null, entries:[], balance: 0});
  365. $httpBackend.expect('GET', apiRoutes.accounts )
  366. .respond(DEFAULT_ACCOUNTS);
  367. var accountController = createController();
  368. $httpBackend.flush();
  369. $timeout.flush();
  370. accountController.updateSubCategoryEditForm('560a84058812ad8d0ff200f0');
  371. accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(3);
  372. accountController.disabledSubCategories.should.be.false;
  373. }));
  374. it('should return empty subcategory list successfully', inject(function($httpBackend) {
  375. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  376. .respond(DEFAULT_ACCOUNT);
  377. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  378. .respond({entry: null, entries:[], balance: 0});
  379. $httpBackend.expect('GET', apiRoutes.accounts )
  380. .respond(DEFAULT_ACCOUNTS);
  381. var accountController = createController();
  382. $httpBackend.flush();
  383. $timeout.flush();
  384. accountController.updateSubCategoryEditForm('560a84058812ad8d0ff200ef');
  385. accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  386. accountController.disabledSubCategories.should.be.true;
  387. }));
  388. it('should return empty subcategory list for unknown category', inject(function($httpBackend) {
  389. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  390. .respond(DEFAULT_ACCOUNT);
  391. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  392. .respond({entry: null, entries:[], balance: 0});
  393. $httpBackend.expect('GET', apiRoutes.accounts )
  394. .respond(DEFAULT_ACCOUNTS);
  395. var accountController = createController();
  396. $httpBackend.flush();
  397. $timeout.flush();
  398. accountController.updateSubCategoryEditForm('fake_id');
  399. accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  400. accountController.disabledSubCategories.should.be.true;
  401. }));
  402. });
  403. })
  404. });