Skip to content

Commit 4427d1e

Browse files
authored
fix(store): custom id for creating enumerable model (#298)
1 parent 8dc12b5 commit 4427d1e

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ function set(model, values = {}) {
13111311
if (!isDraft && values && hasOwnProperty.call(values, "id")) {
13121312
if (!config.enumerable) {
13131313
throw TypeError(`Values must not contain 'id' property: ${values.id}`);
1314-
} else if (!isInstance || values.id !== model.id) {
1314+
} else if (isInstance && values.id !== model.id) {
13151315
throw TypeError(
13161316
`You cannot change the 'id' property of the model instance: ${values.id}`,
13171317
);

test/spec/store.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,6 @@ describe("store:", () => {
383383
expect(() => store.set(Model, { id: "test" })).toThrow();
384384
});
385385

386-
it("throws an error when values contain 'id' property for enumerable model", async () => {
387-
expect(() => store.set(Model, { id: "test" })).toThrow();
388-
});
389-
390386
it("throws an error when updating an instance and values contain different 'id' property", async () => {
391387
const model = await promise;
392388
expect(() => store.set(model, { ...model, id: "different" })).toThrow();
@@ -500,6 +496,14 @@ describe("store:", () => {
500496
expect(model.nestedArrayOfObjects[0].id).not.toBeDefined();
501497
}));
502498

499+
it('uses provided id for objects with "id" key', () =>
500+
store
501+
.set(Model, { id: 123, nestedArrayOfExternalObjects: [{ id: 456 }] })
502+
.then((model) => {
503+
expect(model.id).toBe("123");
504+
expect(model.nestedArrayOfExternalObjects[0].id).toBe("456");
505+
}));
506+
503507
it("updates single property", () =>
504508
promise.then((model) =>
505509
store.set(model, { string: "new value" }).then((newModel) => {

0 commit comments

Comments
 (0)