Skip to content

Commit 0d3f538

Browse files
committed
minor changes to testing to import store
1 parent 8cb05d4 commit 0d3f538

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/app/__tests__/ActionContainer.test.tsx

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import React from 'react';
44
import { render, screen, fireEvent } from '@testing-library/react';
55
import '@testing-library/jest-dom/extend-expect';
66
import ActionContainer from '../containers/ActionContainer';
7-
import { useStoreContext } from '../store';
7+
// import { useStoreContext } from '../store';
88
import TravelContainer from '../containers/TravelContainer';
9+
import { useDispatch } from 'react-redux';
10+
import {store} from '../'
911

1012
const state = {
1113
tabs: {
@@ -162,4 +164,82 @@ describe('integration testing for ActionContainer', () => {
162164
fireEvent.click(screen.getAllByRole('button')[0]);
163165
expect(screen.getByRole('slider')).toHaveStyle('left: 0');
164166
});
165-
});
167+
});
168+
169+
170+
// To convert your existing test file to use Redux Toolkit, you need to update your test setup to work with Redux Toolkit's `configureStore` and create a Redux store. Assuming you already have a Redux store and slice set up, here's how you can modify your test file:
171+
172+
// 1. Import `configureStore` from Redux Toolkit and the Redux store you want to use in your tests.
173+
174+
// ```javascript
175+
// import { render, screen, fireEvent } from '@testing-library/react';
176+
// import { Provider } from 'react-redux'; // Import Provider from react-redux
177+
// import { configureStore } from '@reduxjs/toolkit'; // Import configureStore from Redux Toolkit
178+
// import ActionContainer from '../containers/ActionContainer';
179+
// import TravelContainer from '../containers/TravelContainer';
180+
181+
// // Import your Redux store and slice here if not already done
182+
// import { rootReducer } from '../store'; // Replace with your actual reducer and store
183+
// ```
184+
185+
// 2. Create a Redux store with `configureStore` and pass it as a prop to your components.
186+
187+
// ```javascript
188+
// const store = configureStore({
189+
// reducer: rootReducer, // Replace with your actual reducer
190+
// });
191+
192+
// describe('unit testing for ActionContainer', () => {
193+
// beforeEach(() => {
194+
// render(
195+
// <Provider store={store}>
196+
// <ActionContainer actionView={true} />
197+
// </Provider>
198+
// );
199+
// });
200+
201+
// // Your tests here
202+
// });
203+
204+
// describe('integration testing for ActionContainer', () => {
205+
// beforeEach(() => {
206+
// render(
207+
// <Provider store={store}>
208+
// <ActionContainer actionView={true} />
209+
// <TravelContainer snapshotsLength={0} />
210+
// </Provider>
211+
// );
212+
// });
213+
214+
// // Your tests here
215+
// });
216+
// ```
217+
218+
// 3. Ensure that you import and use `useDispatch` from `react-redux` for your component testing as follows:
219+
220+
// ```javascript
221+
// import { useDispatch, useSelector } from 'react-redux'; // Import useDispatch and useSelector from react-redux
222+
// ```
223+
224+
// And in your component code where you use `dispatch`, use `useDispatch`:
225+
226+
// ```javascript
227+
// const dispatch = useDispatch();
228+
// ```
229+
230+
// 4. Update your tests accordingly to work with Redux Toolkit's `configureStore`. For example, if you have a test that checks if `dispatch` is called, you can do something like this:
231+
232+
// ```javascript
233+
// test('Click works on clear button', () => {
234+
// const { getByRole } = render(
235+
// <Provider store={store}>
236+
// <ActionContainer actionView={true} />
237+
// </Provider>
238+
// );
239+
240+
// fireEvent.click(getByRole('button'));
241+
// expect(dispatch).toHaveBeenCalledTimes(1);
242+
// });
243+
// ```
244+
245+
// With these modifications, your test file should work with Redux Toolkit and your existing Redux store and slice. Make sure to replace `rootReducer` with your actual reducer and adjust the imports as needed for your project's structure.

src/app/__tests__/action.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React from 'react';
22
import { render, screen, fireEvent } from '@testing-library/react';
3-
import user from '@testing-library/user-event';
3+
// import user from '@testing-library/user-event'; //might be unused
44
import '@testing-library/jest-dom/extend-expect'; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
55
import Action from '../components/Action';
6-
import { changeView, changeSlider } from '../actions/actions';
6+
import { changeView, changeSlider } from '../RTKslices';
7+
import { Provider } from 'react-redux';
8+
import { store } from '../RTKstore'; //importing store for testing to give us access to Redux Store we configured
9+
710

811
// @ts-ignore
912
Action.cleanTime = jest.fn().mockReturnValue();

0 commit comments

Comments
 (0)