test(searchbar_smart-table): fix for integration
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2022-09-29 14:28:50 +02:00
parent 84003ab09c
commit 94c143bc4c
5 changed files with 131 additions and 108 deletions

View File

@ -256,7 +256,6 @@ export default class Searchbar extends Component {
this.filter = filter;
if (source == 'removeBar') {
console.log(params);
delete params[this.toRemove];
delete this.model.userParams[this.toRemove];
this.model.refresh();
@ -299,30 +298,10 @@ export default class Searchbar extends Component {
} else {
params = Object.assign({}, filter);
console.log('pre', params);
if (this.fetchParams)
params = this.fetchParams({$params: params});
console.log('post', params);
}
/* console.log(params);
const paramsKeys = Object.keys(params);
const suggestedKeys = Object.keys(this.suggestedFilter);
if (params != this.suggestedFilter) {
for (let suggested in this.suggestedFilter)
delete this.model.userParams[suggested];
}*/
console.log('this.fromBar()', this.fromBar());
console.log('this.$params.q', this.$params.q);
console.log('param', params);
console.log('userParams', this.model.userParams);
console.log('userFilter', this.model.userFilter);
console.log('suggestedFilter', this.suggestedFilter);
console.log('fetch-params', this.fetchParams);
/* if (this.fromBar()) {
for (let param in params)
delete this.model.userParams[param];
}*/
this.tableQ = null;
if (this.$params.q && Object.keys(JSON.parse(this.$params.q)).length) {
const stateFilter = JSON.parse(this.$params.q);
@ -341,11 +320,9 @@ export default class Searchbar extends Component {
params[param] = stateFilter.tableQ[param];
Object.assign(stateFilter, params);
console.log('PRE FINAL PARAMS: ', params);
return this.model.applyParams(params)
.then(() => this.model.data);
}
console.log('FINAL PARAMS: ', params);
return this.model.applyFilter(where ? {where} : null, params)
.then(() => this.model.data);

View File

@ -6,7 +6,7 @@ describe('Component vnSearchbar', () => {
let $state;
let $params;
let $scope;
let filter = {id: 1, search: 'needle'};
const filter = {id: 1, search: 'needle'};
beforeEach(ngModule('vnCore', $stateProvider => {
$stateProvider
@ -70,8 +70,8 @@ describe('Component vnSearchbar', () => {
describe('filter() setter', () => {
it(`should update the bar params and search`, () => {
let withoutHours = new Date(2000, 1, 1);
let withHours = new Date(withoutHours.getTime());
const withoutHours = new Date(2000, 1, 1);
const withHours = new Date(withoutHours.getTime());
withHours.setHours(12, 30, 15, 10);
controller.filter = {
@ -83,8 +83,8 @@ describe('Component vnSearchbar', () => {
myObjectProp: {myProp: 1}
};
let chips = {};
for (let param of controller.params || [])
const chips = {};
for (const param of controller.params || [])
chips[param.key] = param.chip;
expect(controller.searchString).toBe('needle');
@ -173,11 +173,15 @@ describe('Component vnSearchbar', () => {
it(`should remove the parameter from the filter`, () => {
jest.spyOn(controller, 'doSearch');
controller.model = {
applyParams: () => {
return new Promise(resolve => resolve());
refresh: jest.fn(),
userParams: {
id: 1
}
};
controller.model.applyParams = jest.fn().mockReturnValue(Promise.resolve());
jest.spyOn(controller.model, 'applyParams');
controller.filter = filter;
controller.removeParam(0);
@ -204,7 +208,7 @@ describe('Component vnSearchbar', () => {
it(`should go to the summary state when one result`, () => {
jest.spyOn($state, 'go');
let data = [{id: 1}];
const data = [{id: 1}];
controller.baseState = 'foo';
controller.onFilter(filter, 'any', data);
@ -219,7 +223,7 @@ describe('Component vnSearchbar', () => {
$scope.$apply();
jest.spyOn($state, 'go');
let data = [{id: 1}];
const data = [{id: 1}];
controller.baseState = 'foo';
controller.onFilter(filter, 'any', data);
@ -234,7 +238,7 @@ describe('Component vnSearchbar', () => {
$scope.$apply();
jest.spyOn($state, 'go');
let data = [{id: 1}];
const data = [{id: 1}];
controller.baseState = 'foo';
controller.onFilter(filter, 'any', data);
@ -252,7 +256,7 @@ describe('Component vnSearchbar', () => {
controller.onFilter(filter, 'any');
$scope.$apply();
let queryParams = {q: JSON.stringify(filter)};
const queryParams = {q: JSON.stringify(filter)};
expect($state.go).toHaveBeenCalledWith('search.state', queryParams, undefined);
expect(controller.filter).toEqual(filter);

View File

@ -103,7 +103,4 @@
</div>
</tpl-body>
</vn-popover>
<vn-button
label="TEST"
ng-click="$ctrl.test()">
</vn-button>

View File

@ -229,7 +229,7 @@ export default class SmartTable extends Component {
}
this.model.order = order;
this.model.refresh();
this.refresh();
}
registerColumns() {
@ -429,6 +429,14 @@ export default class SmartTable extends Component {
this.searchByColumn(field);
}
searchByColumn(field) {
const filters = this.filterSanitizer(field);
if (filters && filters.userFilter)
this.model.userFilter = filters.userFilter;
this.addFilter(field, this.$inputsScope.searchProps[field]);
}
searchPropsSanitizer() {
if (!this.$inputsScope || !this.$inputsScope.searchProps) return null;
let searchProps = this.$inputsScope.searchProps;
@ -440,14 +448,6 @@ export default class SmartTable extends Component {
return Object.fromEntries(searchProps);
}
searchByColumn(field) {
const filters = this.filterSanitizer(field);
if (filters && filters.userFilter)
this.model.userFilter = filters.userFilter;
this.addFilter(field, this.$inputsScope.searchProps[field]);
}
addFilter(field, value) {
if (value == '') value = null;
@ -477,7 +477,7 @@ export default class SmartTable extends Component {
const params = {q: JSON.stringify(stateFilter)};
this.$state.go(this.$state.current.name, params, {location: 'replace'});
this.model.refresh();
this.refresh();
}
applySort() {
@ -498,7 +498,7 @@ export default class SmartTable extends Component {
const params = {q: JSON.stringify(stateFilter)};
this.$state.go(this.$state.current.name, params, {location: 'replace'});
this.model.refresh();
this.refresh();
}
filterSanitizer(field) {
@ -589,10 +589,6 @@ export default class SmartTable extends Component {
this.model.refresh()
.then(() => this.isRefreshing = false);
}
test() {
console.log('USER_FILTER', this.model.userFilter, 'USER_PARAMS', this.model.userParams);
}
}
SmartTable.$inject = ['$element', '$scope', '$transclude'];

View File

@ -9,6 +9,11 @@ describe('Component smartTable', () => {
$httpBackend = _$httpBackend_;
$element = $compile(`<smart-table></smart-table>`)($rootScope);
controller = $element.controller('smartTable');
controller.model = {
refresh: jest.fn().mockReturnValue(new Promise(resolve => resolve())),
addFilter: jest.fn(),
userParams: {}
};
}));
afterEach(() => {
@ -83,7 +88,7 @@ describe('Component smartTable', () => {
describe('defaultOrder', () => {
it('should insert a new object to the controller sortCriteria with a sortType value of "ASC"', () => {
const element = document.createElement('div');
controller.model = {order: 'id'};
controller.model.order = 'id';
controller.columns = [
{field: 'id', element: element},
{field: 'test1', element: element},
@ -101,7 +106,8 @@ describe('Component smartTable', () => {
it('should add new entries to the controller sortCriteria with a sortType values of "ASC" and "DESC"', () => {
const element = document.createElement('div');
controller.model = {order: 'test1, id DESC'};
controller.model.order = 'test1, id DESC';
controller.columns = [
{field: 'id', element: element},
{field: 'test1', element: element},
@ -125,8 +131,6 @@ describe('Component smartTable', () => {
describe('addFilter()', () => {
it('should call the model addFilter() with a basic where filter if exprBuilder() was not received', () => {
controller.model = {addFilter: jest.fn()};
controller.addFilter('myField', 'myValue');
const expectedFilter = {
@ -140,7 +144,6 @@ describe('Component smartTable', () => {
it('should call the model addFilter() with a built where filter resultant of exprBuilder()', () => {
controller.exprBuilder = jest.fn().mockReturnValue({builtField: 'builtValue'});
controller.model = {addFilter: jest.fn()};
controller.addFilter('myField', 'myValue');
@ -155,35 +158,48 @@ describe('Component smartTable', () => {
});
describe('applySort()', () => {
it('should call the model refresh() without making changes on the model order', () => {
controller.model = {refresh: jest.fn()};
it('should call the $state go and model refresh without making changes on the model order', () => {
controller.$state = {
go: jest.fn(),
current: {
name: 'section'
}
};
jest.spyOn(controller, 'refresh');
controller.applySort();
expect(controller.model.order).toBeUndefined();
expect(controller.model.refresh).toHaveBeenCalled();
expect(controller.$state.go).toHaveBeenCalled();
expect(controller.refresh).toHaveBeenCalled();
});
it('should call the model.refresh() after setting model order according to the controller sortCriteria', () => {
controller.model = {refresh: jest.fn()};
it('should call the $state go and model refresh after setting model order according to the controller sortCriteria', () => {
const orderBy = {field: 'myField', sortType: 'ASC'};
controller.$state = {
go: jest.fn(),
current: {
name: 'section'
}
};
jest.spyOn(controller, 'refresh');
controller.sortCriteria = [orderBy];
controller.applySort();
expect(controller.model.order).toEqual(`${orderBy.field} ${orderBy.sortType}`);
expect(controller.model.refresh).toHaveBeenCalled();
expect(controller.$state.go).toHaveBeenCalled();
expect(controller.refresh).toHaveBeenCalled();
});
});
describe('filterSanitizer()', () => {
it('should remove the where filter after leaving no fields in it', () => {
controller.model = {
userFilter: {
where: {fieldToRemove: 'valueToRemove'}
},
userParams: {}
controller.model.userFilter = {
where: {fieldToRemove: 'valueToRemove'}
};
controller.model.userParams = {};
const result = controller.filterSanitizer('fieldToRemove');
@ -193,23 +209,21 @@ describe('Component smartTable', () => {
});
it('should remove the where filter after leaving no fields and "empty ands/ors" in it', () => {
controller.model = {
userFilter: {
where: {
and: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
{
or: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
]
}
]
}
},
userParams: {}
};
controller.model.userFilter = {
where: {
and: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
{
or: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
]
}
]
}
},
controller.model.userParams = {};
const result = controller.filterSanitizer('aFieldToRemove');
@ -219,24 +233,22 @@ describe('Component smartTable', () => {
});
it('should not remove the where filter after leaving no empty "ands/ors" in it', () => {
controller.model = {
userFilter: {
where: {
and: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
{
or: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
]
}
],
or: [{dontKillMe: 'thanks'}]
}
},
userParams: {}
controller.model.userFilter = {
where: {
and: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
{
or: [
{aFieldToRemove: 'aValueToRemove'},
{aFieldToRemove: 'aValueToRemove'},
]
}
],
or: [{dontKillMe: 'thanks'}]
}
};
controller.model.userParams = {};
const result = controller.filterSanitizer('aFieldToRemove');
@ -249,7 +261,7 @@ describe('Component smartTable', () => {
describe('saveAll()', () => {
it('should throw an error if there are no changes to save in the model', () => {
jest.spyOn(controller.vnApp, 'showError');
controller.model = {isChanged: false};
controller.model.isChanged = false;
controller.saveAll();
expect(controller.vnApp.showError).toHaveBeenCalledWith('No changes to save');
@ -258,10 +270,8 @@ describe('Component smartTable', () => {
it('should call the showSuccess() if there are changes to save in the model', done => {
jest.spyOn(controller.vnApp, 'showSuccess');
controller.model = {
save: jest.fn().mockReturnValue(Promise.resolve()),
isChanged: true
};
controller.model.save = jest.fn().mockReturnValue(Promise.resolve());
controller.model.isChanged = true;
controller.saveAll().then(() => {
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
@ -269,4 +279,43 @@ describe('Component smartTable', () => {
}).catch(done.fail);
});
});
describe('defaultFilter()', () => {
it('should call model refresh and model addFilter with filter', () => {
controller.exprBuilder = jest.fn().mockReturnValue({builtField: 'builtValue'});
controller.$params = {
q: '{"tableQ": {"fieldName":"value"}}'
};
controller.columns = [
{field: 'fieldName'}
];
controller.$inputsScope = {
searchProps: {}
};
jest.spyOn(controller, 'refresh');
controller.defaultFilter();
expect(controller.model.addFilter).toHaveBeenCalled();
expect(controller.refresh).toHaveBeenCalled();
});
});
describe('searchPropsSanitizer()', () => {
it('should searchProps sanitize', () => {
controller.$inputsScope = {
searchProps: {
filterOne: '1',
filterTwo: ''
}
};
const searchPropsExpected = {
filterOne: '1'
};
const newSearchProps = controller.searchPropsSanitizer();
expect(newSearchProps).toEqual(searchPropsExpected);
});
});
});