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

153 lines
5.1 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;
2019-11-10 12:10:52 +00:00
let filter = {id: 1, search: 'needle'};
2019-02-25 08:28:25 +00:00
beforeEach(ngModule('vnCore'));
2019-02-25 08:28:25 +00:00
2019-11-10 12:10:52 +00:00
beforeEach(angular.mock.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
controller.panel = 'vn-client-search-panel';
}));
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`, () => {
2019-02-25 08:28:25 +00:00
controller.$postLink();
2019-11-10 12:10:52 +00:00
expect(controller.filter).toEqual(filter);
expect(controller.searchString).toBe('needle');
expect(controller.params.length).toBe(1);
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`, () => {
controller.searchState = 'other.state';
2019-02-25 08:28:25 +00:00
controller.$postLink();
2019-11-10 12:10:52 +00:00
expect(controller.filter).toBeNull();
expect(controller.searchString).toBeNull();
expect(controller.params.length).toBe(0);
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`, () => {
let withoutHours = new Date(2000, 1, 1);
let withHours = new Date(withoutHours.getTime());
withHours.setHours(12, 30, 15, 10);
controller.filter = {
search: 'needle',
withHours: withHours.toJSON(),
withoutHours: withoutHours.toJSON(),
boolean: true,
negated: false,
myObjectProp: {myProp: 1}
};
let chips = {};
for (let param of controller.params || [])
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
});
});
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')};
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);
2019-11-10 12:10:52 +00:00
expect(controller.filter).toEqual({
id: 1,
myObject: {keepThis: true},
myArray: [true]
});
2019-09-20 07:35:12 +00:00
});
});
describe('onSubmit()', () => {
2019-11-10 12:10:52 +00:00
it(`should define the filter`, () => {
controller.filter = filter;
controller.searchString = 'mySearch';
2019-09-20 07:35:12 +00:00
controller.onSubmit();
2019-11-10 12:10:52 +00:00
expect(controller.filter).toEqual({id: 1, search: 'mySearch'});
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`, () => {
controller.filter = filter;
controller.removeParam(0);
2019-09-20 07:35:12 +00:00
2019-11-10 12:10:52 +00:00
expect(controller.filter).toEqual({search: 'needle'});
2019-09-20 07:35:12 +00:00
});
});
2019-11-10 12:10:52 +00:00
describe('doSearch()', () => {
it(`should go to the search state and pass the filter as query param`, () => {
spyOn($state, 'go');
controller.searchState = 'search.state';
controller.doSearch(filter);
2019-02-25 08:28:25 +00:00
2019-11-10 12:10:52 +00:00
let queryParams = {q: JSON.stringify(filter)};
2019-02-25 08:28:25 +00:00
2019-11-10 12:10:52 +00:00
expect($state.go).toHaveBeenCalledWith('search.state', queryParams, null);
expect(controller.filter).toEqual(filter);
2019-02-25 08:28:25 +00:00
});
});
});