| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- describe('AccountsController', function() {
-
- var $location,
- $rootScope,
- $scope,
- $timeout,
- $httpBackend,
- AccountsService,
- FlashService,
- createController,
- apiRoutes,
- shouldPass,
- DEFAULT_ACCOUNT = {
- "name": "test",
- "reference": "1234567890",
- "user_id": "55b78934d2a706265ea28e9c",
- "_id": "560aa0e79633cd7c1495ff21"
- };
-
- beforeEach(module('cloudbudget'));
-
- beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, _AccountsService_, _FlashService_, _apiRoutes_) {
- $location = _$location_;
- $httpBackend = $httpBackend;
- $rootScope = _$rootScope_.$new();
- $scope = _$rootScope_.$new();
- $scope.form = {
- $valid: true,
- $setPristine: function() {}
- };
- $timeout = _$timeout_;
- AccountsService = _AccountsService_;
- FlashService = _FlashService_;
- apiRoutes = _apiRoutes_;
-
- createController = function() {
- return $controller('AccountsController', {
- '$scope': $scope,
- '$location': $location,
- '$rootScope': $rootScope,
- FlashService: _FlashService_,
- AccountsService: _AccountsService_,
- });
- };
- }));
-
- describe('init()', function() {
- it('should create successfully', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([DEFAULT_ACCOUNT]);
-
-
- var accountsController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
- }));
- });
-
- describe('* create()', function() {
- it('should create successfully', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([]);
-
- $httpBackend.expect('POST', apiRoutes.accounts)
- .respond(DEFAULT_ACCOUNT);
-
- var accountsController = createController();
- accountsController.account = {
- name: 'test',
- reference: '1234567890'
- };
-
- accountsController.create();
- $httpBackend.flush();
- $timeout.flush();
-
- var account = accountsController.accounts[0];
- account.name.should.be.equal('test');
- account.reference.should.be.equal('1234567890');
- should.exist(account._id);
- }));
-
- it('should fail to create account', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([]);
-
- $httpBackend.expect('POST', apiRoutes.accounts)
- .respond(400, [{"field":"name","rule":"required","message":"Path `name` is required."}]);
-
- var accountsController = createController();
- accountsController.account = {
- reference: '1234567890'
- };
-
- accountsController.create();
- $httpBackend.flush();
- $timeout.flush();
-
- accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
- }));
- });
-
- describe('* delete()', function() {
- it('should delete successfully', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([DEFAULT_ACCOUNT]);
-
- $httpBackend.expect('DELETE', apiRoutes.accounts + '560aa0e79633cd7c1495ff21')
- .respond(204);
-
- var accountsController = createController();
- accountsController.drop({_id: '560aa0e79633cd7c1495ff21'});
- $httpBackend.flush();
- $timeout.flush();
-
- accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(0);
- }));
-
- it('should fail to delete unknown account', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([DEFAULT_ACCOUNT]);
-
- $httpBackend.expect('DELETE', apiRoutes.accounts + 'fake_id')
- .respond(404);
-
- var accountsController = createController();
- accountsController.drop({_id: 'fake_id'});
- $httpBackend.flush();
- $timeout.flush();
-
- accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
- }));
- });
-
- describe('* edit()', function() {
- it('should edit successfully', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([DEFAULT_ACCOUNT]);
-
- $httpBackend.expect('PUT', apiRoutes.accounts + '560aa0e79633cd7c1495ff21')
- .respond(200, {
- "name": "test updated",
- "reference": "1234567890",
- "user_id": "55b78934d2a706265ea28e9c",
- "_id": "560aa0e79633cd7c1495ff21"
- });
-
- var accountsController = createController();
- accountsController.edit({ name:"test updated"}, DEFAULT_ACCOUNT);
- $httpBackend.flush();
- $timeout.flush();
-
- accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
- var account = accountsController.accounts[0];
- account.name.should.be.equal('test updated');
- }));
-
- it('should fail to edit unknown account', inject(function($controller, $httpBackend) {
- $httpBackend.expect('GET', apiRoutes.accounts)
- .respond([DEFAULT_ACCOUNT]);
-
- $httpBackend.expect('PUT', apiRoutes.accounts + 'fake_id')
- .respond(404);
-
- var accountsController = createController();
- accountsController.edit({name:"test updated"}, {_id: 'fake_id'});
- $httpBackend.flush();
- $timeout.flush();
-
- accountsController.accounts.should.be.instanceof(Array).and.have.lengthOf(1);
- var account = accountsController.accounts[0];
- account.name.should.be.equal('test');
- }));
- });
-
- });
|