import './searchbar.js'; describe('Component vnSearchbar', () => { let controller; let $element; let $state; let $scope; beforeEach(ngModule('vnCore')); beforeEach(angular.mock.inject(($componentController, _$state_, $rootScope) => { $scope = $rootScope.$new(); $state = _$state_; $element = angular.element(``); controller = $componentController('vnSearchbar', {$element, $scope}); 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('onStateChange()', () => { it(`should set a formated _filter in the controller`, () => { spyOn(controller, 'doSearch'); Object.assign($state.params, {q: '{"id": 999}'}); 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); }); }); describe('filter() setter', () => { it(`should call $state.go() to replace the current state location instead of creating a new one`, () => { controller._filter = {}; spyOn($state, 'go'); controller.filter = {expected: 'filter'}; expect(controller._filter).toEqual(controller.filter); expect($state.go).toHaveBeenCalledWith('.', Object({q: '{"expected":"filter"}'}), Object({location: 'replace'})); }); }); 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(); }); }); 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' }); }); }); });