Skip to content

Commit

Permalink
Fix getEntitiesValue returning an empty string if the value is false
Browse files Browse the repository at this point in the history
  • Loading branch information
acurrieclark committed Feb 5, 2024
1 parent 36bc47b commit 212ebd8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/actions/bind-entity-int-deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function bindEntityIntDeferred<
setByPath(
doc.entities[id],
path,
parseInt(value || "0") as PathValue<T, typeof path>,
parseInt(value ?? "0") as PathValue<T, typeof path>,
);
});
return doc;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
Expand Down
2 changes: 1 addition & 1 deletion tests/actions/entity-deferred.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
Expand Down
15 changes: 15 additions & 0 deletions tests/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
},
},
},
Expand Down
49 changes: 49 additions & 0 deletions tests/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -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<Person>;

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,
);
});
});
});

0 comments on commit 212ebd8

Please sign in to comment.