4159-tableQ_filter #1069

Merged
alexm merged 21 commits from 4159-tableQ_filter into dev 2022-11-03 07:52:25 +00:00
5 changed files with 131 additions and 108 deletions
Showing only changes of commit 94c143bc4c - Show all commits

View File

@ -256,7 +256,6 @@ export default class Searchbar extends Component {
this.filter = filter; this.filter = filter;
if (source == 'removeBar') { if (source == 'removeBar') {
console.log(params);
delete params[this.toRemove]; delete params[this.toRemove];
delete this.model.userParams[this.toRemove]; delete this.model.userParams[this.toRemove];
this.model.refresh(); this.model.refresh();
@ -299,30 +298,10 @@ export default class Searchbar extends Component {
} else { } else {
params = Object.assign({}, filter); params = Object.assign({}, filter);
console.log('pre', params);
if (this.fetchParams) if (this.fetchParams)
params = this.fetchParams({$params: params}); 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; this.tableQ = null;
if (this.$params.q && Object.keys(JSON.parse(this.$params.q)).length) { if (this.$params.q && Object.keys(JSON.parse(this.$params.q)).length) {
alexm marked this conversation as resolved Outdated
Outdated
Review

Extraer esta parte a una variable que de contexto:

Object.keys(JSON.parse(this.$params.q)).length

Extraer esta parte a una variable que de contexto: `Object.keys(JSON.parse(this.$params.q)).length`
const stateFilter = JSON.parse(this.$params.q); const stateFilter = JSON.parse(this.$params.q);
@ -341,11 +320,9 @@ export default class Searchbar extends Component {
params[param] = stateFilter.tableQ[param]; params[param] = stateFilter.tableQ[param];
Object.assign(stateFilter, params); Object.assign(stateFilter, params);
console.log('PRE FINAL PARAMS: ', params);
return this.model.applyParams(params) return this.model.applyParams(params)
.then(() => this.model.data); .then(() => this.model.data);
} }
console.log('FINAL PARAMS: ', params);
return this.model.applyFilter(where ? {where} : null, params) return this.model.applyFilter(where ? {where} : null, params)
.then(() => this.model.data); .then(() => this.model.data);

View File

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

View File

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

View File

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