Files
element-call/src/vitest.setup.ts
T

104 lines
2.5 KiB
TypeScript
Raw Normal View History

/*
Copyright 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
2024-09-05 16:23:44 -04:00
import "global-jsdom/register";
2026-01-05 13:55:58 +01:00
import "@formatjs/intl-durationformat/polyfill.js";
import "@formatjs/intl-segmenter/polyfill";
import i18n from "i18next";
import posthog from "posthog-js";
import { initReactI18next } from "react-i18next";
2024-09-05 16:23:44 -04:00
import { afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
2024-09-05 16:23:44 -04:00
import "vitest-axe/extend-expect";
2025-03-13 13:58:43 +01:00
import { logger } from "matrix-js-sdk/lib/logger";
import "@testing-library/jest-dom/vitest";
2024-12-04 14:51:29 +00:00
import EN from "../locales/en/app.json";
import { Config } from "./config/Config";
// Bare-minimum i18n config
i18n
.use(initReactI18next)
.init({
2024-12-04 14:51:29 +00:00
lng: "en",
fallbackLng: "en",
supportedLngs: ["en"],
// We embed the translations, so that it never needs to fetch
resources: {
2024-12-04 14:51:29 +00:00
en: {
2025-03-03 14:37:29 +01:00
translation: EN,
},
},
interpolation: {
escapeValue: false, // React has built-in XSS protections
},
})
.catch((e) => logger.warn("Failed to init i18n for testing", e));
Config.initDefault();
posthog.opt_out_capturing();
2024-09-05 16:23:44 -04:00
afterEach(cleanup);
2024-09-06 13:15:34 -04:00
// Used by a lot of components
window.matchMedia = global.matchMedia = (): MediaQueryList =>
({
matches: false,
addEventListener: () => {},
removeEventListener: () => {},
}) as Partial<MediaQueryList> as MediaQueryList;
2026-04-17 18:21:48 +02:00
const storage: Record<string, string> = {};
const localStoragePolyfill = {
getItem(key: string) {
return Object.prototype.hasOwnProperty.call(storage, key)
? storage[key]
: null;
},
setItem(key: string, value: string) {
storage[key] = String(value);
},
removeItem(key: string) {
delete storage[key];
},
clear() {
for (const key in storage) {
delete storage[key];
}
},
key(index: number) {
const keys = Object.keys(storage);
return keys[index] ?? null;
},
get length() {
return Object.keys(storage).length;
},
} as unknown as Storage;
if (
typeof globalThis.localStorage === "undefined" ||
typeof globalThis.localStorage.clear !== "function"
) {
Object.defineProperty(globalThis, "localStorage", {
value: localStoragePolyfill,
writable: true,
configurable: true,
});
}
if (
typeof window !== "undefined" &&
(typeof window.localStorage === "undefined" ||
typeof window.localStorage.clear !== "function")
) {
Object.defineProperty(window, "localStorage", {
value: localStoragePolyfill,
writable: true,
configurable: true,
});
2026-04-17 18:28:02 +02:00
}