added crud-model save() unit test
gitea/salix/2076-item_tags_refactor This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-02-06 13:38:46 +01:00
parent 462cf46753
commit c7c79831d5
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,50 @@
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();
});
});
});

View File

@ -68,12 +68,14 @@ module.exports = function(Self) {
let promises = [];
for (let id of deletes)
promises.push(this.destroyById(id, options));
await Promise.all(promises);
}
if (updates) {
let promises = [];
for (let update of updates)
promises.push(this.upsertWithWhere(update.where, update.data, options));
await Promise.all(promises);
}
let created;