Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as React from "react"; | ||
import { useSyncExternalStore } from "use-sync-external-store/shim"; | ||
|
||
declare global { | ||
interface WindowEventHandlersEventMap { | ||
timezonechange: Event; | ||
} | ||
|
||
interface WindowEventHandlers { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ontimezonechange?: ((this: Window, ev: Event) => any) | null; | ||
} | ||
} | ||
|
||
function subscribe(callback: () => void): () => void { | ||
// @see https://github.com/whatwg/html/pull/3047 | ||
if ("ontimezonechange" in window) { | ||
window.addEventListener("timezonechange", callback); | ||
return () => { | ||
window.removeEventListener("timezonechange", callback); | ||
}; | ||
} | ||
|
||
// If the "timezonechange" event is not supported, use "visibilitychange" as a proxy | ||
document.addEventListener("visibilitychange", callback); | ||
return () => document.removeEventListener("visibilitychange", callback); | ||
} | ||
|
||
function getSnapshot() { | ||
return new Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
} | ||
|
||
const SSRTimeZoneContext = React.createContext("Etc/UTC"); | ||
|
||
/** | ||
* Custom hook which returns the current `timeZone`. | ||
* | ||
* Defaults to `"Etc/UTC"` during SSR and hydration, but this may be overriden with a provider | ||
* `<TimeZoneProvider ssrTimeZone={timeZone}>`. Ensure that the value used during SSR and hydration is the same. | ||
* @returns IANA tz database identifier | ||
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones | ||
*/ | ||
export default function useTimeZone() { | ||
const ssrTimeZone = React.useContext(SSRTimeZoneContext); | ||
const getServerSnapshot = React.useCallback(() => ssrTimeZone, [ssrTimeZone]); | ||
|
||
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); | ||
} | ||
|
||
export interface TimeZoneProviderProps { | ||
children?: React.ReactNode; | ||
/** IANA tz database identifier */ | ||
ssrTimeZone: string; | ||
} | ||
|
||
/** | ||
* Context Provider for the `useTimeZone` hook. May be used to override the default `"Etc/UTC"` `timeZone` value during | ||
* SSR and hydration. | ||
* @param props.children | ||
* @param props.ssrTimeZone IANA tz database identifier | ||
*/ | ||
export const TimeZoneProvider = ({ | ||
children, | ||
ssrTimeZone, | ||
}: TimeZoneProviderProps) => ( | ||
<SSRTimeZoneContext.Provider value={ssrTimeZone}> | ||
{children} | ||
</SSRTimeZoneContext.Provider> | ||
); |