crud model test

This commit is contained in:
Bernat Exposito Domenech 2020-02-17 13:50:54 +01:00
parent c45ce23cd6
commit f900b7ef62
3 changed files with 206 additions and 20 deletions

View File

@ -100,7 +100,7 @@ export default class CrudModel extends ModelProxy {
}
removeFilter() {
return applyFilter(null, null);
return this.applyFilter(null, null);
}
/**
@ -240,14 +240,12 @@ export default class CrudModel extends ModelProxy {
onRemoteDone(json, filter, append) {
let data = json.data;
if (append)
this.orgData = this.orgData.concat(data);
else {
this.orgData = data;
this.currentFilter = filter;
}
this.data = this.proxiedData.slice();
this.moreRows = filter.limit && data.length == filter.limit;
this.onRequestEnd();

View File

@ -25,7 +25,7 @@ describe('Component vnCrudModel', () => {
});
describe('save()', () => {
it(`should make an HTTP post query and then update the original rows with the returned values`, () => {
it('should make an HTTP post query and then update the original rows with the returned values', () => {
spyOn(controller, 'applyChanges');
controller.insert({value: 'My new item 1'});
@ -47,4 +47,192 @@ describe('Component vnCrudModel', () => {
expect(controller.applyChanges).toHaveBeenCalledWith();
});
});
describe('setter url()', () => {
it('should set the url', () => {
spyOn(controller, 'autoRefresh');
spyOn(controller, 'clear');
controller.url = '/TestUrl';
expect(controller.url).toEqual('/TestUrl');
});
});
describe('isLoading()', () => {
it('should return false if canceler is null', () => {
controller.canceler = null;
expect(controller.isLoading).toBe(false);
});
it('should return true if canceler is not null', () => {
controller.canceler = 'validValue';
expect(controller.isLoading).toBe(true);
});
});
describe('autoRefresh()', () => {
it('', () => {
// spyOn(controller, 'autoRefresh');
// spyOn(controller, 'clear');
// controller.url = '/TestUrl';
// expect(controller.url).toEqual('/TestUrl');
});
});
describe('buildFilter()', () => {
it('should build a filter and return it', () => {
controller.order = 'id ASC';
controller.fields = ['id'];
controller.limit = 1;
controller.filter = 'filterTest';
const result = controller.buildFilter();
expect(Object.keys(result).length).toEqual(13);
});
});
describe('sendRequest()', () => {
it('should call refresh() and check that the sendRequest is called', () => {
spyOn(controller, 'sendRequest').and.callThrough();
spyOn(controller, 'onRemoteDone');
spyOn(controller, 'onRemoteError');
const filter = {id: 1};
const serializedParams = encodeURIComponent(JSON.stringify(filter));
$httpBackend.whenRoute('GET', `model?filter=${serializedParams}`).respond();
controller.sendRequest(filter, true);
$httpBackend.flush();
expect(controller.isPaging).toBe(false);
});
});
describe('addFilter()', () => {
it('should call addFilter and check that the new filter has been added', () => {
spyOn(controller, 'refresh');
const filter = {where: {id: 1}};
controller.userFilter = {where: {name: 'test'}};
const filterMerged = {'where': {'and': [{'name': 'test'}, {'id': 1}]}};
controller.addFilter(filter);
expect(controller.userFilter).toEqual(filterMerged);
expect(controller.refresh).toHaveBeenCalledWith();
});
});
describe('applyFilter()', () => {
it('should call applyFilter and check that the refresh() is called', () => {
spyOn(controller, 'refresh');
const filter = {where: {id: 1}};
const params = {where: {id: 2}};
controller.applyFilter(filter, params);
expect(controller.userFilter).toEqual(filter);
expect(controller.userParams).toEqual(params);
expect(controller.refresh).toHaveBeenCalledWith();
});
});
describe('removeFilter()', () => {
it('should check the userFilter and userParams are removed', () => {
controller.removeFilter();
expect(controller.userFilter).toBe(null);
expect(controller.userParams).toBe(null);
});
});
xdescribe('cancelRequest()', () => {
it('should check the resolve method is called and canceler is null', () => {
controller.canceler = {resolve: () => {}};
spyOn(controller.canceler, 'resolve');
controller.cancelRequest();
expect(controller.canceler).toBe(null);
});
});
describe('loadMore()', () => {
it('should call sendRequest with the new filter', () => {
spyOn(controller, 'sendRequest');
controller.moreRows = true;
controller.loadMore();
expect(controller.sendRequest).toHaveBeenCalledWith({'skip': 2}, true);
});
});
describe('clear()', () => {
it('should check that orgData and moreRows are set to null', () => {
controller.moreRows = true;
controller.clear();
expect(controller.moreRows).toBe(null);
expect(controller.orgData).toBe(null);
});
});
// describe('buildParams()', () => {
// it('', () => {
// // spyOn(controller, 'autoRefresh');
// // spyOn(controller, 'clear');
// // controller.url = '/TestUrl';
// // expect(controller.url).toEqual('/TestUrl');
// });
// });
describe('refresh()', () => {
it('shold resolve a fake promise if this._url is undefined', () => {
spyOn(controller.$q, 'resolve');
controller._url = undefined;
controller.refresh();
expect(controller.$q.resolve).toHaveBeenCalledWith();
});
});
describe('onRemoteDone()', () => {
it('', () => {
spyOn(controller, 'onRequestEnd');
const json = {data: [
{
id: 1,
name: 'test'
}]};
const filter = {limit: 1};
controller.onRemoteDone(json, filter, true);
expect(controller.moreRows).toBe(true);
expect(controller.onRequestEnd).toHaveBeenCalledWith();
});
});
xdescribe('onRemoteError()', () => {
it('should check the error', () => {
spyOn(controller, 'onRequestEnd');
controller.onRemoteError();
// expect(controller.url).toEqual('/TestUrl');
});
});
});

View File

@ -26,4 +26,4 @@
"permission": "ALLOW"
}
]
}
}