Skip to content

Commit 33586a0

Browse files
authored
Fix instanceof implementation for mocked Dates (#616) (#616)
By adding Symbol.hasInstance to MockDate we allow for instanceof to work with mocked Dates. When the global Date object is replaced with a mocked one unmocked Dates are also instances of MockedDate and vice versa.
1 parent f073eba commit 33586a0

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

.changeset/warm-ladybugs-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ladle/react": patch
3+
---
4+
5+
Fix instanceof implementation for mocked Dates

e2e/decorators/src/mock-date.stories.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ export const Inactive: Story = () => {
3131
</h1>
3232
);
3333
};
34+
35+
export const Instanceof: Story = () => {
36+
const mockDate = new Date();
37+
const realDate = new Date(1234);
38+
39+
return (
40+
<dl>
41+
<dt>mockDate instanceof Date:</dt>
42+
<dd data-testid="mockDate">{mockDate instanceof Date ? "yes" : "no"}</dd>
43+
<dt>realDate instanceof Date:</dt>
44+
<dd data-testid="realDate">{realDate instanceof Date ? "yes" : "no"}</dd>
45+
</dl>
46+
);
47+
};
48+
49+
Instanceof.meta = {
50+
mockDate: "1995-12-17T03:24:00",
51+
};

e2e/decorators/tests/mock-date.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ test("the date is current", async ({ page }) => {
2525
}),
2626
);
2727
});
28+
29+
test("MockDate works with instanceof", async ({ page }) => {
30+
await page.goto("/?story=mock-date--instanceof");
31+
32+
await expect(page.locator('[data-testid="mockDate"]')).toHaveText("yes");
33+
await expect(page.locator('[data-testid="realDate"]')).toHaveText("yes");
34+
});

e2e/decorators/tests/params.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ test("meta.json is correctly using defaults and overrides", async ({
6969
name: "Inactive",
7070
namedExport: "Inactive",
7171
},
72+
"mock-date--instanceof": {
73+
filePath: "src/mock-date.stories.tsx",
74+
levels: ["Mock date"],
75+
locEnd: 47,
76+
locStart: 35,
77+
meta: {
78+
mockDate: "1995-12-17T03:24:00",
79+
},
80+
name: "Instanceof",
81+
namedExport: "Instanceof",
82+
},
7283
"root--examples--first": {
7384
filePath: "src/params.stories.tsx",
7485
levels: ["Root", "Examples"],

packages/ladle/lib/app/src/mock-date.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ const MockDate = class Date extends RealDate {
5656

5757
return date;
5858
}
59+
60+
static [Symbol.hasInstance](instance: unknown): boolean {
61+
return instance instanceof RealDate;
62+
}
5963
};
6064

6165
MockDate.UTC = RealDate.UTC;

0 commit comments

Comments
 (0)