diff --git a/front/core/components/searchbar/__snapshots__/searchbar.spec.js.snap b/front/core/components/searchbar/__snapshots__/searchbar.spec.js.snap new file mode 100644 index 000000000..698e1ba88 --- /dev/null +++ b/front/core/components/searchbar/__snapshots__/searchbar.spec.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Component vnSearchbar doSearch() should define searchString, call onSearch and the model applyFilter method 1`] = `"function search() { [native code] } 0:i 1:d 2:: 3:( ) 4:1 5:2 6:3 7:4 8:5 9:6"`; + +exports[`Component vnSearchbar onPanelSubmit() should hide the popover, and set the filter as it wasn't defined 1`] = `Object {}`; + +exports[`Component vnSearchbar onPanelSubmit() should hide the popover, compact the filter 1`] = ` +Object { + "id": 999, +} +`; + +exports[`Component vnSearchbar onSubmit() should define the controller's filter 1`] = ` +Object { + "search": "id: 999", +} +`; diff --git a/front/core/components/searchbar/searchbar.js b/front/core/components/searchbar/searchbar.js index 6412e67bc..9384b712b 100644 --- a/front/core/components/searchbar/searchbar.js +++ b/front/core/components/searchbar/searchbar.js @@ -22,7 +22,7 @@ export default class Controller extends Component { this.$element = $element; this.$compile = $compile; this.$state = $state; - this.$scope = $scope; + this.$ = $scope; let criteria = {to: this.$state.current.name}; this.deregisterCallback = $transitions.onSuccess(criteria, () => this.onStateChange()); @@ -51,7 +51,9 @@ export default class Controller extends Component { if (this.$state.params.q) { try { this._filter = JSON.parse(this.$state.params.q); - } catch (e) {} + } catch (e) { + console.error(e); + } } this.doSearch(); diff --git a/front/core/components/searchbar/searchbar.spec.js b/front/core/components/searchbar/searchbar.spec.js index 44a6097bb..14950d665 100644 --- a/front/core/components/searchbar/searchbar.spec.js +++ b/front/core/components/searchbar/searchbar.spec.js @@ -4,15 +4,17 @@ describe('Component vnSearchbar', () => { let controller; let $element; let $state; + let $scope; beforeEach(angular.mock.module('vnCore', $translateProvider => { $translateProvider.translations('en', {}); })); - beforeEach(angular.mock.inject(($componentController, _$state_) => { + beforeEach(angular.mock.inject(($componentController, _$state_, $rootScope, $compile) => { + $scope = $rootScope.$new(); $state = _$state_; - $element = angular.element(`
`); - controller = $componentController('vnSearchbar', {$element, $state}); + $element = angular.element(``); + controller = $componentController('vnSearchbar', {$element, $state, $scope, $compile}); controller.panel = 'vn-client-search-panel'; })); @@ -36,6 +38,38 @@ describe('Component vnSearchbar', () => { }); }); + describe('onStateChange()', () => { + it(`should set a formated _filter in the controller`, () => { + spyOn(controller, 'doSearch'); + Object.assign(controller.$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 = {}; @@ -59,6 +93,83 @@ describe('Component vnSearchbar', () => { }); }); + describe('onPopoverClose()', () => { + it(`should get rid of $panel and $panelScope`, () => { + controller.$panel = { + I: 'should disappear', + remove: () => {} + }; + + controller.$panelScope = {$destroy: jasmine.createSpy('$destroy')}; + + controller.onPopoverClose(); + + expect(controller.$panelScope.$destroy).toHaveBeenCalledWith(); + expect(controller.$panel).toBeNull(); + }); + }); + + 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'); diff --git a/jest.config.js b/jest.config.js index 4ff5ee8f7..facd1c8a7 100644 --- a/jest.config.js +++ b/jest.config.js @@ -29,7 +29,8 @@ module.exports = { // An array of regexp pattern strings used to skip coverage collection coveragePathIgnorePatterns: [ - '/node_modules/' + '/node_modules/', + '.spec.js' ], // A list of reporter names that Jest uses when writing coverage reports