Last active
February 12, 2025 04:51
-
-
Save night-fury-rider/f5db771933ee029db5672f80e840cf1f to your computer and use it in GitHub Desktop.
jest - Render Test Component using Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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