describe('AccountController', function() { var $location, $rootScope, $scope, $timeout, $httpBackend, $filter, AccountService, FlashService, createController, apiRoutes, shouldPass, DEFAULT_ACCOUNT = { "name": "test", "reference": "1234567890", "user_id": "55b78934d2a706265ea28e9c", "_id": "560aa0e79633cd7c1495ff21", "categories": [{ "key": "alimony_payments", "label": "Alimony Payments", "_id": "560a84058812ad8d0ff200ef", "sub_categories": [] }, { "key": "automobile_expenses", "label": "Automobile Expenses", "_id": "560a84058812ad8d0ff200f0", "sub_categories": [{ "label": "Car Payment", "key": "car_payment", "_id": "560a84058812ad8d0ff200f3" }, { "label": "Gasoline", "key": "gasoline", "_id": "560a84058812ad8d0ff200f2" }, { "label": "Maintenance", "key": "maintenance", "_id": "560a84058812ad8d0ff200f1" }] }] }, DEFAULT_ENTRY = { "_id": "561280789f3c83904adcf41b", "account_id": "560a84058812ad8d0ff200ee", "amount": 100, "date": "2015-09-29T22:00:00.000Z", "type": "DEPOSIT", "category": "560a84058812ad8d0ff200f0", "sub_category": "560a84058812ad8d0ff200f3" }; beforeEach(module('cloudbudget')); beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, $routeParams, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) { $location = _$location_; $httpBackend = $httpBackend; $rootScope = _$rootScope_.$new(); $scope = _$rootScope_.$new(); $scope.form = { $valid: true, $setPristine: function() {} }; $timeout = _$timeout_; $filter = _$filter_; AccountService = _AccountService_; FlashService = _FlashService_; apiRoutes = _apiRoutes_; createController = function() { return $controller('AccountController', { '$scope': $scope, '$location': $location, '$rootScope': $rootScope, '$routeParams': {account_id: DEFAULT_ACCOUNT._id}, FlashService: _FlashService_, AccountService: _AccountService_, }); }; })); describe('init()', function() { it('should init successfully', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); should.exist(accountController.account); accountController.account._id.should.be.equal(DEFAULT_ACCOUNT._id); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1); })); it('should fail to init', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(400); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond(400); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); should.not.exist(accountController.account); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0); })); }); describe('* create()', function() { it('should create successfully', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[], balance: 0}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100}); accountController.entry = DEFAULT_ENTRY; accountController.create(); $httpBackend.flush(); $timeout.flush(); var entry = accountController.entries[0]; entry.amount.should.be.equal(DEFAULT_ENTRY.amount); entry.category.should.be.equal(DEFAULT_ENTRY.category); entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category); entry.type.should.be.equal(DEFAULT_ENTRY.type); should.exist(entry._id); })); it('should fail to create entry', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[], balance: 0}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); $httpBackend.expect('POST', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond(400, [{"field":"amount","rule":"required","message":"Path `amount` is required."}]); accountController.entry = { date: DEFAULT_ENTRY.date }; accountController.create(); $httpBackend.flush(); $timeout.flush(); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0); })); }); describe('* delete()', function() { it('should delete successfully', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id) .respond(204, {entry: null, entries:[], balance: 0}); accountController.drop( accountController.entries[0] ); $httpBackend.flush(); $timeout.flush(); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0); })); it('should fail to delete unknown entry', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); $httpBackend.expect('DELETE', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/fake_id') .respond(200, {entry: null, entries:[DEFAULT_ENTRY], balance: 100}); accountController.drop({_id: 'fake_id'}); $httpBackend.flush(); $timeout.flush(); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1); accountController.entries[0]._id.should.be.equal(DEFAULT_ENTRY._id); })); }); describe('* edit()', function() { it('should edit successfully', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); var altered_entry = { "_id": "561280789f3c83904adcf41b", "account_id": "560a84058812ad8d0ff200ee", "amount": 120, "date": "2015-09-29T22:00:00.000Z", "type": "DEPOSIT", "category": "560a84058812ad8d0ff200f0", "sub_category": "560a84058812ad8d0ff200f3" }; $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id) .respond({entry: altered_entry, entries:[altered_entry], balance: 120}); accountController.edit(altered_entry, DEFAULT_ENTRY); $httpBackend.flush(); $timeout.flush(); var entry = accountController.entries[0]; entry.amount.should.be.equal(altered_entry.amount); entry.category.should.be.equal(DEFAULT_ENTRY.category); entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category); entry.type.should.be.equal(DEFAULT_ENTRY.type); })); it('should fail to edit unknown entry', inject(function($httpBackend) { $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) .respond(DEFAULT_ACCOUNT); $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); var accountController = createController(); $httpBackend.flush(); $timeout.flush(); var altered_entry = { "_id": "561280789f3c83904adcf41b", "account_id": "560a84058812ad8d0ff200ee", "amount": 120, "date": "2015-09-29T22:00:00.000Z", "type": "DEPOSIT", "category": "560a84058812ad8d0ff200f0", "sub_category": "560a84058812ad8d0ff200f3" }; $httpBackend.expect('PUT', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries/' + DEFAULT_ENTRY._id) .respond(400, {entry: DEFAULT_ENTRY, entries:[DEFAULT_ENTRY], balance: 100}); accountController.edit(altered_entry, DEFAULT_ENTRY); $httpBackend.flush(); $timeout.flush(); var entry = accountController.entries[0]; entry.amount.should.be.equal(DEFAULT_ENTRY.amount); entry.category.should.be.equal(DEFAULT_ENTRY.category); entry.sub_category.should.be.equal(DEFAULT_ENTRY.sub_category); entry.type.should.be.equal(DEFAULT_ENTRY.type); })); }); describe('Filters', function() { describe('* CategoryFilter', function() { it('should return empty without inputs', function() { var result = $filter('category')(undefined, DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return empty for invalid input', function() { var result = $filter('category')('fake_id', DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return label successfully', function() { var result = $filter('category')('560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories); result.should.be.equal('Automobile Expenses'); }); }); describe('* SubcategoryFilter', function() { it('should return empty without input', function() { var result = $filter('sub_category')(undefined, undefined, DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return empty without category_id', function() { var result = $filter('sub_category')('560a84058812ad8d0ff200f3', undefined, DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return empty without sub_category_id', function() { var result = $filter('sub_category')(undefined, '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return empty with invalid category_id', function() { var result = $filter('sub_category')('560a84058812ad8d0ff200f3', 'fake_id', DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return empty with invalid sub_category_id', function() { var result = $filter('sub_category')('fake_id', '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories); result.should.be.equal(''); }); it('should return label successfully', function() { var result = $filter('sub_category')('560a84058812ad8d0ff200f3', '560a84058812ad8d0ff200f0', DEFAULT_ACCOUNT.categories); result.should.be.equal('Car Payment'); }); }); }); });