pleroma/app/soapbox/jest/test-setup.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-01-05 15:45:48 -08:00
import { act } from '@testing-library/react';
2022-12-20 10:14:38 -08:00
import { toast } from 'react-hot-toast';
// eslint-disable-next-line import/no-extraneous-dependencies
import '@testing-library/jest-dom/vitest';
2022-12-20 10:14:38 -08:00
2022-11-15 05:43:26 -08:00
import { __clear as clearApiMocks } from '../api/__mocks__';
// API mocking
vi.mock('soapbox/api');
afterEach(() => {
clearApiMocks();
});
2022-10-28 10:01:39 -07:00
// Query mocking
vi.mock('soapbox/queries/client');
2022-10-28 10:01:39 -07:00
2022-04-21 15:03:29 -07:00
// Mock IndexedDB
// https://dev.to/andyhaskell/testing-your-indexeddb-code-with-jest-2o17
require('fake-indexeddb/auto');
2022-12-20 10:14:38 -08:00
// Clear toasts after each test.
afterEach(() => {
2023-01-05 15:45:48 -08:00
act(() => {
toast.remove();
});
2022-12-20 10:14:38 -08:00
});
2022-04-06 07:10:21 -07:00
const intersectionObserverMock = () => ({ observe: () => null, disconnect: () => null });
window.IntersectionObserver = vi.fn().mockImplementation(intersectionObserverMock);
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // Deprecated
removeListener: vi.fn(), // Deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});