Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save night-fury-rider/f5db771933ee029db5672f80e840cf1f to your computer and use it in GitHub Desktop.
Save night-fury-rider/f5db771933ee029db5672f80e840cf1f to your computer and use it in GitHub Desktop.
jest - Render Test Component using Method
import { Provider } from "react-redux";
import thunk, { ThunkMiddleware } from "redux-thunk";
import { render, RenderAPI } from "react-test-renderer";
import configureMockStore from "redux-mock-store";
import TestComponent from "./TestComponent";
import { describe } from "node:test";
const middlewares: Array<ThunkMiddleware> = [thunk];
const createMockStore = configureMockStore(middlewares);
const navigationParams = {
fromDashboard: true,
};
const mockProps: any = {
navigation: {
navigate: jest.fn(),
},
route: {
params: navigationParams,
},
};
const testReduxStore = {
Config: {
DUAL_ORDER_ENABLED: true,
},
};
const testStore = createMockStore(testReduxStore);
const renderTestComponent = (
props = mockProps,
mockedStore = testStore
): RenderAPI => {
const navigationContextTestValue: any = {
addListener: jest.fn(() => jest.fn()),
};
return render(
<Provider store={mockedStore}>
<NavigationContext.Provider value={navigationContextTestValue}>
<TestComponent {...props} />
</NavigationContext.Provider>
</Provider>
);
};
describe("Testing Test Component", () => {
it("should render as expected", () => {
const component = renderTestComponent(mockProps, testStore);
expect(component).toBeDefined();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment