Skip to content

Commit

Permalink
fix: do not fail when saving a new item when using dataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
manolo committed Mar 9, 2022
1 parent 8e44e1e commit bc17328
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/crud/src/vaadin-crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
this.items.push(item);
}
} else {
this.editedItem = this.editedItem || {};
Object.assign(this.editedItem, item);
}
this._grid.clearCache();
Expand Down
36 changes: 35 additions & 1 deletion packages/crud/test/crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,41 @@ describe('crud', () => {
});
});

['default', 'slotted buttons'].forEach((mode) => {
describe('dataProvider', () => {
const items = [{ foo: 'bar' }];
beforeEach(async () => {
crud = fixtureSync('<vaadin-crud style="width: 300px;"></vaadin-crud>');
crud.dataProvider = (_, callback) => callback(items, items.length);
await nextRender(crud);
[btnSave, btnCancel, btnDelete] = crud.querySelectorAll('vaadin-button');
});

it('should save a new item', async (done) => {
listenOnce(crud, 'save', (e) => {
expect(e.detail.item.foo).to.be.equal('baz');
done();
});
crud.$.new.click();
crud._form._fields[0].value = 'baz';
change(crud._form);
btnSave.click();
expect(crud.editedItem.foo).to.be.equal('baz');
});

it('should save an edited item', async (done) => {
listenOnce(crud, 'save', (e) => {
expect(e.detail.item.foo).to.be.equal('baz');
done();
});
edit(items[0]);
crud._form._fields[0].value = 'baz';
change(crud._form);
btnSave.click();
expect(crud.editedItem.foo).to.be.equal('baz');
});
});

[('default', 'slotted buttons')].forEach((mode) => {
describe(`[${mode}] items`, () => {
beforeEach(async () => {
if (mode === 'default') {
Expand Down

0 comments on commit bc17328

Please sign in to comment.