accounts.controller.spec.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 create successfully', inject(function($controller, $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. });
  52. describe('* create()', function() {
  53. it('should create successfully', inject(function($controller, $httpBackend) {
  54. $httpBackend.expect('GET', apiRoutes.accounts)
  55. .respond([]);
  56. $httpBackend.expect('POST', apiRoutes.accounts)
  57. .respond(DEFAULT_ACCOUNT);
  58. var accountsController = createController();
  59. accountsController.account = {
  60. name: 'test',
  61. reference: '1234567890'
  62. };
  63. accountsController.create();
  64. $httpBackend.flush();
  65. $timeout.flush();
  66. var account = accountsController.accounts[0];
  67. account.name.should.be.equal('test');
  68. account.reference.should.be.equal('1234567890');
  69. should.exist(account._id);
  70. }));
  71. it('should fail to create account', inject(function($controller, $httpBackend) {
  72. $httpBackend.expect('GET', apiRoutes.accounts)
  73. .respond([]);
  74. $httpBackend.expect('POST', apiRoutes.accounts)
  75. .respond(400, [{"field":"name","rule":"required","message":"Path `name` is required."}]);
  76. var accountsController = createController();
  77. accountsController.account = {
  78. reference: '1234567890'
  79. };
  80. accountsController.create();
  81. $httpBackend.flush();
  82. $timeout.flush();
  83. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
  84. }));
  85. });
  86. describe('* delete()', function() {
  87. it('should delete successfully', inject(function($controller, $httpBackend) {
  88. $httpBackend.expect('GET', apiRoutes.accounts)
  89. .respond([DEFAULT_ACCOUNT]);
  90. $httpBackend.expect('DELETE', apiRoutes.accounts + '560aa0e79633cd7c1495ff21')
  91. .respond(204);
  92. var accountsController = createController();
  93. accountsController.drop({_id: '560aa0e79633cd7c1495ff21'});
  94. $httpBackend.flush();
  95. $timeout.flush();
  96. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
  97. }));
  98. it('should fail to delete unknown account', inject(function($controller, $httpBackend) {
  99. $httpBackend.expect('GET', apiRoutes.accounts)
  100. .respond([DEFAULT_ACCOUNT]);
  101. $httpBackend.expect('DELETE', apiRoutes.accounts + 'fake_id')
  102. .respond(404);
  103. var accountsController = createController();
  104. accountsController.drop({_id: 'fake_id'});
  105. $httpBackend.flush();
  106. $timeout.flush();
  107. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  108. }));
  109. });
  110. describe('* edit()', function() {
  111. it('should edit successfully', inject(function($controller, $httpBackend) {
  112. $httpBackend.expect('GET', apiRoutes.accounts)
  113. .respond([DEFAULT_ACCOUNT]);
  114. $httpBackend.expect('PUT', apiRoutes.accounts + '560aa0e79633cd7c1495ff21')
  115. .respond(200, {
  116. "name": "test updated",
  117. "reference": "1234567890",
  118. "user_id": "55b78934d2a706265ea28e9c",
  119. "_id": "560aa0e79633cd7c1495ff21"
  120. });
  121. var accountsController = createController();
  122. accountsController.edit({ name:"test updated"}, DEFAULT_ACCOUNT);
  123. $httpBackend.flush();
  124. $timeout.flush();
  125. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  126. var account = accountsController.accounts[0];
  127. account.name.should.be.equal('test updated');
  128. }));
  129. it('should fail to edit unknown account', inject(function($controller, $httpBackend) {
  130. $httpBackend.expect('GET', apiRoutes.accounts)
  131. .respond([DEFAULT_ACCOUNT]);
  132. $httpBackend.expect('PUT', apiRoutes.accounts + 'fake_id')
  133. .respond(404);
  134. var accountsController = createController();
  135. accountsController.edit({name:"test updated"}, {_id: 'fake_id'});
  136. $httpBackend.flush();
  137. $timeout.flush();
  138. accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
  139. var account = accountsController.accounts[0];
  140. account.name.should.be.equal('test');
  141. }));
  142. });
  143. });