Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/react-ghostmaker/src/maybeGhost/asGhost.test.ts
Original file line number Diff line number Diff line change
@@ -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<Props>({
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<Props>({
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<Props>({
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<Props>(
{
first: "hello",
second: "world",
third: "!",
},
["first"],
);

expect(ghostProps.firstGhost).toSatisfy(isReactGhost);
expect(ghostProps.secondGhost).not.toSatisfy(isReactGhost);
expect(ghostProps.thirdGhost).not.toSatisfy(isReactGhost);
});
6 changes: 5 additions & 1 deletion packages/react-ghostmaker/src/maybeGhost/asGhost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ export function asGhostProps<T, TProps extends (keyof T)[] = (keyof T)[]>(
const fixupObjectEntry = (entry: [string, unknown]) => {
const [key, value] = entry;

if (value === undefined) {
return entry;
}

const shouldFixup =
keys === undefined || keys.includes(key as TProps[number]);

if (shouldFixup) {
return [`${key}Ghost`, asGhost(value)];
}

return [key, value];
return entry;
};

return Object.fromEntries(
Expand Down