salix/front/core/components/crud-model/index.spec.js

233 lines
7.3 KiB
JavaScript
Raw Normal View History

2020-02-06 12:38:46 +00:00
describe('Component vnCrudModel', () => {
let $httpBackend;
let controller;
let $element;
beforeEach(ngModule('vnCore'));
beforeEach(inject(($compile, $rootScope, _$httpBackend_) => {
$element = $compile(`<vn-crud-model></vn-crud-model>`)($rootScope);
$httpBackend = _$httpBackend_;
controller = $element.controller('vnCrudModel');
controller.orgData = [
{id: 1, value: 'My item 1'},
{id: 2, value: 'My item 2'}
];
controller.data = [
{id: 1, value: 'My item 1'},
{id: 2, value: 'My item 2'}
];
controller._url = 'Model';
}));
afterEach(() => {
$element.remove();
});
describe('save()', () => {
it(`should make an HTTP post query and then update the original rows with the returned values`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'applyChanges');
2020-02-06 12:38:46 +00:00
controller.insert({value: 'My new item 1'});
controller.insert({value: 'My new item 2'});
$httpBackend.when('POST', 'Model/crud').respond([
{id: 3, value: 'My new item 1'},
{id: 4, value: 'My modified item 2'}
]);
controller.save();
$httpBackend.flush();
const thirdRow = controller.data[2];
const fourthRow = controller.data[3];
expect(thirdRow.id).toEqual(3);
expect(fourthRow.id).toEqual(4);
expect(fourthRow.value).toEqual('My modified item 2');
expect(controller.applyChanges).toHaveBeenCalledWith();
});
});
2020-02-17 12:50:54 +00:00
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('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);
});
});
describe('loadMore()', () => {
it('should call sendRequest with the new filter', () => {
spyOn(controller, 'sendRequest');
controller.moreRows = true;
controller.loadMore(true);
2020-02-17 12:50:54 +00:00
expect(controller.sendRequest).toHaveBeenCalledWith({'skip': 2}, true);
2020-02-17 12:50:54 +00:00
});
});
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('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()', () => {
2020-02-26 07:24:02 +00:00
it('should check onRequestEnd is called, moreRows is true and currentFilter is undefined when append is true', () => {
2020-02-17 12:50:54 +00:00
spyOn(controller, 'onRequestEnd');
2020-02-26 07:57:51 +00:00
const append = true;
2020-02-17 12:50:54 +00:00
const json = {data: [
{
id: 1,
name: 'test'
}]};
const filter = {limit: 1};
2020-02-26 07:57:51 +00:00
controller.onRemoteDone(json, filter, append);
2020-02-17 12:50:54 +00:00
expect(controller.moreRows).toBe(true);
2020-02-26 07:57:51 +00:00
expect(controller.currentFilter).toBeUndefined();
2020-02-26 07:24:02 +00:00
expect(controller.onRequestEnd).toHaveBeenCalledWith();
});
it('should check onRequestEnd is called, moreRows is true and currentFilter is defined when append is false', () => {
spyOn(controller, 'onRequestEnd');
2020-02-26 07:57:51 +00:00
const append = false;
2020-02-26 07:24:02 +00:00
const json = {data: [
{
id: 1,
name: 'test'
}]};
const filter = {limit: 1};
2020-02-26 07:57:51 +00:00
controller.onRemoteDone(json, filter, append);
2020-02-26 07:24:02 +00:00
expect(controller.moreRows).toBe(true);
expect(controller.currentFilter).toBe(filter);
2020-02-17 12:50:54 +00:00
expect(controller.onRequestEnd).toHaveBeenCalledWith();
});
});
2020-02-26 06:32:28 +00:00
describe('onRemoteError()', () => {
2020-02-17 12:50:54 +00:00
it('should check the error', () => {
spyOn(controller, 'onRequestEnd');
2020-02-26 06:32:28 +00:00
let error;
try {
2020-02-26 07:24:02 +00:00
const newError = new Error('TestError');
controller.onRemoteError(newError);
2020-02-26 06:32:28 +00:00
} catch (e) {
error = e.message;
}
2020-02-17 12:50:54 +00:00
2020-02-26 07:24:02 +00:00
expect(controller.onRequestEnd).toHaveBeenCalledWith();
expect(error).toEqual('TestError');
2020-02-17 12:50:54 +00:00
});
});
2020-02-06 12:38:46 +00:00
});