Skip to content

Commit 7c76f82

Browse files
committed
fix(store): store factory function id should be always a string value
1 parent daac11f commit 7c76f82

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/store.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,10 @@ function store(Model, options = {}) {
15191519
}
15201520

15211521
const resolveId = options.id
1522-
? options.id
1522+
? (host) => {
1523+
const id = options.id(host);
1524+
return id !== undefined && id !== null ? String(id) : undefined;
1525+
}
15231526
: (host, value) => {
15241527
if (value !== null && value !== undefined) {
15251528
return typeof value === "object" ? value.id : String(value);

test/spec/store.js

+8
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,9 @@ describe("store:", () => {
12151215
byundefined: store(Model, { id: () => undefined }),
12161216
byprop: store(Model, { id: "modelId" }),
12171217
byfn: store(Model, { id: ({ modelId }) => modelId }),
1218+
byfnwithnumber: store(Model, {
1219+
id: ({ modelId }) => Number(modelId),
1220+
}),
12181221
withoutid: store(Model),
12191222
draft: store(Model, { draft: true, id: "modelId" }),
12201223
draftwithoutid: store(Model, { draft: true }),
@@ -1256,6 +1259,11 @@ describe("store:", () => {
12561259
});
12571260
});
12581261

1262+
fit("gets a model instance when id is a number zero", () => {
1263+
el.modelId = "0";
1264+
expect(el.byfnwithnumber).toBe(store.get(Model, "0"));
1265+
});
1266+
12591267
describe("in draft mode", () => {
12601268
it("throws when try to set value by assertion", () => {
12611269
expect(() => {

0 commit comments

Comments
 (0)