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

168 lines
6.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-09-20 07:35:12 +00:00
let $scope;
2019-02-25 08:28:25 +00:00
beforeEach(ngModule('vnCore'));
2019-02-25 08:28:25 +00:00
beforeEach(angular.mock.inject(($componentController, _$state_, $rootScope) => {
2019-09-20 07:35:12 +00:00
$scope = $rootScope.$new();
2019-02-25 08:28:25 +00:00
$state = _$state_;
2019-09-20 07:35:12 +00:00
$element = angular.element(`<vn-textfield><input></input></vn-textfield>`);
controller = $componentController('vnSearchbar', {$element, $scope});
2019-02-25 08:28:25 +00:00
controller.panel = 'vn-client-search-panel';
}));
describe('$postLink()', () => {
it(`should not call onStateChange() if filter is defined`, () => {
spyOn(controller, 'onStateChange');
controller.filter = {};
controller.$postLink();
expect(controller.onStateChange).not.toHaveBeenCalledWith();
});
it(`should call onStateChange() if filter is null`, () => {
spyOn(controller, 'onStateChange');
controller.filter = null;
controller.$postLink();
expect(controller.onStateChange).toHaveBeenCalledWith();
});
});
2019-09-20 07:35:12 +00:00
describe('onStateChange()', () => {
it(`should set a formated _filter in the controller`, () => {
spyOn(controller, 'doSearch');
Object.assign($state.params, {q: '{"id": 999}'});
2019-09-20 07:35:12 +00:00
controller.onStateChange();
expect(controller._filter).toEqual({id: 999});
expect(controller.doSearch).toHaveBeenCalledWith();
});
});
describe('shownFilter() getter', () => {
it(`should return the _filter if not NULL`, () => {
controller.filter = '{"id": 999}';
let shownFilter = controller.shownFilter;
expect(shownFilter).toEqual(controller._filter);
expect(shownFilter).not.toEqual(controller.suggestedFilter);
});
it(`should return the suggested filter if filter is NULL`, () => {
controller.suggestedFilter = '{"id": 888}';
let shownFilter = controller.shownFilter;
expect(shownFilter).not.toEqual(controller._filter);
expect(shownFilter).toEqual(controller.suggestedFilter);
});
});
2019-02-25 08:28:25 +00:00
describe('filter() setter', () => {
it(`should call $state.go() to replace the current state location instead of creating a new one`, () => {
2019-02-25 08:28:25 +00:00
controller._filter = {};
spyOn($state, 'go');
2019-02-25 08:28:25 +00:00
controller.filter = {expected: 'filter'};
expect(controller._filter).toEqual(controller.filter);
expect($state.go).toHaveBeenCalledWith('.', Object({q: '{"expected":"filter"}'}), Object({location: 'replace'}));
2019-09-20 07:35:12 +00:00
});
});
describe('onPanelSubmit()', () => {
it(`should hide the popover, and set the filter as it wasn't defined`, () => {
controller.$.popover = {hide: jasmine.createSpy('hide')};
const filter = undefined;
controller.onPanelSubmit(filter);
expect(controller.filter).toMatchSnapshot();
});
it(`should hide the popover, compact the filter`, () => {
controller.$.popover = {hide: jasmine.createSpy('hide')};
const filter = {id: 999, thisKeyShouldBePurged: null};
controller.onPanelSubmit(filter);
expect(controller.filter).toMatchSnapshot();
});
});
describe('onSubmit()', () => {
it(`should define the controller's filter`, () => {
controller.searchString = 'id: 999';
controller.onSubmit();
expect(controller.filter).toMatchSnapshot();
});
});
describe('doSearch()', () => {
it(`should define searchString, call onSearch and the model applyFilter method`, () => {
controller._filter = 'id: 123456';
controller.autoload = true;
controller.onSearch = jasmine.createSpy('onSearch');
controller.model = {applyFilter: jasmine.createSpy('applyFilter')};
controller.paramBuilder = jasmine.createSpy('paramBuilder').and.returnValue({'param': `${controller._filter}`});
controller.doSearch();
expect(controller.searchString).toMatchSnapshot();
expect(controller.onSearch).toHaveBeenCalledWith({'$params': `${controller._filter}`});
expect(controller.model.applyFilter).toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Object));
});
it(`should define searchString, call onSearch and the model clear method`, () => {
controller._filter = null;
controller.autoload = false;
controller.onSearch = jasmine.createSpy('onSearch');
controller.model = {clear: jasmine.createSpy('clear')};
controller.paramBuilder = jasmine.createSpy('paramBuilder').and.returnValue({'param': `${controller._filter}`});
controller.doSearch();
expect(controller.searchString).toEqual('');
expect(controller.onSearch).toHaveBeenCalledWith({'$params': null});
expect(controller.model.clear).toHaveBeenCalledWith();
});
});
2019-02-25 08:28:25 +00:00
describe('getObjectFromString()', () => {
it(`should return a formated object based on the string received for basic search`, () => {
let result = controller.getObjectFromString('Bruce Wayne');
expect(result).toEqual({search: 'Bruce Wayne'});
});
it(`should return a formated object based on the string received for advanced search`, () => {
let result = controller.getObjectFromString('id:101 name:(Bruce Wayne)');
expect(result).toEqual({id: '101', name: 'Bruce Wayne'});
});
it(`should format the object grouping any unmatched part of the instring of the string to the search property`, () => {
let string = 'I am the search id:101 name:(Bruce Wayne) concatenated value';
let result = controller.getObjectFromString(string);
expect(result).toEqual({
id: '101',
name: 'Bruce Wayne',
search: 'I am the search concatenated value'
});
});
});
});