mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 03:23:02 +00:00
test: working test with TypeScript + vite (using vitest)
This commit is contained in:
@@ -1 +1,16 @@
|
||||
import '@testing-library/jest-dom';
|
||||
|
||||
// FIXME: Use something like jsdom-worker?
|
||||
// see: https://github.com/developit/jsdom-worker
|
||||
if (!global.Worker) {
|
||||
(global as any).Worker = class MockWorker {
|
||||
events: Record<string, (e: unknown) => void> = Object.create(null);
|
||||
|
||||
onmessage?: () => {};
|
||||
addEventListener(name: string, e: unknown) {
|
||||
if (Object.hasOwn(this.events, name)) {
|
||||
this.events[name](e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
25
src/test-utils/test-helper.tsx
Normal file
25
src/test-utils/test-helper.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { PreloadedState } from '@reduxjs/toolkit';
|
||||
import { RenderOptions, render } from '@testing-library/react';
|
||||
import { PropsWithChildren } from 'react';
|
||||
import { Provider } from 'react-redux';
|
||||
import { AppStore, RootState, setupStore } from '~/store';
|
||||
|
||||
// Adapted from: https://redux.js.org/usage/writing-tests
|
||||
|
||||
export * from '@testing-library/react';
|
||||
|
||||
export interface ExtendedRenderOptions extends RenderOptions {
|
||||
preloadedState?: PreloadedState<RootState>;
|
||||
store?: AppStore;
|
||||
}
|
||||
|
||||
export function renderWithProviders(
|
||||
ui: React.ReactElement,
|
||||
{ preloadedState = {}, store = setupStore(preloadedState), ...renderOptions }: ExtendedRenderOptions = {}
|
||||
) {
|
||||
function Wrapper({ children }: PropsWithChildren<{}>): JSX.Element {
|
||||
return <Provider store={store}>{children}</Provider>;
|
||||
}
|
||||
|
||||
return { store, ...render(ui, { wrapper: Wrapper, ...renderOptions }) };
|
||||
}
|
||||
Reference in New Issue
Block a user