salix/front/core/components/searchbar/searchbar.spec.js

264 lines
8.6 KiB
JavaScript
Raw Normal View History

2019-02-25 08:28:25 +00:00
import './searchbar.js';
describe('Component vnSearchbar', () => {
let controller;
let $element;
let $state;
2019-11-10 12:10:52 +00:00
let $params;
2019-09-20 07:35:12 +00:00
let $scope;
const filter = {id: 1, search: 'needle'};
2019-02-25 08:28:25 +00:00
2020-03-18 12:54:05 +00:00
beforeEach(ngModule('vnCore', $stateProvider => {
$stateProvider
.state('foo', {
abstract: true
})
.state('foo.index', {
url: '/foo/index'
})
.state('foo.card', {
abstract: true
})
.state('foo.card.summary', {
url: '/foo/:id/summary'
})
.state('foo.card.bar', {
url: '/foo/:id/bar'
})
.state('foo.card.baz', {
abstract: true
})
.state('foo.card.baz.index', {
2020-03-18 14:59:03 +00:00
url: '/foo/:id/baz/index'
2020-03-18 12:54:05 +00:00
})
.state('foo.card.baz.edit', {
2020-03-18 14:59:03 +00:00
url: '/foo/:id/baz/:bazId/edit'
2020-03-18 12:54:05 +00:00
});
}));
2019-02-25 08:28:25 +00:00
2020-03-18 12:54:05 +00:00
beforeEach(inject(($componentController, $rootScope, _$state_) => {
2019-09-20 07:35:12 +00:00
$scope = $rootScope.$new();
2019-02-25 08:28:25 +00:00
$state = _$state_;
2019-11-10 12:10:52 +00:00
$params = $state.params;
$params.q = JSON.stringify(filter);
$element = angular.element(`<div></div>`);
controller = $componentController('vnSearchbar', {$element, $scope});
2019-02-25 08:28:25 +00:00
}));
describe('$postLink()', () => {
2019-11-10 12:10:52 +00:00
it(`should fetch the filter from the state if it's in the filter state`, () => {
2020-03-16 17:08:44 +00:00
jest.spyOn(controller, 'doSearch');
controller.autoState = false;
2019-02-25 08:28:25 +00:00
controller.$postLink();
2020-03-16 17:08:44 +00:00
expect(controller.doSearch).toHaveBeenCalledWith(filter, 'state');
2019-02-25 08:28:25 +00:00
});
2019-11-10 12:10:52 +00:00
it(`should not fetch the filter from the state if not in the filter state`, () => {
2020-03-16 17:08:44 +00:00
jest.spyOn(controller, 'doSearch');
controller.autoState = false;
2019-11-10 12:10:52 +00:00
controller.searchState = 'other.state';
2019-02-25 08:28:25 +00:00
controller.$postLink();
2020-03-16 17:08:44 +00:00
expect(controller.doSearch).toHaveBeenCalledWith(null, 'state');
2019-02-25 08:28:25 +00:00
});
});
2019-11-10 12:10:52 +00:00
describe('filter() setter', () => {
it(`should update the bar params and search`, () => {
const withoutHours = new Date(2000, 1, 1);
const withHours = new Date(withoutHours.getTime());
2019-11-10 12:10:52 +00:00
withHours.setHours(12, 30, 15, 10);
controller.filter = {
search: 'needle',
withHours: withHours.toJSON(),
withoutHours: withoutHours.toJSON(),
boolean: true,
negated: false,
myObjectProp: {myProp: 1}
};
const chips = {};
for (const param of controller.params || [])
2019-11-10 12:10:52 +00:00
chips[param.key] = param.chip;
expect(controller.searchString).toBe('needle');
expect(chips.withHours).toBe('withHours: 2000-02-01 12:30');
expect(chips.withoutHours).toBe('withoutHours: 2000-02-01');
expect(chips.boolean).toBe('boolean');
expect(chips.negated).toBe('not negated');
expect(chips.myObjectProp).toBe('myObjectProp');
2019-09-20 07:35:12 +00:00
});
2020-03-16 17:08:44 +00:00
it(`should clear the filter when null`, () => {
controller.filter = null;
expect(controller.filter).toBeNull();
expect(controller.searchString).toBeNull();
expect(controller.params.length).toBe(0);
});
2019-09-20 07:35:12 +00:00
});
describe('shownFilter() getter', () => {
2019-11-10 12:10:52 +00:00
it(`should return the _filter if not null`, () => {
controller.filter = filter;
controller.suggestedFilter = {sugestedParam: 'suggestedValue'};
2019-09-20 07:35:12 +00:00
2019-11-10 12:10:52 +00:00
expect(controller.shownFilter).toEqual(filter);
2019-09-20 07:35:12 +00:00
});
2019-11-10 12:10:52 +00:00
it(`should return the suggested filter if filter is null`, () => {
controller.filter = null;
controller.suggestedFilter = {sugestedParam: 'suggestedValue'};
2019-09-20 07:35:12 +00:00
2019-11-10 12:10:52 +00:00
expect(controller.shownFilter).toEqual(controller.suggestedFilter);
2019-09-20 07:35:12 +00:00
});
});
2019-11-10 12:10:52 +00:00
describe('searchString() setter', () => {
it(`should clear the whole filter when it's null`, () => {
controller.filter = filter;
controller.searchString = null;
2019-02-25 08:28:25 +00:00
2019-11-10 12:10:52 +00:00
expect(controller.searchString).toBeNull();
expect(controller.params.length).toBe(0);
2019-09-20 07:35:12 +00:00
});
});
describe('onPanelSubmit()', () => {
2019-11-10 12:10:52 +00:00
it(`should compact and define the filter`, () => {
2019-09-20 07:35:12 +00:00
controller.$.popover = {hide: jasmine.createSpy('hide')};
2020-03-16 17:08:44 +00:00
jest.spyOn(controller, 'doSearch');
2019-09-20 07:35:12 +00:00
2019-11-10 12:10:52 +00:00
const filter = {
id: 1,
thisKeyShouldBePurged: null,
alsoThis: [],
andThis: {emptyProp: undefined, nullProp: null},
dontForgetThis: [null, undefined],
myObject: {keepThis: true, butNotThis: null},
myArray: [null, undefined, true]
};
2019-09-20 07:35:12 +00:00
controller.onPanelSubmit(filter);
2020-03-16 17:08:44 +00:00
expect(controller.doSearch).toHaveBeenCalledWith({
2019-11-10 12:10:52 +00:00
id: 1,
myObject: {keepThis: true},
myArray: [true]
2020-03-16 17:08:44 +00:00
}, 'panel');
2019-09-20 07:35:12 +00:00
});
});
describe('onSubmit()', () => {
2019-11-10 12:10:52 +00:00
it(`should define the filter`, () => {
2020-03-16 17:08:44 +00:00
jest.spyOn(controller, 'doSearch');
2019-11-10 12:10:52 +00:00
controller.filter = filter;
controller.searchString = 'mySearch';
2019-09-20 07:35:12 +00:00
controller.onSubmit();
2020-03-16 17:08:44 +00:00
expect(controller.doSearch).toHaveBeenCalledWith({
id: 1,
search: 'mySearch'
}, 'bar');
2019-09-20 07:35:12 +00:00
});
});
2019-11-10 12:10:52 +00:00
describe('removeParam()', () => {
it(`should remove the parameter from the filter`, () => {
2020-03-16 17:08:44 +00:00
jest.spyOn(controller, 'doSearch');
controller.model = {
refresh: jest.fn(),
2022-12-21 14:01:05 +00:00
applyFilter: jest.fn().mockReturnValue(Promise.resolve()),
userParams: {
id: 1
}
};
2020-03-16 17:08:44 +00:00
2019-11-10 12:10:52 +00:00
controller.filter = filter;
controller.removeParam(0);
2019-09-20 07:35:12 +00:00
2020-03-16 17:08:44 +00:00
expect(controller.doSearch).toHaveBeenCalledWith({
search: 'needle'
}, 'removeBar');
2019-09-20 07:35:12 +00:00
});
});
2019-11-10 12:10:52 +00:00
describe('doSearch()', () => {
2020-03-18 12:54:05 +00:00
it(`should do the filter`, () => {
jest.spyOn(controller, 'onSearch');
jest.spyOn(controller, 'onFilter');
controller.doSearch(filter, 'any');
$scope.$apply();
2023-05-24 05:33:01 +00:00
expect(controller.onSearch).toHaveBeenCalledWith({$params: filter}, 'any');
2020-03-18 12:54:05 +00:00
expect(controller.onFilter).toHaveBeenCalledWith(filter, 'any', undefined);
});
});
describe('onFilter()', () => {
it(`should go to the summary state when one result`, () => {
jest.spyOn($state, 'go');
const data = [{id: 1}];
2020-03-18 12:54:05 +00:00
controller.baseState = 'foo';
controller.onFilter(filter, 'any', data);
$scope.$apply();
expect($state.go).toHaveBeenCalledWith('foo.card.summary', {id: 1}, undefined);
expect(controller.filter).toEqual(null);
});
it(`should keep the same card state when one result and it's already inside any card state`, () => {
$state.go('foo.card.bar', {id: 1});
$scope.$apply();
jest.spyOn($state, 'go');
const data = [{id: 1}];
2020-03-18 12:54:05 +00:00
controller.baseState = 'foo';
controller.onFilter(filter, 'any', data);
$scope.$apply();
expect($state.go).toHaveBeenCalledWith('foo.card.bar', {id: 1}, undefined);
expect(controller.filter).toEqual(null);
});
it(`should keep the same card state but index when one result and it's already in card state but inside more than three-nested states`, () => {
2020-03-18 14:59:03 +00:00
$state.go('foo.card.baz.edit', {id: 1, bazId: 1});
2020-03-18 12:54:05 +00:00
$scope.$apply();
jest.spyOn($state, 'go');
const data = [{id: 1}];
2020-03-18 12:54:05 +00:00
controller.baseState = 'foo';
controller.onFilter(filter, 'any', data);
$scope.$apply();
expect($state.go).toHaveBeenCalledWith('foo.card.baz.index', {id: 1}, undefined);
expect(controller.filter).toEqual(null);
});
it(`should go to the search state when multiple results and pass the filter as query param`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn($state, 'go');
2020-03-16 17:08:44 +00:00
controller.autoState = false;
2019-11-10 12:10:52 +00:00
controller.searchState = 'search.state';
2020-03-18 12:54:05 +00:00
controller.onFilter(filter, 'any');
2020-03-16 17:08:44 +00:00
$scope.$apply();
2019-02-25 08:28:25 +00:00
const queryParams = {q: JSON.stringify(filter)};
2019-02-25 08:28:25 +00:00
2020-03-16 17:08:44 +00:00
expect($state.go).toHaveBeenCalledWith('search.state', queryParams, undefined);
2019-11-10 12:10:52 +00:00
expect(controller.filter).toEqual(filter);
2019-02-25 08:28:25 +00:00
});
});
});