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
|
|
|
|
2019-10-24 22:53:53 +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>`);
|
2019-10-28 16:31:33 +00:00
|
|
|
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`, () => {
|
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`, () => {
|
|
|
|
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
|
|
|
});
|
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');
|
|
|
|
|
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'
|
|
|
|
}, 'bar');
|
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`, () => {
|
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';
|
|
|
|
controller.doSearch(filter);
|
2020-03-16 17:08:44 +00:00
|
|
|
$scope.$apply();
|
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
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|