Skip to content

Commit

Permalink
fix(store): Memory-based storage returns null for non-existing inst…
Browse files Browse the repository at this point in the history
…ance
  • Loading branch information
smalluban committed Apr 19, 2024
1 parent dbff629 commit d224450
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function setupStorage(config, options) {

function memoryStorage(config) {
return {
get: config.enumerable ? () => {} : () => config.create({}),
get: config.enumerable ? () => null : () => config.create({}),
set: config.enumerable
? (id, values) => values
: (id, values) => (values === null ? { id } : values),
Expand Down Expand Up @@ -1042,7 +1042,10 @@ function get(Model, id) {
(result === undefined || typeof result !== "object")
) {
throw TypeError(
`Storage 'get' method must return a Promise, an instance, or null: ${result}`,
stringifyModel(
Model,
`Storage 'get' method must return a Promise, an instance, or null: ${result}`,
),
);
}

Expand Down
9 changes: 9 additions & 0 deletions test/spec/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ describe("store:", () => {
expect(spy).toHaveBeenCalledTimes(1);
});

it("returns a model in the error state for non-existing instance of memory based enumerable definition", () => {
Model = { id: true, value: "" };
const model = store.get(Model, "1");

expect(model).toBeInstanceOf(Object);
expect(store.error(model)).toBeInstanceOf(Error);
expect(store.error(model).message.includes("does not exist")).toBe(true);
});

describe("for singleton", () => {
beforeEach(() => {
Model = {
Expand Down

0 comments on commit d224450

Please sign in to comment.