accounts.controller.spec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. describe('AccountsController', function() {
  2. var $location,
  3. $rootScope,
  4. $scope,
  5. $timeout,
  6. $httpBackend,
  7. AccountsService,
  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. };
  18. beforeEach(module('cloudbudget'));
  19. beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, _AccountsService_, _FlashService_, _apiRoutes_) {
  20. $location = _$location_;
  21. $httpBackend = $httpBackend;
  22. $rootScope = _$rootScope_.$new();
  23. $scope = _$rootScope_.$new();
  24. $scope.form = {
  25. $valid: true,
  26. $setPristine: function() {}
  27. };
  28. $timeout = _$timeout_;
  29. AccountsService = _AccountsService_;
  30. FlashService = _FlashService_;
  31. apiRoutes = _apiRoutes_;
  32. createController = function() {
  33. return $controller('AccountsController', {
  34. '$scope': $scope,
  35. '$location': $location,
  36. '$rootScope': $rootScope,
  37. FlashService: _FlashService_,
  38. AccountsService: _AccountsService_,
  39. });
  40. };
  41. }));
  42. describe('init()', function() {
  43. it('should init successfully', inject(function($httpBackend) {
  44. $httpBackend.expect('GET', apiRoutes.accounts)
  45. .respond([DEFAULT_ACCOUNT]);
  46. var accountsController = createController();
  47. $httpBackend.flush();
  48. $timeout.flush();
  49. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  50. }));
  51. it('should fail to init', inject(function($httpBackend) {
  52. $httpBackend.expect('GET', apiRoutes.accounts)
  53. .respond(400);
  54. var accountsController = createController();
  55. $httpBackend.flush();
  56. $timeout.flush();
  57. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
  58. }));
  59. });
  60. describe('* create()', function() {
  61. it('should create successfully', inject(function($httpBackend) {
  62. $httpBackend.expect('GET', apiRoutes.accounts)
  63. .respond([]);
  64. $httpBackend.expect('POST', apiRoutes.accounts)
  65. .respond(DEFAULT_ACCOUNT);
  66. var accountsController = createController();
  67. accountsController.account = {
  68. name: 'test',
  69. reference: '1234567890'
  70. };
  71. accountsController.create();
  72. $httpBackend.flush();
  73. $timeout.flush();
  74. var account = accountsController.accounts[0];
  75. account.name.should.be.equal('test');
  76. account.reference.should.be.equal('1234567890');
  77. should.exist(account._id);
  78. }));
  79. it('should fail to create account', inject(function($httpBackend) {
  80. $httpBackend.expect('GET', apiRoutes.accounts)
  81. .respond([]);
  82. $httpBackend.expect('POST', apiRoutes.accounts)
  83. .respond(400, [{"field":"name","rule":"required","message":"Path `name` is required."}]);
  84. var accountsController = createController();
  85. accountsController.account = {
  86. reference: '1234567890'
  87. };
  88. accountsController.create();
  89. $httpBackend.flush();
  90. $timeout.flush();
  91. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
  92. }));
  93. });
  94. describe('* delete()', function() {
  95. it('should delete successfully', inject(function($httpBackend) {
  96. $httpBackend.expect('GET', apiRoutes.accounts)
  97. .respond([DEFAULT_ACCOUNT]);
  98. $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  99. .respond(204);
  100. var accountsController = createController();
  101. accountsController.drop({_id: DEFAULT_ACCOUNT._id});
  102. $httpBackend.flush();
  103. $timeout.flush();
  104. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
  105. }));
  106. it('should fail to delete unknown account', inject(function($httpBackend) {
  107. $httpBackend.expect('GET', apiRoutes.accounts)
  108. .respond([DEFAULT_ACCOUNT]);
  109. $httpBackend.expect('DELETE', apiRoutes.accounts + 'fake_id')
  110. .respond(404);
  111. var accountsController = createController();
  112. accountsController.drop({_id: 'fake_id'});
  113. $httpBackend.flush();
  114. $timeout.flush();
  115. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  116. }));
  117. });
  118. describe('* edit()', function() {
  119. it('should edit successfully', inject(function($httpBackend) {
  120. $httpBackend.expect('GET', apiRoutes.accounts)
  121. .respond([DEFAULT_ACCOUNT]);
  122. $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
  123. .respond(200, {
  124. "name": "test updated",
  125. "reference": "1234567890",
  126. "user_id": "55b78934d2a706265ea28e9c",
  127. "_id": "560aa0e79633cd7c1495ff21"
  128. });
  129. var accountsController = createController();
  130. accountsController.edit({ name:"test updated"}, DEFAULT_ACCOUNT);
  131. $httpBackend.flush();
  132. $timeout.flush();
  133. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  134. var account = accountsController.accounts[0];
  135. account.name.should.be.equal('test updated');
  136. }));
  137. it('should fail to edit unknown account', inject(function($httpBackend) {
  138. $httpBackend.expect('GET', apiRoutes.accounts)
  139. .respond([DEFAULT_ACCOUNT]);
  140. $httpBackend.expect('PUT', apiRoutes.accounts + 'fake_id')
  141. .respond(404);
  142. var accountsController = createController();
  143. accountsController.edit({name:"test updated"}, {_id: 'fake_id'});
  144. $httpBackend.flush();
  145. $timeout.flush();
  146. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  147. var account = accountsController.accounts[0];
  148. account.name.should.be.equal('test');
  149. }));
  150. });
  151. describe('* consult()', function() {
  152. it('should redirect to account consultation', inject(function($httpBackend,$rootScope, $location) {
  153. $httpBackend.expect('GET', apiRoutes.accounts)
  154. .respond([DEFAULT_ACCOUNT]);
  155. $rootScope.globals.user = true;
  156. var accountsController = createController();
  157. accountsController.consult(DEFAULT_ACCOUNT);
  158. $httpBackend.flush();
  159. $timeout.flush();
  160. $location.path().should.be.equal('/account/' + DEFAULT_ACCOUNT._id);
  161. }));
  162. })
  163. });