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();
});
describe('onClick(event)', () => {
describe('onButtonClick(event)', () => {
it(`should emit the open event`, () => {
spyOn(controller, 'emit');
@ -23,7 +23,7 @@ describe('Component vnButtonMenu', () => {
bubbles: true,
cancelable: true
});
controller.onClick(event);
controller.onButtonClick(event);
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();
}
get filter() {
return this._filter;
}
set filter(value) {
this._filter = value;
this.toBar(value);
}
get shownFilter() {
return this.filter != null
? this.filter
: this.suggestedFilter;
}
get searchString() {
return this._searchString;
}
set searchString(value) {
this._searchString = value;
if (value == null) this.params = [];
}
onStateChange() {
let filter = null;
if (this.$state.is(this.searchState)) {
if (this.$state.params.q) {
if (this.$params.q) {
try {
this.filter = JSON.parse(this.$state.params.q);
filter = JSON.parse(this.$params.q);
} catch (e) {
console.error(e);
}
} else
this.filter = null;
}
focus(this.element.querySelector('vn-textfield input'));
} else
this.filter = null;
}
this.toBar(this.filter);
this.filter = filter;
}
openPanel(event) {
@ -94,16 +112,9 @@ export default class Controller extends Component {
this.doSearch(this.fromBar());
}
get searchString() {
return this._searchString;
}
set searchString(value) {
this._searchString = value;
if (value == null) this.params = [];
}
doSearch(filter) {
this.filter = filter;
let opts = this.$state.is(this.searchState)
? {location: 'replace'} : null;
this.$state.go(this.searchState,

View File

@ -4,164 +4,149 @@ describe('Component vnSearchbar', () => {
let controller;
let $element;
let $state;
let $params;
let $scope;
let filter = {id: 1, search: 'needle'};
beforeEach(ngModule('vnCore'));
beforeEach(angular.mock.inject(($componentController, _$state_, $rootScope) => {
beforeEach(angular.mock.inject(($componentController, $rootScope, _$state_) => {
$scope = $rootScope.$new();
$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.panel = 'vn-client-search-panel';
}));
describe('$postLink()', () => {
it(`should not call onStateChange() if filter is defined`, () => {
spyOn(controller, 'onStateChange');
controller.filter = {};
it(`should fetch the filter from the state if it's in the filter state`, () => {
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`, () => {
spyOn(controller, 'onStateChange');
controller.filter = null;
it(`should not fetch the filter from the state if not in the filter state`, () => {
controller.searchState = 'other.state';
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);
expect(controller.filter).toBeNull();
expect(controller.searchString).toBeNull();
expect(controller.params.length).toBe(0);
});
});
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'};
it(`should update the bar params and search`, () => {
let withoutHours = new Date(2000, 1, 1);
let withHours = new Date(withoutHours.getTime());
withHours.setHours(12, 30, 15, 10);
expect(controller._filter).toEqual(controller.filter);
expect($state.go).toHaveBeenCalledWith('.', Object({q: '{"expected":"filter"}'}), Object({location: 'replace'}));
controller.filter = {
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()', () => {
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')};
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);
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();
expect(controller.filter).toEqual({
id: 1,
myObject: {keepThis: true},
myArray: [true]
});
});
});
describe('onSubmit()', () => {
it(`should define the controller's filter`, () => {
controller.searchString = 'id: 999';
it(`should define the filter`, () => {
controller.filter = filter;
controller.searchString = 'mySearch';
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()', () => {
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}`});
it(`should go to the search state and pass the filter as query param`, () => {
spyOn($state, 'go');
controller.searchState = 'search.state';
controller.doSearch(filter);
let queryParams = {q: JSON.stringify(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'
});
expect($state.go).toHaveBeenCalledWith('search.state', queryParams, null);
expect(controller.filter).toEqual(filter);
});
});
});

View File

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

View File

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