Skip to content

Commit 17d867c

Browse files
authored
Merge pull request #22 from mittwald/fix-undefined-handling
fix: fix undefined should not be converted to Ghost
2 parents 811a657 + b9ab828 commit 17d867c

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { asGhostProps } from "../maybeGhost";
2+
import { isReactGhost } from "../types";
3+
import { expect, test } from "vitest";
4+
5+
interface Props {
6+
first: string;
7+
second?: string;
8+
third: string | undefined;
9+
}
10+
11+
test("makes Ghosts of all props", () => {
12+
const ghostProps = asGhostProps<Props>({
13+
first: "hello",
14+
second: "world",
15+
third: "!",
16+
});
17+
18+
expect(ghostProps.firstGhost).toSatisfy(isReactGhost);
19+
expect(ghostProps.secondGhost).toSatisfy(isReactGhost);
20+
expect(ghostProps.thirdGhost).toSatisfy(isReactGhost);
21+
});
22+
23+
test("does not make Ghosts of explicit undefined", () => {
24+
const ghostProps = asGhostProps<Props>({
25+
first: "hello",
26+
second: undefined,
27+
third: "!",
28+
});
29+
30+
expect(ghostProps.firstGhost).toSatisfy(isReactGhost);
31+
expect(ghostProps.secondGhost).toBeUndefined();
32+
expect(ghostProps.thirdGhost).toSatisfy(isReactGhost);
33+
});
34+
35+
test("does not make Ghosts of not given props", () => {
36+
const ghostProps = asGhostProps<Props>({
37+
first: "hello",
38+
third: "!",
39+
});
40+
41+
expect(ghostProps.firstGhost).toSatisfy(isReactGhost);
42+
expect(ghostProps.secondGhost).toBeUndefined();
43+
expect(ghostProps.thirdGhost).toSatisfy(isReactGhost);
44+
});
45+
46+
test("does not make Ghosts of excluded props", () => {
47+
const ghostProps = asGhostProps<Props>(
48+
{
49+
first: "hello",
50+
second: "world",
51+
third: "!",
52+
},
53+
["first"],
54+
);
55+
56+
expect(ghostProps.firstGhost).toSatisfy(isReactGhost);
57+
expect(ghostProps.secondGhost).not.toSatisfy(isReactGhost);
58+
expect(ghostProps.thirdGhost).not.toSatisfy(isReactGhost);
59+
});

packages/react-ghostmaker/src/maybeGhost/asGhost.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ export function asGhostProps<T, TProps extends (keyof T)[] = (keyof T)[]>(
2020
const fixupObjectEntry = (entry: [string, unknown]) => {
2121
const [key, value] = entry;
2222

23+
if (value === undefined) {
24+
return entry;
25+
}
26+
2327
const shouldFixup =
2428
keys === undefined || keys.includes(key as TProps[number]);
2529

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

30-
return [key, value];
34+
return entry;
3135
};
3236

3337
return Object.fromEntries(

0 commit comments

Comments
 (0)