import './searchbar.js'; describe('Component vnSearchbar', () => { let controller; let $element; let $state; beforeEach(ngModule('vnCore')); beforeEach(angular.mock.inject(($componentController, _$state_) => { $state = _$state_; $element = angular.element(`
`); controller = $componentController('vnSearchbar', {$element, $state}); 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(); }); }); describe('filter() setter', () => { it(`should call $state.go() to replace the current state location instead of creating a new one`, () => { controller._filter = {}; spyOn(controller.$state, 'go'); controller.filter = {expected: 'filter'}; expect(controller._filter).toEqual(controller.filter); expect(controller.$state.go).toHaveBeenCalledWith('.', Object({q: '{"expected":"filter"}'}), Object({location: 'replace'})); }); }); describe('openPanel()', () => { it(`should do nothing if the event is prevented`, () => { let event = { defaultPrevented: true, preventDefault: jasmine.createSpy('preventDefault') }; controller.openPanel(event); expect(event.preventDefault).not.toHaveBeenCalledWith(); }); }); 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' }); }); }); });