Skip to content

Commit

Permalink
fix(store): clear or remove records only for null value
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed Jul 12, 2024
1 parent 0e92b27 commit a3e0a0b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,17 +722,22 @@ function setupModel(Model, nested) {
const localConfig = bootstrap(defaultValue, true);

return (model, data, lastModel) => {
const record = data[key];
const result =
lastModel && lastModel[key] ? { ...lastModel[key] } : {};

if (record === null || record === undefined) {
if (data[key] === null) {
model[key] = {};
return;
}

const record = data[key] || {};
let result = {};

if (lastModel) {
for (const id of Object.keys(lastModel[key])) {
result[id] = lastModel[key][id];
}
}

for (const id of Object.keys(record)) {
if (record[id] === null || record[id] === undefined) {
if (record[id] === null) {
delete result[id];
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions test/spec/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,7 @@ describe("store:", () => {
beforeEach(() => {
Model = {
id: true,
other: true,
values: store.record(""),
};
});
Expand Down Expand Up @@ -1213,6 +1214,15 @@ describe("store:", () => {
expect(model.values).toEqual({});
});
});

it("keeps the values if another property is updated", () => {
return store
.set(Model, { values: { a: "b", c: "d" } })
.then((model) => store.set(model, { other: "test" }))
.then((model) => {
expect(model.values).toEqual({ a: "b", c: "d" });
});
});
});

describe("for nested model value", () => {
Expand Down

0 comments on commit a3e0a0b

Please sign in to comment.