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

51 lines
1.6 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();
});
});
});