2018-09-14 12:18:39 +00:00
|
|
|
import './index.js';
|
2018-12-27 11:54:16 +00:00
|
|
|
import crudModel from 'core/mocks/crud-model';
|
2018-09-14 12:18:39 +00:00
|
|
|
|
2020-01-30 13:35:21 +00:00
|
|
|
describe('Order', () => {
|
2018-09-14 12:18:39 +00:00
|
|
|
describe('Component vnOrderCatalog', () => {
|
|
|
|
let $scope;
|
2020-01-30 12:53:14 +00:00
|
|
|
let $state;
|
2018-09-14 12:18:39 +00:00
|
|
|
let controller;
|
2020-01-30 12:53:14 +00:00
|
|
|
let $httpBackend;
|
2018-09-14 12:18:39 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
beforeEach(ngModule('order'));
|
2018-09-14 12:18:39 +00:00
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_, $rootScope) => {
|
|
|
|
$httpBackend = _$httpBackend_;
|
2018-09-14 12:18:39 +00:00
|
|
|
$scope = $rootScope.$new();
|
|
|
|
$scope.model = crudModel;
|
2020-01-30 12:53:14 +00:00
|
|
|
$scope.search = {};
|
2020-02-19 10:57:39 +00:00
|
|
|
$scope.itemId = {};
|
2020-01-30 12:53:14 +00:00
|
|
|
$state = _$state_;
|
|
|
|
$state.params.categoryId = 1;
|
|
|
|
$state.params.typeId = 2;
|
|
|
|
$state.current.name = 'my.current.state';
|
|
|
|
controller = $componentController('vnOrderCatalog', {$scope, $state});
|
2020-02-06 11:46:44 +00:00
|
|
|
controller._order = {id: 4};
|
2018-09-14 12:18:39 +00:00
|
|
|
}));
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
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', () => {
|
2018-10-31 13:56:54 +00:00
|
|
|
it(`should return an object with order params`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'buildTagsFilter');
|
|
|
|
jest.spyOn(controller, 'buildOrderFilter');
|
|
|
|
|
2020-02-11 13:41:59 +00:00
|
|
|
const expectedResult = [{field: 'showOrder, price', name: 'Color and price'}];
|
|
|
|
const items = [{id: 1, name: 'My Item', tags: [
|
2019-02-19 14:13:27 +00:00
|
|
|
{tagFk: 4, name: 'Length'},
|
|
|
|
{tagFk: 5, name: 'Color'}
|
2018-10-31 13:56:54 +00:00
|
|
|
]}];
|
2020-02-11 13:41:59 +00:00
|
|
|
controller.items = items;
|
2018-10-31 13:56:54 +00:00
|
|
|
|
2020-02-11 13:41:59 +00:00
|
|
|
expect(controller.orderFields.length).toEqual(6);
|
2019-10-28 16:31:33 +00:00
|
|
|
expect(controller.orderFields).toEqual(jasmine.arrayContaining(expectedResult));
|
2020-02-11 13:41:59 +00:00
|
|
|
expect(controller.buildTagsFilter).toHaveBeenCalledWith(items);
|
|
|
|
expect(controller.buildOrderFilter).toHaveBeenCalledWith(items);
|
2018-10-31 13:56:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
describe('categoryId() setter', () => {
|
|
|
|
it(`should set category property to null, call updateStateParams() method and not call applyFilters()`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'updateStateParams');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.categoryId = null;
|
|
|
|
|
|
|
|
expect(controller.updateStateParams).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should set category property and then call updateStateParams() and applyFilters() methods`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'updateStateParams');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.categoryId = 2;
|
|
|
|
|
|
|
|
expect(controller.updateStateParams).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-06 11:46:44 +00:00
|
|
|
describe('changeCategory()', () => {
|
|
|
|
it(`should set categoryId property to null if the new value equals to the old one`, () => {
|
|
|
|
controller.categoryId = 2;
|
|
|
|
controller.changeCategory(2);
|
|
|
|
|
|
|
|
expect(controller.categoryId).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should set categoryId property`, () => {
|
|
|
|
controller.categoryId = 2;
|
|
|
|
controller.changeCategory(1);
|
|
|
|
|
|
|
|
expect(controller.categoryId).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
describe('typeId() setter', () => {
|
|
|
|
it(`should set type property to null, call updateStateParams() method and not call applyFilters()`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'updateStateParams');
|
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.typeId = null;
|
|
|
|
|
|
|
|
expect(controller.updateStateParams).toHaveBeenCalledWith();
|
|
|
|
expect(controller.applyFilters).not.toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should set category property and then call updateStateParams() and applyFilters() methods`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'updateStateParams');
|
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.typeId = 2;
|
|
|
|
|
|
|
|
expect(controller.updateStateParams).toHaveBeenCalledWith();
|
|
|
|
expect(controller.applyFilters).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-19 10:57:39 +00:00
|
|
|
describe('itemId() setter', () => {
|
|
|
|
it(`should set itemId property and then call updateStateParams() and applyFilters() methods`, () => {
|
|
|
|
jest.spyOn(controller, 'updateStateParams');
|
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
|
|
|
controller.itemId = 1;
|
|
|
|
|
|
|
|
expect(controller.updateStateParams).toHaveBeenCalledWith();
|
|
|
|
expect(controller.applyFilters).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('tags() setter', () => {
|
|
|
|
it(`should set tags property and then call updateStateParams() and applyFilters() methods`, () => {
|
|
|
|
jest.spyOn(controller, 'updateStateParams');
|
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
|
|
|
controller.tags = [{tagFk: 11, value: 'Brown'}];
|
|
|
|
|
|
|
|
expect(controller.updateStateParams).toHaveBeenCalledWith();
|
|
|
|
expect(controller.applyFilters).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
describe('onSearchByTag()', () => {
|
|
|
|
it(`should not add a new tag if the event key code doesn't equals to 'Enter'`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.order = {id: 4};
|
2020-02-19 10:57:39 +00:00
|
|
|
controller.$.search.value = 'Brown';
|
2020-01-30 12:53:14 +00:00
|
|
|
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()`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'applyFilters');
|
2020-01-30 12:53:14 +00:00
|
|
|
|
2020-02-19 10:57:39 +00:00
|
|
|
controller.order = {id: 4};
|
|
|
|
controller.$.search.value = 'Brown';
|
2020-01-30 12:53:14 +00:00
|
|
|
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'`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
|
|
|
controller.$.itemId.value = 1;
|
2020-01-30 12:53:14 +00:00
|
|
|
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()`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'applyFilters');
|
2020-01-30 12:53:14 +00:00
|
|
|
|
2020-02-19 10:57:39 +00:00
|
|
|
controller.$.itemId.value = 1;
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.onSearchById({key: 'Enter'});
|
|
|
|
|
|
|
|
expect(controller.applyFilters).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('applyFilters()', () => {
|
|
|
|
it(`should call model applyFilter() method with a new filter`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller.$.model, 'applyFilter');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller._categoryId = 2;
|
|
|
|
controller._typeId = 4;
|
|
|
|
|
|
|
|
controller.applyFilters();
|
|
|
|
|
2020-02-19 10:57:39 +00:00
|
|
|
expect(controller.$.model.applyFilter).toHaveBeenCalledWith(
|
2020-01-30 12:53:14 +00:00
|
|
|
{where: {categoryFk: 2, typeFk: 4}},
|
|
|
|
{orderFk: 4, orderBy: controller.getOrderBy(), tags: []});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('remove()', () => {
|
|
|
|
it(`should remove a tag from tags property`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
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`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'applyFilters');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
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`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller.$state, 'go');
|
|
|
|
|
2020-01-30 12:53:14 +00:00
|
|
|
controller._categoryId = 2;
|
|
|
|
controller._typeId = 4;
|
2020-02-19 10:57:39 +00:00
|
|
|
controller._itemId = 1;
|
|
|
|
controller._tags = [
|
|
|
|
{tagFk: 11, value: 'Precission', tagSelection: {name: 'Category'}}
|
|
|
|
];
|
|
|
|
const tags = JSON.stringify([{
|
|
|
|
value: 'Precission',
|
|
|
|
tagFk: 11, tagSelection: {name: 'Category'}}
|
|
|
|
]);
|
|
|
|
let result = {categoryId: 2, typeId: 4, itemId: 1, tags: tags};
|
2020-01-30 12:53:14 +00:00
|
|
|
controller.updateStateParams();
|
|
|
|
|
|
|
|
expect(controller.$state.go).toHaveBeenCalledWith('my.current.state', result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-31 13:56:54 +00:00
|
|
|
describe('getOrderBy()', () => {
|
|
|
|
it(`should return an object with order params`, () => {
|
2019-10-28 16:31:33 +00:00
|
|
|
controller.orderField = 'relevancy DESC, name';
|
|
|
|
controller.orderWay = 'DESC';
|
|
|
|
let expectedResult = {
|
|
|
|
field: 'relevancy DESC, name',
|
|
|
|
way: 'DESC',
|
|
|
|
isTag: false
|
|
|
|
};
|
2018-10-31 13:56:54 +00:00
|
|
|
let result = controller.getOrderBy();
|
|
|
|
|
|
|
|
expect(result).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('applyOrder()', () => {
|
|
|
|
it(`should apply order param to model calling getOrderBy()`, () => {
|
2020-02-19 10:57:39 +00:00
|
|
|
jest.spyOn(controller, 'getOrderBy');
|
|
|
|
jest.spyOn(controller.$.model, 'addFilter');
|
|
|
|
|
2018-10-31 13:56:54 +00:00
|
|
|
controller.field = 'relevancy DESC, name';
|
|
|
|
controller.way = 'ASC';
|
2020-01-30 12:53:14 +00:00
|
|
|
controller._categoryId = 1;
|
|
|
|
controller._typeId = 1;
|
2018-10-31 13:56:54 +00:00
|
|
|
let expectedOrder = {orderBy: controller.getOrderBy()};
|
2020-02-19 10:57:39 +00:00
|
|
|
|
2018-10-31 13:56:54 +00:00
|
|
|
controller.applyOrder();
|
|
|
|
|
|
|
|
expect(controller.getOrderBy).toHaveBeenCalledWith();
|
2020-01-30 12:53:14 +00:00
|
|
|
expect(controller.$.model.addFilter).toHaveBeenCalledWith(null, expectedOrder);
|
2018-09-14 12:18:39 +00:00
|
|
|
});
|
|
|
|
});
|
2020-02-11 13:41:59 +00:00
|
|
|
|
|
|
|
describe('buildTagsFilter()', () => {
|
|
|
|
it(`should create an array of non repeated tag values and then set the tagValues property`, () => {
|
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
id: 1, name: 'My Item 1', tags: [
|
|
|
|
{tagFk: 4, name: 'Length', value: 1},
|
|
|
|
{tagFk: 5, name: 'Color', value: 'red'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2, name: 'My Item 2', tags: [
|
|
|
|
{tagFk: 4, name: 'Length', value: 1},
|
|
|
|
{tagFk: 5, name: 'Color', value: 'blue'}
|
|
|
|
]
|
|
|
|
}];
|
|
|
|
controller.buildTagsFilter(items);
|
|
|
|
|
|
|
|
expect(controller.tagValues.length).toEqual(3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('buildOrderFilter()', () => {
|
|
|
|
it(`should create an array of non repeated tags plus default filters and then set the orderFields property`, () => {
|
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
id: 1, name: 'My Item 1', tags: [
|
|
|
|
{tagFk: 4, name: 'Length'},
|
|
|
|
{tagFk: 5, name: 'Color'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2, name: 'My Item 2', tags: [
|
|
|
|
{tagFk: 5, name: 'Color'},
|
|
|
|
{tagFk: 6, name: 'Relevancy'}
|
|
|
|
]
|
|
|
|
}];
|
|
|
|
controller.buildOrderFilter(items);
|
|
|
|
|
|
|
|
expect(controller.orderFields.length).toEqual(7);
|
|
|
|
});
|
|
|
|
});
|
2018-09-14 12:18:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|