2021-11-18 10:04:51 +00:00
|
|
|
describe('Component smartTable', () => {
|
|
|
|
let $element;
|
|
|
|
let controller;
|
|
|
|
let $httpBackend;
|
|
|
|
|
|
|
|
beforeEach(ngModule('vnCore'));
|
|
|
|
|
|
|
|
beforeEach(inject(($compile, $rootScope, _$httpBackend_) => {
|
|
|
|
$httpBackend = _$httpBackend_;
|
|
|
|
$element = $compile(`<smart-table></smart-table>`)($rootScope);
|
|
|
|
controller = $element.controller('smartTable');
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model = {
|
|
|
|
refresh: jest.fn().mockReturnValue(new Promise(resolve => resolve())),
|
|
|
|
addFilter: jest.fn(),
|
|
|
|
userParams: {}
|
|
|
|
};
|
2021-11-18 10:04:51 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
$element.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('options setter()', () => {
|
|
|
|
it(`should throw an error if the table doesn't have an identifier`, () => {
|
|
|
|
const options = {activeButtons: {shownColumns: []}};
|
|
|
|
|
|
|
|
expect(() => controller.options = options).toThrowError(/View identifier not defined/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw an error if the table does have an identifier', () => {
|
|
|
|
const options = {activeButtons: {shownColumns: []}};
|
|
|
|
controller.viewConfigId = 'test';
|
|
|
|
|
|
|
|
expect(() => controller.options = options).not.toThrow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getDefaultViewConfig()', () => {
|
|
|
|
it('should perform a query and return the default view columns', done => {
|
|
|
|
const expectedResponse = [{
|
|
|
|
columns: {}
|
|
|
|
}];
|
|
|
|
|
|
|
|
$httpBackend.expectGET('DefaultViewConfigs').respond(expectedResponse);
|
|
|
|
controller.getDefaultViewConfig().then(columns => {
|
|
|
|
expect(columns).toEqual(expectedResponse[0].columns);
|
|
|
|
done();
|
|
|
|
}).catch(done.fail);
|
|
|
|
$httpBackend.flush();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('viewConfig setter', () => {
|
|
|
|
it('should just call applyViewConfig() if a viewConfig was provided', () => {
|
|
|
|
spyOn(controller, 'applyViewConfig');
|
|
|
|
controller.viewConfig = [{}];
|
|
|
|
|
|
|
|
expect(controller.applyViewConfig).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not get a defaultConfig then insert a new one', () => {
|
|
|
|
spyOn(controller, 'applyViewConfig');
|
|
|
|
|
|
|
|
controller.$.userViewModel = {
|
|
|
|
insert: jest.fn()
|
|
|
|
};
|
|
|
|
|
|
|
|
const emptyResponse = [{
|
|
|
|
columns: {}
|
|
|
|
}];
|
|
|
|
|
|
|
|
controller.columns = [
|
|
|
|
{field: 'test1'},
|
|
|
|
{field: 'test2'}
|
|
|
|
];
|
|
|
|
|
|
|
|
$httpBackend.expectGET('DefaultViewConfigs').respond(emptyResponse);
|
|
|
|
controller.viewConfig = [];
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
const expectedObject = {configuration: {'test1': true, 'test2': true}};
|
|
|
|
|
|
|
|
expect(controller.$.userViewModel.insert).toHaveBeenCalledWith(expect.objectContaining(expectedObject));
|
|
|
|
expect(controller.applyViewConfig).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-21 09:37:29 +00:00
|
|
|
describe('defaultOrder', () => {
|
|
|
|
it('should insert a new object to the controller sortCriteria with a sortType value of "ASC"', () => {
|
|
|
|
const element = document.createElement('div');
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.order = 'id';
|
2021-12-21 09:37:29 +00:00
|
|
|
controller.columns = [
|
|
|
|
{field: 'id', element: element},
|
|
|
|
{field: 'test1', element: element},
|
|
|
|
{field: 'test2', element: element}
|
|
|
|
];
|
|
|
|
|
|
|
|
controller.defaultOrder();
|
|
|
|
|
|
|
|
const firstSortCriteria = controller.sortCriteria[0];
|
|
|
|
|
|
|
|
expect(firstSortCriteria.field).toEqual('id');
|
|
|
|
expect(firstSortCriteria.sortType).toEqual('ASC');
|
2022-01-20 08:53:40 +00:00
|
|
|
expect(firstSortCriteria.priority).toEqual(1);
|
2021-12-21 09:37:29 +00:00
|
|
|
});
|
|
|
|
|
2022-01-20 08:53:40 +00:00
|
|
|
it('should add new entries to the controller sortCriteria with a sortType values of "ASC" and "DESC"', () => {
|
2021-12-21 09:37:29 +00:00
|
|
|
const element = document.createElement('div');
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.order = 'test1, id DESC';
|
|
|
|
|
2021-12-21 09:37:29 +00:00
|
|
|
controller.columns = [
|
|
|
|
{field: 'id', element: element},
|
|
|
|
{field: 'test1', element: element},
|
|
|
|
{field: 'test2', element: element}
|
|
|
|
];
|
|
|
|
|
|
|
|
controller.defaultOrder();
|
|
|
|
|
|
|
|
const firstSortCriteria = controller.sortCriteria[0];
|
|
|
|
const secondSortCriteria = controller.sortCriteria[1];
|
|
|
|
|
|
|
|
expect(firstSortCriteria.field).toEqual('test1');
|
|
|
|
expect(firstSortCriteria.sortType).toEqual('ASC');
|
2022-01-20 08:53:40 +00:00
|
|
|
expect(firstSortCriteria.priority).toEqual(1);
|
2022-01-20 09:43:03 +00:00
|
|
|
|
2021-12-21 09:37:29 +00:00
|
|
|
expect(secondSortCriteria.field).toEqual('id');
|
|
|
|
expect(secondSortCriteria.sortType).toEqual('DESC');
|
2022-01-20 09:43:03 +00:00
|
|
|
expect(secondSortCriteria.priority).toEqual(2);
|
2021-12-21 09:37:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
describe('addFilter()', () => {
|
|
|
|
it('should call the model addFilter() with a basic where filter if exprBuilder() was not received', () => {
|
|
|
|
controller.addFilter('myField', 'myValue');
|
|
|
|
|
|
|
|
const expectedFilter = {
|
|
|
|
where: {
|
|
|
|
myField: 'myValue'
|
|
|
|
}
|
2021-11-18 10:04:51 +00:00
|
|
|
};
|
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
expect(controller.model.addFilter).toHaveBeenCalledWith(expectedFilter);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call the model addFilter() with a built where filter resultant of exprBuilder()', () => {
|
|
|
|
controller.exprBuilder = jest.fn().mockReturnValue({builtField: 'builtValue'});
|
|
|
|
|
|
|
|
controller.addFilter('myField', 'myValue');
|
|
|
|
|
|
|
|
const expectedFilter = {
|
|
|
|
where: {
|
|
|
|
builtField: 'builtValue'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(controller.model.addFilter).toHaveBeenCalledWith(expectedFilter);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('applySort()', () => {
|
2022-09-29 12:28:50 +00:00
|
|
|
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');
|
2021-11-23 12:58:12 +00:00
|
|
|
|
|
|
|
controller.applySort();
|
|
|
|
|
|
|
|
expect(controller.model.order).toBeUndefined();
|
2022-09-29 12:28:50 +00:00
|
|
|
expect(controller.$state.go).toHaveBeenCalled();
|
|
|
|
expect(controller.refresh).toHaveBeenCalled();
|
2021-11-23 12:58:12 +00:00
|
|
|
});
|
|
|
|
|
2022-09-29 12:28:50 +00:00
|
|
|
it('should call the $state go and model refresh after setting model order according to the controller sortCriteria', () => {
|
2021-11-23 12:58:12 +00:00
|
|
|
const orderBy = {field: 'myField', sortType: 'ASC'};
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.$state = {
|
|
|
|
go: jest.fn(),
|
|
|
|
current: {
|
|
|
|
name: 'section'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
jest.spyOn(controller, 'refresh');
|
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
controller.sortCriteria = [orderBy];
|
|
|
|
|
|
|
|
controller.applySort();
|
|
|
|
|
|
|
|
expect(controller.model.order).toEqual(`${orderBy.field} ${orderBy.sortType}`);
|
2022-09-29 12:28:50 +00:00
|
|
|
expect(controller.$state.go).toHaveBeenCalled();
|
|
|
|
expect(controller.refresh).toHaveBeenCalled();
|
2021-11-23 12:58:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('filterSanitizer()', () => {
|
|
|
|
it('should remove the where filter after leaving no fields in it', () => {
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.userFilter = {
|
|
|
|
where: {fieldToRemove: 'valueToRemove'}
|
2021-11-23 12:58:12 +00:00
|
|
|
};
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.userParams = {};
|
2021-11-18 10:04:51 +00:00
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
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', () => {
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.userFilter = {
|
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
{
|
|
|
|
or: [
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
controller.model.userParams = {};
|
2021-11-23 12:58:12 +00:00
|
|
|
|
|
|
|
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', () => {
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.userFilter = {
|
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
{
|
|
|
|
or: [
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
{aFieldToRemove: 'aValueToRemove'},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
or: [{dontKillMe: 'thanks'}]
|
|
|
|
}
|
2021-11-23 12:58:12 +00:00
|
|
|
};
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.userParams = {};
|
2021-11-23 12:58:12 +00:00
|
|
|
|
|
|
|
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');
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.isChanged = false;
|
2021-11-23 12:58:12 +00:00
|
|
|
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');
|
|
|
|
|
2022-09-29 12:28:50 +00:00
|
|
|
controller.model.save = jest.fn().mockReturnValue(Promise.resolve());
|
|
|
|
controller.model.isChanged = true;
|
2021-11-23 12:58:12 +00:00
|
|
|
|
|
|
|
controller.saveAll().then(() => {
|
|
|
|
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
|
|
|
|
done();
|
|
|
|
}).catch(done.fail);
|
2021-11-18 10:04:51 +00:00
|
|
|
});
|
|
|
|
});
|
2022-09-29 12:28:50 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2021-11-18 10:04:51 +00:00
|
|
|
});
|