-
Notifications
You must be signed in to change notification settings - Fork 3
/
useTimeZone.spec.tsx
75 lines (68 loc) · 2.43 KB
/
useTimeZone.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { act, renderHook } from "@testing-library/react";
import { identity } from "lodash";
import * as React from "react";
import useTimeZone, { TimeZoneProvider } from "./useTimeZone";
describe("useTimeZone", () => {
let onRender: jest.Mock<string, [string]>;
let resolvedOptions: jest.SpiedFunction<
Intl.DateTimeFormat["resolvedOptions"]
>;
beforeEach(() => {
window.ontimezonechange = null;
onRender = jest.fn().mockImplementation(identity);
resolvedOptions = jest.spyOn(
Intl.DateTimeFormat.prototype,
"resolvedOptions",
);
resolvedOptions.mockReturnValue({
timeZone: "Australia/Sydney",
} as Intl.ResolvedDateTimeFormatOptions);
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should return Australia/Sydney", () => {
const { result } = renderHook(() => useTimeZone());
expect(result.current).toBe("Australia/Sydney");
});
it("should return Etc/UTC during hydration", () => {
renderHook(() => onRender(useTimeZone()), { hydrate: true });
expect(onRender).toHaveBeenNthCalledWith(1, "Etc/UTC");
expect(onRender).toHaveBeenLastCalledWith("Australia/Sydney");
});
it("should return ssrTimeZone from context during hydration", () => {
renderHook(() => onRender(useTimeZone()), {
hydrate: true,
wrapper({ children }) {
return (
<TimeZoneProvider ssrTimeZone="Australia/Brisbane">
{children}
</TimeZoneProvider>
);
},
});
expect(onRender).toHaveBeenNthCalledWith(1, "Australia/Brisbane");
expect(onRender).toHaveBeenLastCalledWith("Australia/Sydney");
});
it("should response to timezonechange events", () => {
const { result } = renderHook(() => useTimeZone());
resolvedOptions.mockReturnValue({
timeZone: "Australia/Brisbane",
} as Intl.ResolvedDateTimeFormatOptions);
act(() => {
window.dispatchEvent(new Event("timezonechange"));
});
expect(result.current).toBe("Australia/Brisbane");
});
it("should response to visibilitychange events when ontimezonechange is not supported", () => {
delete window.ontimezonechange;
const { result } = renderHook(() => useTimeZone());
resolvedOptions.mockReturnValue({
timeZone: "Australia/Brisbane",
} as Intl.ResolvedDateTimeFormatOptions);
act(() => {
document.dispatchEvent(new Event("visibilitychange"));
});
expect(result.current).toBe("Australia/Brisbane");
});
});