From 212ebd8a2256d09866f1f8634e62e5c56fddfc5a Mon Sep 17 00:00:00 2001 From: Alex Currie-Clark Date: Mon, 5 Feb 2024 20:58:32 +0000 Subject: [PATCH] Fix `getEntitiesValue` returning an empty string if the value is false --- src/actions/bind-entity-int-deferred.ts | 2 +- src/actions/utilities.ts | 2 +- tests/actions/entity-deferred.test.ts | 2 +- tests/data.ts | 15 ++++++++ tests/utilities.test.ts | 49 +++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/utilities.test.ts diff --git a/src/actions/bind-entity-int-deferred.ts b/src/actions/bind-entity-int-deferred.ts index 73d34a7..890bb67 100644 --- a/src/actions/bind-entity-int-deferred.ts +++ b/src/actions/bind-entity-int-deferred.ts @@ -29,7 +29,7 @@ export function bindEntityIntDeferred< setByPath( doc.entities[id], path, - parseInt(value || "0") as PathValue, + parseInt(value ?? "0") as PathValue, ); }); return doc; diff --git a/src/actions/utilities.ts b/src/actions/utilities.ts index d96185f..b005ceb 100644 --- a/src/actions/utilities.ts +++ b/src/actions/utilities.ts @@ -13,7 +13,7 @@ export const getEntitiesValue = < const values = new Set(ids.map((id) => getByPath(doc.entities[id], path))); if (values.size === 1) { - return (getByPath(doc.entities[ids[0]], path) as string) || ""; + return (getByPath(doc.entities[ids[0]], path) as string) ?? ""; } else { return ""; } diff --git a/tests/actions/entity-deferred.test.ts b/tests/actions/entity-deferred.test.ts index be119fb..ece1bea 100644 --- a/tests/actions/entity-deferred.test.ts +++ b/tests/actions/entity-deferred.test.ts @@ -220,7 +220,7 @@ Object.entries({ expect(input.value).toBe(toType); input.reset(); - expect(input.value).toBe(""); + expect(input.value).toBe("") expect(get(store).people.entities["1"][field]).toBe( document.people.entities["1"][field], ); diff --git a/tests/data.ts b/tests/data.ts index c5d7d1a..f1f76fe 100644 --- a/tests/data.ts +++ b/tests/data.ts @@ -19,6 +19,11 @@ export type DocumentType = { export type Person = { id: string; name: string; + surname: string; + age: number; + children: number; + married: boolean; + alive: boolean; }; export const documentData: DocumentType = { @@ -37,10 +42,20 @@ export const documentData: DocumentType = { "id-1": { id: "id-1", name: "Alex", + surname: "Smith", + age: 30, + children: 0, + married: true, + alive: true, }, "id-2": { id: "id-2", name: "John", + surname: "Smith", + age: 40, + children: 0, + married: false, + alive: true, }, }, }, diff --git a/tests/utilities.test.ts b/tests/utilities.test.ts new file mode 100644 index 0000000..cfe9357 --- /dev/null +++ b/tests/utilities.test.ts @@ -0,0 +1,49 @@ +import { EntityState } from "@onsetsoftware/entity-state"; +import { beforeEach, describe, expect, test } from "vitest"; +import { Person, documentData } from "./data"; + +import { quickClone } from "../src/helpers/quick-clone"; +import { getEntitiesValue } from "../src/actions/utilities"; + +describe("Get entities value", () => { + let people: EntityState; + + beforeEach(() => { + people = quickClone(documentData.people); + }); + + const testCases = [ + { + path: "name", + expected: "", + }, + { + path: "surname", + expected: "Smith", + }, + { + path: "age", + expected: "", + }, + { + path: "children", + expected: 0, + }, + { + path: "married", + expected: "", + }, + { + path: "alive", + expected: true, + }, + ] as const; + + testCases.forEach((testCase) => { + test("it should return the correct " + testCase.path, () => { + expect(getEntitiesValue(people, ["id-1", "id-2"], testCase.path)).toBe( + testCase.expected, + ); + }); + }); +});