2023-09-06 18:59:26 +00:00
|
|
|
import "@testing-library/jest-dom";
|
|
|
|
import "jest-location-mock";
|
2024-03-05 23:31:22 +00:00
|
|
|
import { cleanup } from "@testing-library/react";
|
|
|
|
import crypto from "crypto";
|
2023-09-06 18:59:26 +00:00
|
|
|
import { useMemo } from "react";
|
2024-03-05 23:31:22 +00:00
|
|
|
import type { Region } from "api/typesGenerated";
|
|
|
|
import type { ProxyLatencyReport } from "contexts/useProxyLatency";
|
|
|
|
import { server } from "testHelpers/server";
|
2023-03-13 16:35:09 +00:00
|
|
|
|
2023-05-11 20:42:30 +00:00
|
|
|
// useProxyLatency does some http requests to determine latency.
|
|
|
|
// This would fail unit testing, or at least make it very slow with
|
|
|
|
// actual network requests. So just globally mock this hook.
|
|
|
|
jest.mock("contexts/useProxyLatency", () => ({
|
2023-06-30 15:32:35 +00:00
|
|
|
useProxyLatency: (proxies?: Region[]) => {
|
2023-05-22 14:56:41 +00:00
|
|
|
// Must use `useMemo` here to avoid infinite loop.
|
|
|
|
// Mocking the hook with a hook.
|
2023-06-01 13:37:20 +00:00
|
|
|
const proxyLatencies = useMemo(() => {
|
2023-05-22 14:56:41 +00:00
|
|
|
if (!proxies) {
|
2023-09-06 18:59:26 +00:00
|
|
|
return {} as Record<string, ProxyLatencyReport>;
|
2023-05-11 20:42:30 +00:00
|
|
|
}
|
2023-07-13 11:44:12 +00:00
|
|
|
return proxies.reduce(
|
|
|
|
(acc, proxy) => {
|
|
|
|
acc[proxy.id] = {
|
|
|
|
accurate: true,
|
|
|
|
// Return a constant latency of 8ms.
|
|
|
|
// If you make this random it could break stories.
|
|
|
|
latencyMS: 8,
|
|
|
|
at: new Date(),
|
2023-09-06 18:59:26 +00:00
|
|
|
};
|
|
|
|
return acc;
|
2023-07-13 11:44:12 +00:00
|
|
|
},
|
|
|
|
{} as Record<string, ProxyLatencyReport>,
|
2023-09-06 18:59:26 +00:00
|
|
|
);
|
|
|
|
}, [proxies]);
|
2023-05-22 14:56:41 +00:00
|
|
|
|
2023-09-06 18:59:26 +00:00
|
|
|
return { proxyLatencies, refetch: jest.fn() };
|
2023-05-11 20:42:30 +00:00
|
|
|
},
|
2023-09-06 18:59:26 +00:00
|
|
|
}));
|
2023-05-11 20:42:30 +00:00
|
|
|
|
2023-10-09 17:10:48 +00:00
|
|
|
global.scrollTo = jest.fn();
|
2022-03-18 18:07:08 +00:00
|
|
|
|
2024-02-27 12:24:07 +00:00
|
|
|
window.HTMLElement.prototype.scrollIntoView = jest.fn();
|
2024-03-19 22:42:40 +00:00
|
|
|
window.open = jest.fn();
|
2024-02-23 19:23:52 +00:00
|
|
|
|
2022-05-06 18:23:03 +00:00
|
|
|
// Polyfill the getRandomValues that is used on utils/random.ts
|
|
|
|
Object.defineProperty(global.self, "crypto", {
|
|
|
|
value: {
|
|
|
|
getRandomValues: function (buffer: Buffer) {
|
2023-09-06 18:59:26 +00:00
|
|
|
return crypto.randomFillSync(buffer);
|
2022-05-06 18:23:03 +00:00
|
|
|
},
|
|
|
|
},
|
2023-09-06 18:59:26 +00:00
|
|
|
});
|
2022-05-06 18:23:03 +00:00
|
|
|
|
2022-03-18 18:07:08 +00:00
|
|
|
// Establish API mocking before all tests through MSW.
|
|
|
|
beforeAll(() =>
|
|
|
|
server.listen({
|
|
|
|
onUnhandledRequest: "warn",
|
|
|
|
}),
|
2023-09-06 18:59:26 +00:00
|
|
|
);
|
2022-03-18 18:07:08 +00:00
|
|
|
|
|
|
|
// Reset any request handlers that we may add during the tests,
|
|
|
|
// so they don't affect other tests.
|
2022-04-15 17:17:50 +00:00
|
|
|
afterEach(() => {
|
2023-09-06 18:59:26 +00:00
|
|
|
cleanup();
|
|
|
|
server.resetHandlers();
|
2024-04-01 16:14:36 +00:00
|
|
|
jest.resetAllMocks();
|
2023-09-06 18:59:26 +00:00
|
|
|
});
|
2022-03-18 18:07:08 +00:00
|
|
|
|
|
|
|
// Clean up after the tests are finished.
|
2023-09-06 18:59:26 +00:00
|
|
|
afterAll(() => server.close());
|
2022-03-18 18:07:08 +00:00
|
|
|
|
2022-03-17 03:17:41 +00:00
|
|
|
// This is needed because we are compiling under `--isolatedModules`
|
2023-09-06 18:59:26 +00:00
|
|
|
export {};
|