Front tests passed

This commit is contained in:
Juan Ferrer 2019-11-10 13:10:52 +01:00
parent 515234187a
commit bee50a6b03
6 changed files with 139 additions and 164 deletions

View File

@ -14,7 +14,7 @@ describe('Component vnButtonMenu', () => {
$element.remove(); $element.remove();
}); });
describe('onClick(event)', () => { describe('onButtonClick(event)', () => {
it(`should emit the open event`, () => { it(`should emit the open event`, () => {
spyOn(controller, 'emit'); spyOn(controller, 'emit');
@ -23,7 +23,7 @@ describe('Component vnButtonMenu', () => {
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });
controller.onClick(event); controller.onButtonClick(event);
expect(controller.emit).toHaveBeenCalledWith('open'); expect(controller.emit).toHaveBeenCalledWith('open');
}); });

View File

@ -1,17 +0,0 @@
// 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",
}
`;

View File

@ -32,28 +32,46 @@ export default class Controller extends Component {
this.deregisterCallback(); this.deregisterCallback();
} }
get filter() {
return this._filter;
}
set filter(value) {
this._filter = value;
this.toBar(value);
}
get shownFilter() { get shownFilter() {
return this.filter != null return this.filter != null
? this.filter ? this.filter
: this.suggestedFilter; : this.suggestedFilter;
} }
get searchString() {
return this._searchString;
}
set searchString(value) {
this._searchString = value;
if (value == null) this.params = [];
}
onStateChange() { onStateChange() {
let filter = null;
if (this.$state.is(this.searchState)) { if (this.$state.is(this.searchState)) {
if (this.$state.params.q) { if (this.$params.q) {
try { try {
this.filter = JSON.parse(this.$state.params.q); filter = JSON.parse(this.$params.q);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
} else }
this.filter = null;
focus(this.element.querySelector('vn-textfield input')); focus(this.element.querySelector('vn-textfield input'));
} else }
this.filter = null;
this.toBar(this.filter); this.filter = filter;
} }
openPanel(event) { openPanel(event) {
@ -94,16 +112,9 @@ export default class Controller extends Component {
this.doSearch(this.fromBar()); this.doSearch(this.fromBar());
} }
get searchString() {
return this._searchString;
}
set searchString(value) {
this._searchString = value;
if (value == null) this.params = [];
}
doSearch(filter) { doSearch(filter) {
this.filter = filter;
let opts = this.$state.is(this.searchState) let opts = this.$state.is(this.searchState)
? {location: 'replace'} : null; ? {location: 'replace'} : null;
this.$state.go(this.searchState, this.$state.go(this.searchState,

View File

@ -4,164 +4,149 @@ describe('Component vnSearchbar', () => {
let controller; let controller;
let $element; let $element;
let $state; let $state;
let $params;
let $scope; let $scope;
let filter = {id: 1, search: 'needle'};
beforeEach(ngModule('vnCore')); beforeEach(ngModule('vnCore'));
beforeEach(angular.mock.inject(($componentController, _$state_, $rootScope) => { beforeEach(angular.mock.inject(($componentController, $rootScope, _$state_) => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$state = _$state_; $state = _$state_;
$element = angular.element(`<vn-textfield><input></input></vn-textfield>`); $params = $state.params;
$params.q = JSON.stringify(filter);
$element = angular.element(`<div></div>`);
controller = $componentController('vnSearchbar', {$element, $scope}); controller = $componentController('vnSearchbar', {$element, $scope});
controller.panel = 'vn-client-search-panel'; controller.panel = 'vn-client-search-panel';
})); }));
describe('$postLink()', () => { describe('$postLink()', () => {
it(`should not call onStateChange() if filter is defined`, () => { it(`should fetch the filter from the state if it's in the filter state`, () => {
spyOn(controller, 'onStateChange');
controller.filter = {};
controller.$postLink(); controller.$postLink();
expect(controller.onStateChange).not.toHaveBeenCalledWith(); expect(controller.filter).toEqual(filter);
expect(controller.searchString).toBe('needle');
expect(controller.params.length).toBe(1);
}); });
it(`should call onStateChange() if filter is null`, () => { it(`should not fetch the filter from the state if not in the filter state`, () => {
spyOn(controller, 'onStateChange'); controller.searchState = 'other.state';
controller.filter = null;
controller.$postLink(); controller.$postLink();
expect(controller.onStateChange).toHaveBeenCalledWith(); expect(controller.filter).toBeNull();
}); expect(controller.searchString).toBeNull();
}); expect(controller.params.length).toBe(0);
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', () => { describe('filter() setter', () => {
it(`should call $state.go() to replace the current state location instead of creating a new one`, () => { it(`should update the bar params and search`, () => {
controller._filter = {}; let withoutHours = new Date(2000, 1, 1);
spyOn($state, 'go'); let withHours = new Date(withoutHours.getTime());
controller.filter = {expected: 'filter'}; withHours.setHours(12, 30, 15, 10);
expect(controller._filter).toEqual(controller.filter); controller.filter = {
expect($state.go).toHaveBeenCalledWith('.', Object({q: '{"expected":"filter"}'}), Object({location: 'replace'})); 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');
});
});
describe('shownFilter() getter', () => {
it(`should return the _filter if not null`, () => {
controller.filter = filter;
controller.suggestedFilter = {sugestedParam: 'suggestedValue'};
expect(controller.shownFilter).toEqual(filter);
});
it(`should return the suggested filter if filter is null`, () => {
controller.filter = null;
controller.suggestedFilter = {sugestedParam: 'suggestedValue'};
expect(controller.shownFilter).toEqual(controller.suggestedFilter);
});
});
describe('searchString() setter', () => {
it(`should clear the whole filter when it's null`, () => {
controller.filter = filter;
controller.searchString = null;
expect(controller.searchString).toBeNull();
expect(controller.params.length).toBe(0);
}); });
}); });
describe('onPanelSubmit()', () => { describe('onPanelSubmit()', () => {
it(`should hide the popover, and set the filter as it wasn't defined`, () => { it(`should compact and define the filter`, () => {
controller.$.popover = {hide: jasmine.createSpy('hide')}; controller.$.popover = {hide: jasmine.createSpy('hide')};
const filter = undefined;
const filter = {
id: 1,
thisKeyShouldBePurged: null,
alsoThis: [],
andThis: {emptyProp: undefined, nullProp: null},
dontForgetThis: [null, undefined],
myObject: {keepThis: true, butNotThis: null},
myArray: [null, undefined, true]
};
controller.onPanelSubmit(filter); controller.onPanelSubmit(filter);
expect(controller.filter).toMatchSnapshot(); expect(controller.filter).toEqual({
}); id: 1,
myObject: {keepThis: true},
it(`should hide the popover, compact the filter`, () => { myArray: [true]
controller.$.popover = {hide: jasmine.createSpy('hide')}; });
const filter = {id: 999, thisKeyShouldBePurged: null};
controller.onPanelSubmit(filter);
expect(controller.filter).toMatchSnapshot();
}); });
}); });
describe('onSubmit()', () => { describe('onSubmit()', () => {
it(`should define the controller's filter`, () => { it(`should define the filter`, () => {
controller.searchString = 'id: 999'; controller.filter = filter;
controller.searchString = 'mySearch';
controller.onSubmit(); controller.onSubmit();
expect(controller.filter).toMatchSnapshot(); expect(controller.filter).toEqual({id: 1, search: 'mySearch'});
});
});
describe('removeParam()', () => {
it(`should remove the parameter from the filter`, () => {
controller.filter = filter;
controller.removeParam(0);
expect(controller.filter).toEqual({search: 'needle'});
}); });
}); });
describe('doSearch()', () => { describe('doSearch()', () => {
it(`should define searchString, call onSearch and the model applyFilter method`, () => { it(`should go to the search state and pass the filter as query param`, () => {
controller._filter = 'id: 123456'; spyOn($state, 'go');
controller.autoload = true; controller.searchState = 'search.state';
controller.onSearch = jasmine.createSpy('onSearch'); controller.doSearch(filter);
controller.model = {applyFilter: jasmine.createSpy('applyFilter')};
controller.paramBuilder = jasmine.createSpy('paramBuilder').and.returnValue({'param': `${controller._filter}`});
let queryParams = {q: JSON.stringify(filter)};
controller.doSearch(); expect($state.go).toHaveBeenCalledWith('search.state', queryParams, null);
expect(controller.filter).toEqual(filter);
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'
});
}); });
}); });
}); });

View File

@ -47,10 +47,8 @@ export default class Controller {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
let state = `ticket.index`; this.$state.go(`ticket.index`,
let params = {q: `{"clientFk": ${client.id}}`}; {q: JSON.stringify({clientFk: client.id})});
this.$state.go(state, params);
} }
} }
Controller.$inject = ['$scope', '$state']; Controller.$inject = ['$scope', '$state'];

View File

@ -1,27 +1,25 @@
import './index'; import './index';
describe('Client index', () => { describe('Client index', () => {
let $scope; let $state;
let controller; let controller;
beforeEach(ngModule('client')); beforeEach(ngModule('client'));
beforeEach(angular.mock.inject(($componentController, $rootScope, $state) => { beforeEach(angular.mock.inject(($componentController, _$state_) => {
$scope = $rootScope.$new(); $state = _$state_;
controller = $componentController('vnClientIndex', {$scope, $state}); controller = $componentController('vnClientIndex');
})); }));
describe('filterTickets()', () => { describe('filterTickets()', () => {
it('should navigate to the ticket index using params as filter', () => { it('should navigate to the ticket index using params as filter', () => {
const client = {id: 101}; const client = {id: 101};
const event = {preventDefault: () => {}}; const event = new MouseEvent('click', {cancelable: true});
spyOn(event, 'preventDefault'); spyOn($state, 'go');
spyOn(controller.$state, 'go');
controller.filterTickets(client, event); controller.filterTickets(client, event);
expect(event.preventDefault).toHaveBeenCalledWith(); expect($state.go).toHaveBeenCalledWith('ticket.index', jasmine.any(Object));
expect(controller.$state.go).toHaveBeenCalledWith('ticket.index', jasmine.any(Object));
}); });
}); });
}); });