account.controller.spec.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. accountController.balance.should.be.equal(100);
  90. }));
  91. it('should fail to init', inject(function($httpBackend) {
  92. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  93. .respond(400);
  94. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  95. .respond(400);
  96. var accountController = createController();
  97. $httpBackend.flush();
  98. $timeout.flush();
  99. should.not.exist(accountController.account);
  100. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  101. should.not.exist(accountController.balance);
  102. }));
  103. });
  104. describe('* create()', function() {
  105. it('should create successfully', inject(function($httpBackend) {
  106. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  107. .respond(DEFAULT_ACCOUNT);
  108. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  109. .respond({entry: null, entries:[], balance: 0});
  110. var accountController = createController();
  111. $httpBackend.flush();
  112. $timeout.flush();
  113. $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  114. .respond({entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100});
  115. accountController.entry = DEFAULT_ENTRY;
  116. accountController.create();
  117. $httpBackend.flush();
  118. $timeout.flush();
  119. var entry = accountController.entries[0];
  120. entry.amount.should.be.equal(DEFAULT_ENTRY.amount);
  121. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  122. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  123. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  124. should.exist(entry._id);
  125. accountController.balance.should.be.equal(100);
  126. }));
  127. it('should fail to create entry', inject(function($httpBackend) {
  128. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  129. .respond(DEFAULT_ACCOUNT);
  130. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  131. .respond({entry: null, entries:[], balance: 0});
  132. var accountController = createController();
  133. $httpBackend.flush();
  134. $timeout.flush();
  135. $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  136. .respond(400, [{"field":"amount","rule":"required","message":"Path `amount` is required."}]);
  137. accountController.entry = {
  138. date: DEFAULT_ENTRY.date
  139. };
  140. accountController.create();
  141. $httpBackend.flush();
  142. $timeout.flush();
  143. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  144. accountController.balance.should.be.equal(0);
  145. }));
  146. });
  147. describe('* delete()', function() {
  148. it('should delete successfully', inject(function($httpBackend) {
  149. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  150. .respond(DEFAULT_ACCOUNT);
  151. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  152. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  153. var accountController = createController();
  154. $httpBackend.flush();
  155. $timeout.flush();
  156. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  157. .respond(204, {entry: null, entries:[], balance: 0});
  158. accountController.drop( accountController.entries[0] );
  159. $httpBackend.flush();
  160. $timeout.flush();
  161. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
  162. accountController.balance.should.be.equal(0);
  163. }));
  164. it('should fail to delete unknown entry', inject(function($httpBackend) {
  165. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  166. .respond(DEFAULT_ACCOUNT);
  167. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  168. .respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100});
  169. var accountController = createController();
  170. $httpBackend.flush();
  171. $timeout.flush();
  172. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/fake_id')
  173. .respond(200, {entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  174. accountController.drop({_id: 'fake_id'});
  175. $httpBackend.flush();
  176. $timeout.flush();
  177. accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
  178. accountController.entries[0]._id.should.be.equal(DEFAULT_ENTRY._id);
  179. accountController.balance.should.be.equal(100);
  180. }));
  181. });
  182. describe('* edit()', function() {
  183. it('should edit successfully', inject(function($httpBackend) {
  184. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  185. .respond(DEFAULT_ACCOUNT);
  186. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  187. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  188. var accountController = createController();
  189. $httpBackend.flush();
  190. $timeout.flush();
  191. var altered_entry = {
  192. "_id": "561280789f3c83904adcf41b",
  193. "account_id": "560a84058812ad8d0ff200ee",
  194. "amount": 120,
  195. "date": "2015-09-29T22:00:00.000Z",
  196. "type": "DEPOSIT",
  197. "category": "560a84058812ad8d0ff200f0",
  198. "sub_category": "560a84058812ad8d0ff200f3"
  199. };
  200. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  201. .respond({entry: altered_entry, entries:[altered_entry], balance: 120});
  202. accountController.edit(altered_entry, DEFAULT_ENTRY);
  203. $httpBackend.flush();
  204. $timeout.flush();
  205. var entry = accountController.entries[0];
  206. entry.amount.should.be.equal(altered_entry.amount);
  207. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  208. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  209. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  210. accountController.balance.should.be.equal(120);
  211. }));
  212. it('should fail to edit unknown entry', inject(function($httpBackend) {
  213. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  214. .respond(DEFAULT_ACCOUNT);
  215. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  216. .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
  217. var accountController = createController();
  218. $httpBackend.flush();
  219. $timeout.flush();
  220. var altered_entry = {
  221. "_id": "561280789f3c83904adcf41b",
  222. "account_id": "560a84058812ad8d0ff200ee",
  223. "amount": 120,
  224. "date": "2015-09-29T22:00:00.000Z",
  225. "type": "DEPOSIT",
  226. "category": "560a84058812ad8d0ff200f0",
  227. "sub_category": "560a84058812ad8d0ff200f3"
  228. };
  229. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id)
  230. .respond(400, {entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100});
  231. accountController.edit(altered_entry, DEFAULT_ENTRY);
  232. $httpBackend.flush();
  233. $timeout.flush();
  234. var entry = accountController.entries[0];
  235. entry.amount.should.be.equal(DEFAULT_ENTRY.amount);
  236. entry.category.should.be.equal(DEFAULT_ENTRY.category);
  237. entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category);
  238. entry.type.should.be.equal(DEFAULT_ENTRY.type);
  239. accountController.balance.should.be.equal(100);
  240. }));
  241. });
  242. describe('Filters', function() {
  243. describe('* CategoryFilter', function() {
  244. it('should return empty without inputs', function() {
  245. var result = $filter('category')(undefined, DEFAULT_ACCOUNT.categories);
  246. result.should.be.equal('');
  247. });
  248. it('should return empty for invalid input', function() {
  249. var result = $filter('category')('fake_id', DEFAULT_ACCOUNT.categories);
  250. result.should.be.equal('');
  251. });
  252. it('should return label successfully', function() {
  253. var result = $filter('category')('560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  254. result.should.be.equal('Automobile Expenses');
  255. });
  256. });
  257. describe('* SubcategoryFilter', function() {
  258. it('should return empty without input', function() {
  259. var result = $filter('sub_category')(undefined, undefined, DEFAULT_ACCOUNT.categories);
  260. result.should.be.equal('');
  261. });
  262. it('should return empty without category_id', function() {
  263. var result = $filter('sub_category')('560a84058812ad8d0ff200f3', undefined, DEFAULT_ACCOUNT.categories);
  264. result.should.be.equal('');
  265. });
  266. it('should return empty without sub_category_id', function() {
  267. var result = $filter('sub_category')(undefined, '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  268. result.should.be.equal('');
  269. });
  270. it('should return empty with invalid category_id', function() {
  271. var result = $filter('sub_category')('560a84058812ad8d0ff200f3', 'fake_id', DEFAULT_ACCOUNT.categories);
  272. result.should.be.equal('');
  273. });
  274. it('should return empty with invalid sub_category_id', function() {
  275. var result = $filter('sub_category')('fake_id', '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  276. result.should.be.equal('');
  277. });
  278. it('should return label successfully', function() {
  279. var result = $filter('sub_category')('560a84058812ad8d0ff200f3', '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories);
  280. result.should.be.equal('Car Payment');
  281. });
  282. });
  283. });
  284. describe('UI', function() {
  285. describe('* update subcategory for main form', function() {
  286. it('should return subcategory successfully', inject(function($httpBackend) {
  287. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  288. .respond(DEFAULT_ACCOUNT);
  289. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  290. .respond({entry: null, entries:[], balance: 0});
  291. var accountController = createController();
  292. $httpBackend.flush();
  293. $timeout.flush();
  294. accountController.entry = {category: '560a84058812ad8d0ff200f0'};
  295. accountController.updateSubCategory();
  296. accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(3);
  297. }));
  298. it('should return empty subcategory list successfully', inject(function($httpBackend) {
  299. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  300. .respond(DEFAULT_ACCOUNT);
  301. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  302. .respond({entry: null, entries:[], balance: 0});
  303. var accountController = createController();
  304. $httpBackend.flush();
  305. $timeout.flush();
  306. accountController.entry = {category: '560a84058812ad8d0ff200ef'};
  307. accountController.updateSubCategory();
  308. accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  309. }));
  310. it('should return empty subcategory list for unknown category', inject(function($httpBackend) {
  311. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  312. .respond(DEFAULT_ACCOUNT);
  313. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  314. .respond({entry: null, entries:[], balance: 0});
  315. var accountController = createController();
  316. $httpBackend.flush();
  317. $timeout.flush();
  318. accountController.entry = {category: 'fake_id'};
  319. accountController.updateSubCategory();
  320. accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  321. }));
  322. });
  323. describe('* update subcategory for inplace editor form', function() {
  324. it('should return subcategory successfully', inject(function($httpBackend) {
  325. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  326. .respond(DEFAULT_ACCOUNT);
  327. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  328. .respond({entry: null, entries:[], balance: 0});
  329. var accountController = createController();
  330. $httpBackend.flush();
  331. $timeout.flush();
  332. accountController.updateSubCategoryEditForm('560a84058812ad8d0ff200f0');
  333. accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(3);
  334. accountController.disabledSubCategories.should.be.false;
  335. }));
  336. it('should return empty subcategory list successfully', inject(function($httpBackend) {
  337. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  338. .respond(DEFAULT_ACCOUNT);
  339. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  340. .respond({entry: null, entries:[], balance: 0});
  341. var accountController = createController();
  342. $httpBackend.flush();
  343. $timeout.flush();
  344. accountController.updateSubCategoryEditForm('560a84058812ad8d0ff200ef');
  345. accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  346. accountController.disabledSubCategories.should.be.true;
  347. }));
  348. it('should return empty subcategory list for unknown category', inject(function($httpBackend) {
  349. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  350. .respond(DEFAULT_ACCOUNT);
  351. $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
  352. .respond({entry: null, entries:[], balance: 0});
  353. var accountController = createController();
  354. $httpBackend.flush();
  355. $timeout.flush();
  356. accountController.updateSubCategoryEditForm('fake_id');
  357. accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
  358. accountController.disabledSubCategories.should.be.true;
  359. }));
  360. });
  361. })
  362. });