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

233 lines
7.0 KiB
JavaScript

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', () => {
spyOn(controller, 'applyChanges');
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();
});
});
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);
});
});
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();
});
});
describe('onRemoteError()', () => {
it('should check the error', () => {
spyOn(controller, 'onRequestEnd');
let error;
try {
controller.onRemoteError('TestError');
} catch (e) {
error = e.message;
}
expect(error).toBe('errasdor');
});
});
});