| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- 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"
- },
- DEFAULT_ACCOUNTS = [
- {
- "_id": "560a84058812ad8d0ff200ee",
- "name": "foo",
- "reference": "baz",
- "user_id": "55b78934d2a706265ea28e9c"
- }, {
- "_id": "560a7ad08812ad8d0ff20068",
- "name": "bar",
- "user_id": "55b78934d2a706265ea28e9c"
- }
- ];
-
- beforeEach(module('cloudbudget'));
-
- beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) {
- $location = _$location_;
- $httpBackend = _$httpBackend_;
- $rootScope = _$rootScope_;
- $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, $rootScope) {
- $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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(100);
- $rootScope.accounts.should.be.instanceof(Array).and.have.lengthOf(2);
- }));
-
- it('should fail to init', inject(function($httpBackend, $rootScope) {
- $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
- .respond(400);
-
- $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
- .respond(400);
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(400);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- should.not.exist(accountController.account);
- accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
- should.not.exist(accountController.balance);
- should.not.exist($rootScope.accounts);
- }));
- });
-
- 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(100);
- }));
-
- 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(100);
- }));
- });
- 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(120);
- }));
-
- 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- 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);
- accountController.balance.should.be.equal(100);
- }));
- });
-
- 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');
- });
- });
- });
-
- describe('UI', function() {
- describe('* update subcategory for main form', function() {
- it('should return subcategory 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountController.entry = {category: '560a84058812ad8d0ff200f0'};
- accountController.updateSubCategory();
-
- accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(3);
- }));
-
- it('should return empty subcategory list 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountController.entry = {category: '560a84058812ad8d0ff200ef'};
- accountController.updateSubCategory();
-
- accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
- }));
-
- it('should return empty subcategory list for unknown category', 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountController.entry = {category: 'fake_id'};
- accountController.updateSubCategory();
-
- accountController.sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
- }));
- });
-
- describe('* update subcategory for inplace editor form', function() {
- it('should return subcategory 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountController.updateSubCategoryEditForm('560a84058812ad8d0ff200f0');
-
- accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(3);
- accountController.disabledSubCategories.should.be.false;
- }));
-
- it('should return empty subcategory list 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountController.updateSubCategoryEditForm('560a84058812ad8d0ff200ef');
-
- accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
- accountController.disabledSubCategories.should.be.true;
- }));
-
- it('should return empty subcategory list for unknown category', 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});
-
- $httpBackend.expect('GET', apiRoutes.accounts )
- .respond(DEFAULT_ACCOUNTS);
-
- var accountController = createController();
- $httpBackend.flush();
- $timeout.flush();
-
- accountController.updateSubCategoryEditForm('fake_id');
-
- accountController.edit_sub_categories.should.be.instanceof(Array).and.have.lengthOf(0);
- accountController.disabledSubCategories.should.be.true;
- }));
- });
- })
- });
|