React Testing Library
https://testing-library.com/docs
測試 snapshot
import { render } from '@testing-library/react';
test('Match snapshot', () => {
const { asFragment, getByTestId } = render(
<Agreement t={jest.fn()} />
);
expect(asFragment()).toMatchSnapshot();
});測試包含Redux provider 與 store 之 component
import React from 'react';
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
import { cleanup, render } from '@testing-library/react';
import DataPanel from './index';
const mockStore = configureMockStore();
afterEach(cleanup);
test('Match snapshot', () => {
const initialState = {
exportedStudyGroups: {
loaded: '',
},
trainingTasks: {
target: [],
},
};
const store = mockStore(initialState);
const { asFragment } = render(
<Provider store={store}>
<DataPanel />
</Provider>,
);
expect(asFragment()).toMatchSnapshot();
});
測試 useHistory, useRouteMatch
Invariant Violation: You should not use <Route> or withRouter() outside a <Router>
Mock 某個 import 的 module
Uncaught [Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
傳入 i18n
傳入 Material-UI theme
Last updated