Skip to content

Commit c476893

Browse files
committed
testing changes for action.test file
1 parent 9257080 commit c476893

File tree

3 files changed

+159
-188
lines changed

3 files changed

+159
-188
lines changed

src/app/RTKslices.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,13 @@ export const mainSlice = createSlice({
170170

171171
changeView: (state, action) => {
172172
const {tabs, currentTab} = state;
173+
console.log('this is state:', current(state))
174+
console.log('this is tabs:', tabs)
175+
console.log('this is currentabs:', currentTab)
176+
console.log('this is tabs[currentab]', tabs[currentTab])
173177
const {viewIndex} = tabs[currentTab] || {};
174-
178+
console.log('hi this is viewIndex:', viewIndex);
179+
console.log('this is action payload', action.payload)
175180
tabs[currentTab].viewIndex = viewIndex === action.payload ? -1 : action.payload;
176181
},
177182

Lines changed: 62 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
/* eslint-disable react/jsx-filename-extension */
33
import React from 'react';
4-
import { render, screen, fireEvent } from '@testing-library/react';
4+
import { render as rtlRender, screen, fireEvent } from '@testing-library/react';
55
import '@testing-library/jest-dom/extend-expect';
66
import ActionContainer from '../containers/ActionContainer';
7-
7+
// import { useStoreContext } from '../store';
88
import TravelContainer from '../containers/TravelContainer';
9-
import { useDispatch, useSelector } from 'react-redux';
10-
import {store} from '../RTKstore';
11-
import { Provider } from 'react-redux';
12-
import { mainSlice } from '../RTKslices';
9+
import { Provider, useDispatch, useSelector } from 'react-redux';
10+
import { store } from '../RTKstore';
11+
//so far i have imported provider, usedispatch, useselector, and store
12+
//wrapped components in provider
13+
14+
const render = component => rtlRender(
15+
<Provider store={store}>
16+
{component}
17+
</Provider>
18+
)
19+
1320
const state = {
1421
tabs: {
1522
87: {
@@ -108,19 +115,36 @@ const state = {
108115
},
109116
currentTab: 87,
110117
};
111-
const dispatch = useDispatch();
112-
// const dispatch = jest.fn();
113-
// jest.spyOn(React, 'useEffect').mockImplementation(() => jest.fn());
114-
// jest.mock('../store');
118+
//creates jest mock function to simulate behavior of functions/methods
119+
const dispatch = jest.fn();
120+
121+
//TESTING OUR CODE HERRE
122+
123+
124+
115125

116-
// const mockeduseStoreContext = jest.mocked(useStoreContext);
117-
// mockeduseStoreContext.mockImplementation(() => [state, dispatch]);
126+
//ORGINAL CODE HERE
118127

119-
// const getStateMock = jest.spyOn(store, 'getState').mockReturnValue(state.main); // Replace 'state' with your desired initial state
128+
jest.spyOn(React, 'useEffect').mockImplementation(() => jest.fn());
129+
jest.mock('../store');
120130

131+
//jest.spyOn(React, 'useEffect').mockImplementation(() => jest.fn());:
132+
//This line spies on the useEffect function from React, replacing it with a mocked implementation that returns an empty Jest mock function,
133+
//effectively disabling its actual side effects during testing.
121134

122-
// const dispatchMock = jest.spyOn(store, 'dispatch'); // Create a spy for the dispatch function
135+
//jest.mock('../store');:
136+
//This line mocks the import of a module located at '../store', which can be useful to isolate components from real Redux store behavior
137+
//and provide custom mock behavior for testing purposes.
123138

139+
140+
const mockeduseStoreContext = jest.mocked(useStoreContext);
141+
mockeduseStoreContext.mockImplementation(() => [state, dispatch]);
142+
// jest.mocked(useStoreContext): This part of the code uses Jest's jest.mocked function to create a mocked version of the useStoreContext function. The jest.mocked function is used to mock functions and methods. It creates a mock that can be configured with custom behavior.
143+
144+
// mockeduseStoreContext.mockImplementation(() => [state, dispatch]): After creating the mock, this line configures the mock to implement a specific behavior. In this case, it specifies that when useStoreContext is called, it should return an array containing two values: state and dispatch.
145+
146+
147+
////////////////////////////////////////////////////////////////////////////////////
124148
const MockRouteDescription = jest.fn();
125149
jest.mock('../components/RouteDescription', () => () => {
126150
MockRouteDescription();
@@ -135,129 +159,45 @@ jest.mock('../components/SwitchApp', () => () => {
135159

136160
describe('unit testing for ActionContainer', () => {
137161
beforeEach(() => {
138-
// mockeduseStoreContext.mockClear();
139-
// dispatch.mockClear();
162+
mockeduseStoreContext.mockClear();
163+
dispatch.mockClear();
140164
render(
141-
<Provider store={store}>
142165
<ActionContainer actionView={true} />
143-
</Provider>
144-
);
166+
)
145167
});
146168

147169
test('Expect top arrow to be rendered', () => {
148170
expect(screen.getByRole('complementary')).toBeInTheDocument();
149171
});
150172

151-
test('Expect RouteDescription to be rendered', () => {
152-
expect(screen.getAllByText('MockRouteDescription')).toHaveLength(2);
153-
});
154-
155-
test('Expect SwitchApp to be rendered', () => {
156-
expect(screen.getByText('MockSwitchApp')).toBeInTheDocument();
157-
});
158-
159-
test('Click works on clear button', () => {
160-
fireEvent.click(screen.getAllByRole('button')[0]);
161-
expect(dispatch).toHaveBeenCalledTimes(1);
162-
});
163-
});
164-
165-
describe('integration testing for ActionContainer', () => {
166-
beforeEach(() => {
167-
// mockeduseStoreContext.mockClear();
168-
// dispatch.mockClear();
169-
render(
170-
<Provider store={store}>
171-
<ActionContainer actionView={true} />
172-
</Provider>
173-
);
174-
render(
175-
<Provider store={store}>
176-
<TravelContainer snapshotsLength={0} />
177-
</Provider>
178-
);
179-
});
180-
181-
test('Slider resets on clear button', () => {
182-
fireEvent.click(screen.getAllByRole('button')[0]);
183-
expect(screen.getByRole('slider')).toHaveStyle('left: 0');
184-
});
185-
});
186-
187-
188-
// 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:
189-
190-
// 1. Import `configureStore` from Redux Toolkit and the Redux store you want to use in your tests.
191-
192-
// ```javascript
193-
// import { render, screen, fireEvent } from '@testing-library/react';
194-
// import { Provider } from 'react-redux'; // Import Provider from react-redux
195-
// import { configureStore } from '@reduxjs/toolkit'; // Import configureStore from Redux Toolkit
196-
// import ActionContainer from '../containers/ActionContainer';
197-
// import TravelContainer from '../containers/TravelContainer';
198-
199-
// // Import your Redux store and slice here if not already done
200-
// import { rootReducer } from '../store'; // Replace with your actual reducer and store
201-
// ```
202-
203-
// 2. Create a Redux store with `configureStore` and pass it as a prop to your components.
204-
205-
// ```javascript
206-
// const store = configureStore({
207-
// reducer: rootReducer, // Replace with your actual reducer
208-
// });
173+
// test('Expect RouteDescription to be rendered', () => {
174+
// expect(screen.getAllByText('MockRouteDescription')).toHaveLength(2);
175+
// });
209176

210-
// describe('unit testing for ActionContainer', () => {
211-
// beforeEach(() => {
212-
// render(
213-
// <Provider store={store}>
214-
// <ActionContainer actionView={true} />
215-
// </Provider>
216-
// );
177+
// test('Expect SwitchApp to be rendered', () => {
178+
// expect(screen.getByText('MockSwitchApp')).toBeInTheDocument();
217179
// });
218180

219-
// // Your tests here
181+
// test('Click works on clear button', () => {
182+
// fireEvent.click(screen.getAllByRole('button')[0]);
183+
// expect(dispatch).toHaveBeenCalledTimes(1);
184+
// });
220185
// });
221186

222187
// describe('integration testing for ActionContainer', () => {
223188
// beforeEach(() => {
189+
// mockeduseStoreContext.mockClear();
190+
// dispatch.mockClear();
224191
// render(
225-
// <Provider store={store}>
226-
// <ActionContainer actionView={true} />
227-
// <TravelContainer snapshotsLength={0} />
228-
// </Provider>
229-
// );
230-
// });
231-
232-
// // Your tests here
233-
// });
234-
// ```
235-
236-
// 3. Ensure that you import and use `useDispatch` from `react-redux` for your component testing as follows:
237-
238-
// ```javascript
239-
// import { useDispatch, useSelector } from 'react-redux'; // Import useDispatch and useSelector from react-redux
240-
// ```
241-
242-
// And in your component code where you use `dispatch`, use `useDispatch`:
243-
244-
// ```javascript
245-
// const dispatch = useDispatch();
246-
// ```
247-
248-
// 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:
249-
250-
// ```javascript
251-
// test('Click works on clear button', () => {
252-
// const { getByRole } = render(
253-
// <Provider store={store}>
254192
// <ActionContainer actionView={true} />
255-
// </Provider>
256-
// );
257-
258-
// fireEvent.click(getByRole('button'));
259-
// expect(dispatch).toHaveBeenCalledTimes(1);
260-
// });
261-
// ```
193+
// )
194+
// render(
195+
// <TravelContainer snapshotsLength={0} />
196+
// )
197+
// });
262198

263-
// 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.
199+
// test('Slider resets on clear button', () => {
200+
// fireEvent.click(screen.getAllByRole('button')[0]);
201+
// expect(screen.getByRole('slider')).toHaveStyle('left: 0');
202+
// });
203+
});

0 commit comments

Comments
 (0)