import './index.js'; import crudModel from 'core/mocks/crud-model'; fdescribe('Order', () => { describe('Component vnOrderCatalog', () => { let $scope; let $state; let controller; let $httpBackend; beforeEach(ngModule('order')); beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_, $rootScope) => { $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); $scope.model = crudModel; $scope.search = {}; $state = _$state_; $state.params.categoryId = 1; $state.params.typeId = 2; $state.current.name = 'my.current.state'; controller = $componentController('vnOrderCatalog', {$scope, $state}); })); describe('order() setter', () => { it(`should call scope $applyAsync() method and apply filters from state params`, () => { $httpBackend.expect('GET', `Orders/4/getItemTypeAvailable?itemCategoryId=1`).respond(); controller.order = {id: 4}; $scope.$apply(); expect(controller.categoryId).toEqual(1); expect(controller.typeId).toEqual(2); }); }); describe('items() setter', () => { it(`should return an object with order params`, () => { let expectedResult = [{field: 'showOrder, price', name: 'Color'}]; let unexpectedResult = [{tagFk: 5, name: 'Color'}]; controller.items = [{id: 1, name: 'My Item', tags: [ {tagFk: 4, name: 'Length'}, {tagFk: 5, name: 'Color'} ]}]; expect(controller.orderFields.length).toEqual(5); expect(controller.orderFields).toEqual(jasmine.arrayContaining(expectedResult)); expect(controller.orderFields).not.toEqual(jasmine.arrayContaining(unexpectedResult)); }); }); describe('categoryId() setter', () => { it(`should set category property to null, call updateStateParams() method and not call applyFilters()`, () => { spyOn(controller, 'updateStateParams'); controller.categoryId = null; expect(controller.updateStateParams).toHaveBeenCalledWith(); }); it(`should set category property and then call updateStateParams() and applyFilters() methods`, () => { spyOn(controller, 'updateStateParams'); controller._order = {id: 4}; controller.categoryId = 2; expect(controller.updateStateParams).toHaveBeenCalledWith(); }); }); describe('typeId() setter', () => { it(`should set type property to null, call updateStateParams() method and not call applyFilters()`, () => { spyOn(controller, 'updateStateParams'); spyOn(controller, 'applyFilters'); controller.typeId = null; expect(controller.updateStateParams).toHaveBeenCalledWith(); expect(controller.applyFilters).not.toHaveBeenCalledWith(); }); it(`should set category property and then call updateStateParams() and applyFilters() methods`, () => { spyOn(controller, 'updateStateParams'); spyOn(controller, 'applyFilters'); controller.typeId = 2; expect(controller.updateStateParams).toHaveBeenCalledWith(); expect(controller.applyFilters).toHaveBeenCalledWith(); }); }); describe('onSearchByTag()', () => { it(`should not add a new tag if the event key code doesn't equals to 'Enter'`, () => { spyOn(controller, 'applyFilters'); controller.order = {id: 4}; controller.value = 'Color'; controller.onSearchByTag({key: 'Tab'}); expect(controller.applyFilters).not.toHaveBeenCalledWith(); }); it(`should add a new tag if the event key code equals to 'Enter' an then call applyFilters()`, () => { spyOn(controller, 'applyFilters'); controller.order = {id: 4}; controller.value = 'Color'; controller.onSearchByTag({key: 'Enter'}); expect(controller.applyFilters).toHaveBeenCalledWith(); }); }); describe('onSearchById()', () => { it(`should not filter by id if the event key code doesn't equals to 'Enter'`, () => { spyOn(controller, 'applyFilters'); controller.itemId = 1; controller.onSearchById({key: 'Tab'}); expect(controller.applyFilters).not.toHaveBeenCalledWith(); }); it(`should filter by id if the event key code equals to 'Enter' an then call applyFilters()`, () => { spyOn(controller, 'applyFilters'); controller.itemId = 1; controller.onSearchById({key: 'Enter'}); expect(controller.applyFilters).toHaveBeenCalledWith(); }); }); describe('applyFilters()', () => { it(`should call model applyFilter() method with a new filter`, () => { let model = controller.$.model; spyOn(model, 'applyFilter'); controller._categoryId = 2; controller._typeId = 4; controller._order = {id: 4}; controller.applyFilters(); expect(model.applyFilter).toHaveBeenCalledWith( {where: {categoryFk: 2, typeFk: 4}}, {orderFk: 4, orderBy: controller.getOrderBy(), tags: []}); }); }); describe('remove()', () => { it(`should remove a tag from tags property`, () => { spyOn(controller, 'applyFilters'); controller.tags = [{tagFk: 1, value: 'Blue'}, {tagFk: 2, value: '70'}]; controller.remove(0); expect(controller.tags.length).toEqual(1); expect(controller.tags[0].tagFk).toEqual(2); expect(controller.applyFilters).toHaveBeenCalledWith(); }); it(`should remove a tag from tags property and call applyFilters() if there's no more tags`, () => { spyOn(controller, 'applyFilters'); controller._categoryId = 1; controller._typeId = 1; controller.tags = [{tagFk: 1, value: 'Blue'}]; controller.remove(0); expect(controller.tags.length).toEqual(0); expect(controller.applyFilters).toHaveBeenCalledWith(); }); }); describe('updateStateParams()', () => { it(`should call state go() method passing category and type state params`, () => { spyOn(controller.$state, 'go'); controller._categoryId = 2; controller._typeId = 4; let result = {categoryId: 2, typeId: 4}; controller.updateStateParams(); expect(controller.$state.go).toHaveBeenCalledWith('my.current.state', result); }); }); describe('getOrderBy()', () => { it(`should return an object with order params`, () => { controller.orderField = 'relevancy DESC, name'; controller.orderWay = 'DESC'; let expectedResult = { field: 'relevancy DESC, name', way: 'DESC', isTag: false }; let result = controller.getOrderBy(); expect(result).toEqual(expectedResult); }); }); describe('applyOrder()', () => { it(`should apply order param to model calling getOrderBy()`, () => { controller.field = 'relevancy DESC, name'; controller.way = 'ASC'; controller._categoryId = 1; controller._typeId = 1; let expectedOrder = {orderBy: controller.getOrderBy()}; spyOn(controller, 'getOrderBy').and.callThrough(); spyOn(controller.$.model, 'addFilter'); controller.applyOrder(); expect(controller.getOrderBy).toHaveBeenCalledWith(); expect(controller.$.model.addFilter).toHaveBeenCalledWith(null, expectedOrder); }); }); }); });