refactor(smartTable): removeProp updated and tests
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
320cc6c02c
commit
372d5ade17
|
@ -223,26 +223,29 @@ export default class Contextmenu {
|
||||||
delete userFilter.where;
|
delete userFilter.where;
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeProp(instance, findProp, prop) {
|
function removeProp(obj, targetProp, prop) {
|
||||||
if (prop == findProp)
|
if (prop == targetProp)
|
||||||
delete instance[prop];
|
delete obj[prop];
|
||||||
|
|
||||||
if (prop === 'and' || prop === 'or') {
|
if (prop === 'and' || prop === 'or') {
|
||||||
const instanceCopy = instance[prop].slice();
|
const arrayCopy = obj[prop].slice();
|
||||||
for (let param of instanceCopy) {
|
for (let param of arrayCopy) {
|
||||||
const [key] = Object.keys(param);
|
const [key] = Object.keys(param);
|
||||||
const index = instance[prop].findIndex(param => {
|
const index = obj[prop].findIndex(param => {
|
||||||
return Object.keys(param)[0] == key;
|
return Object.keys(param)[0] == key;
|
||||||
});
|
});
|
||||||
if (key == findProp)
|
if (key == targetProp)
|
||||||
instance[prop].splice(index, 1);
|
obj[prop].splice(index, 1);
|
||||||
|
|
||||||
if (param[key] instanceof Array)
|
if (param[key] instanceof Array)
|
||||||
removeProp(param, filterKey, key);
|
removeProp(param, filterKey, key);
|
||||||
|
|
||||||
|
if (Object.keys(param).length == 0)
|
||||||
|
obj[prop].splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance[prop].length == 0)
|
if (obj[prop].length == 0)
|
||||||
delete instance[prop];
|
delete obj[prop];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -365,23 +365,33 @@ export default class SmartTable extends Component {
|
||||||
for (let key of whereKeys) {
|
for (let key of whereKeys) {
|
||||||
removeProp(where, field, key);
|
removeProp(where, field, key);
|
||||||
|
|
||||||
if (!Object.keys(where))
|
if (Object.keys(where).length == 0)
|
||||||
delete userFilter.where;
|
delete userFilter.where;
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeProp(instance, findProp, prop) {
|
function removeProp(obj, targetProp, prop) {
|
||||||
if (prop == findProp)
|
if (prop == targetProp)
|
||||||
delete instance[prop];
|
delete obj[prop];
|
||||||
|
|
||||||
if (prop === 'and') {
|
if (prop === 'and' || prop === 'or') {
|
||||||
for (let [index, param] of instance[prop].entries()) {
|
const arrayCopy = obj[prop].slice();
|
||||||
|
for (let param of arrayCopy) {
|
||||||
const [key] = Object.keys(param);
|
const [key] = Object.keys(param);
|
||||||
if (key == findProp)
|
const index = obj[prop].findIndex(param => {
|
||||||
instance[prop].splice(index, 1);
|
return Object.keys(param)[0] == key;
|
||||||
|
});
|
||||||
|
if (key == targetProp)
|
||||||
|
obj[prop].splice(index, 1);
|
||||||
|
|
||||||
if (param[key] instanceof Array)
|
if (param[key] instanceof Array)
|
||||||
removeProp(param, field, key);
|
removeProp(param, field, key);
|
||||||
|
|
||||||
|
if (Object.keys(param).length == 0)
|
||||||
|
obj[prop].splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (obj[prop].length == 0)
|
||||||
|
delete obj[prop];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,7 +425,7 @@ export default class SmartTable extends Component {
|
||||||
if (!model.isChanged)
|
if (!model.isChanged)
|
||||||
return this.vnApp.showError(this.$t('No changes to save'));
|
return this.vnApp.showError(this.$t('No changes to save'));
|
||||||
|
|
||||||
this.model.save()
|
return this.model.save()
|
||||||
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,18 +80,150 @@ describe('Component smartTable', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('applyViewConfig()', () => {
|
describe('addFilter()', () => {
|
||||||
it('should ', () => {
|
it('should call the model addFilter() with a basic where filter if exprBuilder() was not received', () => {
|
||||||
controller.$.userViewModel = {
|
controller.model = {addFilter: jest.fn()};
|
||||||
viewConfig: {'test1': true, 'test2': false}
|
|
||||||
|
controller.addFilter('myField', 'myValue');
|
||||||
|
|
||||||
|
const expectedFilter = {
|
||||||
|
where: {
|
||||||
|
myField: 'myValue'
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
controller._columns = [
|
expect(controller.model.addFilter).toHaveBeenCalledWith(expectedFilter);
|
||||||
{field: 'test1'},
|
});
|
||||||
{field: 'test2'}
|
|
||||||
];
|
|
||||||
|
|
||||||
controller.applyViewConfig();
|
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');
|
||||||
|
|
||||||
|
const expectedFilter = {
|
||||||
|
where: {
|
||||||
|
builtField: 'builtValue'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(controller.model.addFilter).toHaveBeenCalledWith(expectedFilter);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('applySort()', () => {
|
||||||
|
it('should call the model refresh() without making changes on the model order', () => {
|
||||||
|
controller.model = {refresh: jest.fn()};
|
||||||
|
|
||||||
|
controller.applySort();
|
||||||
|
|
||||||
|
expect(controller.model.order).toBeUndefined();
|
||||||
|
expect(controller.model.refresh).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call the model.refresh() after setting model order according to the controller sortCriteria', () => {
|
||||||
|
controller.model = {refresh: jest.fn()};
|
||||||
|
const orderBy = {field: 'myField', sortType: 'ASC'};
|
||||||
|
controller.sortCriteria = [orderBy];
|
||||||
|
|
||||||
|
controller.applySort();
|
||||||
|
|
||||||
|
expect(controller.model.order).toEqual(`${orderBy.field} ${orderBy.sortType}`);
|
||||||
|
expect(controller.model.refresh).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('filterSanitizer()', () => {
|
||||||
|
it('should remove the where filter after leaving no fields in it', () => {
|
||||||
|
controller.model = {
|
||||||
|
userFilter: {
|
||||||
|
where: {fieldToRemove: 'valueToRemove'}
|
||||||
|
},
|
||||||
|
userParams: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = controller.filterSanitizer('fieldToRemove');
|
||||||
|
|
||||||
|
const exectedObject = {userFilter: {}, userParams: {}};
|
||||||
|
|
||||||
|
expect(result).toEqual(exectedObject);
|
||||||
|
});
|
||||||
|
|
||||||
|
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: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = controller.filterSanitizer('aFieldToRemove');
|
||||||
|
|
||||||
|
const exectedObject = {userFilter: {}, userParams: {}};
|
||||||
|
|
||||||
|
expect(result).toEqual(exectedObject);
|
||||||
|
});
|
||||||
|
|
||||||
|
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: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = controller.filterSanitizer('aFieldToRemove');
|
||||||
|
|
||||||
|
const exectedObject = {userFilter: {where: {or: [{dontKillMe: 'thanks'}]}}, userParams: {}};
|
||||||
|
|
||||||
|
expect(result).toEqual(exectedObject);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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.saveAll();
|
||||||
|
|
||||||
|
expect(controller.vnApp.showError).toHaveBeenCalledWith('No changes to save');
|
||||||
|
});
|
||||||
|
|
||||||
|
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.saveAll().then(() => {
|
||||||
|
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
|
||||||
|
done();
|
||||||
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue