diff --git a/packages/react-ghostmaker/src/maybeGhost/asGhost.test.ts b/packages/react-ghostmaker/src/maybeGhost/asGhost.test.ts new file mode 100644 index 0000000..c5c8f01 --- /dev/null +++ b/packages/react-ghostmaker/src/maybeGhost/asGhost.test.ts @@ -0,0 +1,59 @@ +import { asGhostProps } from "../maybeGhost"; +import { isReactGhost } from "../types"; +import { expect, test } from "vitest"; + +interface Props { + first: string; + second?: string; + third: string | undefined; +} + +test("makes Ghosts of all props", () => { + const ghostProps = asGhostProps({ + first: "hello", + second: "world", + third: "!", + }); + + expect(ghostProps.firstGhost).toSatisfy(isReactGhost); + expect(ghostProps.secondGhost).toSatisfy(isReactGhost); + expect(ghostProps.thirdGhost).toSatisfy(isReactGhost); +}); + +test("does not make Ghosts of explicit undefined", () => { + const ghostProps = asGhostProps({ + first: "hello", + second: undefined, + third: "!", + }); + + expect(ghostProps.firstGhost).toSatisfy(isReactGhost); + expect(ghostProps.secondGhost).toBeUndefined(); + expect(ghostProps.thirdGhost).toSatisfy(isReactGhost); +}); + +test("does not make Ghosts of not given props", () => { + const ghostProps = asGhostProps({ + first: "hello", + third: "!", + }); + + expect(ghostProps.firstGhost).toSatisfy(isReactGhost); + expect(ghostProps.secondGhost).toBeUndefined(); + expect(ghostProps.thirdGhost).toSatisfy(isReactGhost); +}); + +test("does not make Ghosts of excluded props", () => { + const ghostProps = asGhostProps( + { + first: "hello", + second: "world", + third: "!", + }, + ["first"], + ); + + expect(ghostProps.firstGhost).toSatisfy(isReactGhost); + expect(ghostProps.secondGhost).not.toSatisfy(isReactGhost); + expect(ghostProps.thirdGhost).not.toSatisfy(isReactGhost); +}); diff --git a/packages/react-ghostmaker/src/maybeGhost/asGhost.ts b/packages/react-ghostmaker/src/maybeGhost/asGhost.ts index c99197d..d223ace 100644 --- a/packages/react-ghostmaker/src/maybeGhost/asGhost.ts +++ b/packages/react-ghostmaker/src/maybeGhost/asGhost.ts @@ -20,6 +20,10 @@ export function asGhostProps( const fixupObjectEntry = (entry: [string, unknown]) => { const [key, value] = entry; + if (value === undefined) { + return entry; + } + const shouldFixup = keys === undefined || keys.includes(key as TProps[number]); @@ -27,7 +31,7 @@ export function asGhostProps( return [`${key}Ghost`, asGhost(value)]; } - return [key, value]; + return entry; }; return Object.fromEntries(