Skip to content

Commit acd6f9e

Browse files
authored
Merge pull request #29 from oslabs-beta/andrewb
Andrewb
2 parents cefbffa + e291eba commit acd6f9e

File tree

4 files changed

+83
-39
lines changed

4 files changed

+83
-39
lines changed

src/app/__tests__/ActionContainer.test.tsx

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { store } from '../RTKstore';
1111
//so far i have imported provider, usedispatch, useselector, and store
1212
//wrapped components in provider
1313

14+
// jest.mock('react-redux', () => ({
15+
// ...jest.requireActual('react-redux'), // include all the exports from the actual react-redux module
16+
// useDispatch: jest.fn(),
17+
// useSelector: jest.fn() //override the useDispatch from the react redux module with a jest mock function
18+
// }));
19+
1420
// const render = component => rtlRender(
1521
// <Provider store={store}>
1622
// {component}
@@ -119,30 +125,15 @@ const state = {
119125
//creates jest mock function to simulate behavior of functions/methods
120126
const dispatch = jest.fn();
121127

122-
//TESTING OUR CODE HERRE
123-
124-
125-
126-
127-
//ORGINAL CODE HERE
128-
129128
jest.spyOn(React, 'useEffect').mockImplementation(() => jest.fn());
130-
jest.mock('../store');
131-
132-
//jest.spyOn(React, 'useEffect').mockImplementation(() => jest.fn());:
133129
//This line spies on the useEffect function from React, replacing it with a mocked implementation that returns an empty Jest mock function,
134130
//effectively disabling its actual side effects during testing.
135-
136-
//jest.mock('../store');:
131+
jest.mock('../store');
137132
//This line mocks the import of a module located at '../store', which can be useful to isolate components from real Redux store behavior
138133
//and provide custom mock behavior for testing purposes.
139134

140-
141135
const mockeduseStoreContext = jest.mocked(useStoreContext);
142-
mockeduseStoreContext.mockImplementation(() => [state, dispatch]);
143-
// 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.
144-
145-
// 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.
136+
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.
146137

147138

148139
////////////////////////////////////////////////////////////////////////////////////
@@ -171,19 +162,19 @@ describe('unit testing for ActionContainer', () => {
171162
expect(screen.getByRole('complementary')).toBeInTheDocument();
172163
});
173164

174-
// test('Expect RouteDescription to be rendered', () => {
175-
// expect(screen.getAllByText('MockRouteDescription')).toHaveLength(2);
176-
// });
165+
test('Expect RouteDescription to be rendered', () => {
166+
expect(screen.getAllByText('MockRouteDescription')).toHaveLength(2);
167+
});
177168

178-
// test('Expect SwitchApp to be rendered', () => {
179-
// expect(screen.getByText('MockSwitchApp')).toBeInTheDocument();
180-
// });
169+
test('Expect SwitchApp to be rendered', () => {
170+
expect(screen.getByText('MockSwitchApp')).toBeInTheDocument();
171+
});
181172

182-
// test('Click works on clear button', () => {
183-
// fireEvent.click(screen.getAllByRole('button')[0]);
184-
// expect(dispatch).toHaveBeenCalledTimes(1);
185-
// });
186-
// });
173+
test('Click works on clear button', () => {
174+
fireEvent.click(screen.getAllByRole('button')[0]);
175+
expect(dispatch).toHaveBeenCalledTimes(1);
176+
});
177+
});
187178

188179
// describe('integration testing for ActionContainer', () => {
189180
// beforeEach(() => {
@@ -197,8 +188,8 @@ describe('unit testing for ActionContainer', () => {
197188
// )
198189
// });
199190

200-
// test('Slider resets on clear button', () => {
201-
// fireEvent.click(screen.getAllByRole('button')[0]);
202-
// expect(screen.getByRole('slider')).toHaveStyle('left: 0');
203-
// });
191+
test('Slider resets on clear button', () => {
192+
fireEvent.click(screen.getAllByRole('button')[0]);
193+
expect(screen.getByRole('slider')).toHaveStyle('left: 0');
194+
});
204195
});

src/app/__tests__/ActionContainerV2.test.tsx

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Provider } from 'react-redux';
77
import { configureStore } from '@reduxjs/toolkit';
88
import { store } from '../RTKstore';
99
import { mainSlice } from '../RTKslices'
10-
10+
import { useDispatch } from 'react-redux';
1111
//Note for testing:
1212
//typically, jest.mock is commonly used in unit testing to isolate the code under test.
1313
//In contrast, when performing integration testing of components with a real Redux store,
@@ -137,12 +137,59 @@ const render = component => rtlRender(
137137
</Provider>
138138
);
139139

140+
const MockRouteDescription = jest.fn();
141+
jest.mock('../components/RouteDescription', () => () => {
142+
MockRouteDescription();
143+
return <div>MockRouteDescription</div>;
144+
});
145+
146+
const MockSwitchApp = jest.fn();
147+
jest.mock('../components/SwitchApp', () => () => {
148+
MockSwitchApp();
149+
return <div>MockSwitchApp</div>;
150+
});
140151
//need to add this mockFunction for setActionView
141152
//because in actual actioncontainer componenent, it is prop drilled down from maincontainer
142153
//here we set it as a jest.fn()
143154
//then we pass it into our actionContainer on render
144155
const setActionViewMock = jest.fn();
145156

157+
jest.mock('react-redux', () => ({
158+
...jest.requireActual('react-redux'), // Use the actual react-redux module except for the functions you want to mock
159+
useDispatch: jest.fn(), // set up a mock function for useDispatch
160+
}));
161+
162+
// const dispatch = jest.fn();
163+
164+
describe('unit testing for ActionContainer', ()=>{
165+
const useDispatchMock = useDispatch as jest.Mock; //getting a reference to the mock function you setup during jest.mock configuration on line 18
166+
const dummyDispatch = jest.fn(); //separate mock function created because we need to explicitly define on line 30 what
167+
useDispatchMock.mockReturnValue(dummyDispatch);//exactly useDispatchMock returns (which is a jest.fn())
168+
beforeEach(()=>{
169+
render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
170+
});
171+
172+
test('expect top arrow to be rendered', ()=>{
173+
// render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
174+
expect(screen.getByRole('complementary')).toBeInTheDocument();
175+
});
176+
177+
test('Expect RouteDescription to be rendered', () => {
178+
// render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
179+
expect(screen.getAllByText('MockRouteDescription')).toHaveLength(2);
180+
});
181+
182+
test('Expect SwitchApp to be rendered', () => {
183+
// render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
184+
expect(screen.getByText('MockSwitchApp')).toBeInTheDocument();
185+
});
186+
187+
test('Click works on clear button', () => {
188+
fireEvent.click(screen.getAllByRole('button')[0]);
189+
expect(dummyDispatch).toHaveBeenCalledTimes(1);
190+
});
191+
})
192+
146193
describe('Integration testing for ActionContainer.tsx', () => {
147194
test('renders the ActionContainer component', () => {
148195
//tests that the clearButton is rendered by testing if we can get "Clear"
@@ -152,6 +199,13 @@ describe('Integration testing for ActionContainer.tsx', () => {
152199
expect(setActionViewMock).toHaveBeenCalledWith(true);
153200
expect(clearButton).toBeInTheDocument();
154201
});
202+
203+
test('Slider resets on clear button', ()=>{
204+
render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
205+
render( <TravelContainer snapshotsLength={0} />)
206+
fireEvent.click(screen.getAllByRole('button')[0]);
207+
expect(screen.getByRole('slider')).toHaveStyle('left: 0');
208+
});
155209
});
156210

157211

src/app/__tests__/action.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jest.mock('react-redux', () => ({
1818
useDispatch: jest.fn(), // set up a mock function for useDispatch
1919
}));
2020

21+
//wraps the component with our provider each time we use render
2122
const render = component => rtlRender(
2223
<Provider store={store}>
2324
{component}
@@ -130,8 +131,6 @@ describe('Unit testing for Action.tsx', () => {
130131
});
131132

132133
test('Clicking Jump button should trigger changeSlider and changeView', () => {
133-
// const dummyDispatch = jest.fn();
134-
// useDispatchMock.mockReturnValue(dummyDispatch);
135134
render(
136135
<Action {...props} />
137136
);

src/app/store.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// /* eslint-disable @typescript-eslint/no-explicit-any */
2-
// import React, { useContext } from 'react';
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import React, { useContext } from 'react';
33

4-
// export const StoreContext = React.createContext(); // we create a context object and assign it to StoreContext
5-
// export const useStoreContext = () => useContext(StoreContext); // the useStoreContext function allows us to use use the above declared StoreContext.
4+
export const StoreContext = React.createContext(); // we create a context object and assign it to StoreContext
5+
export const useStoreContext = () => useContext(StoreContext); // the useStoreContext function allows us to use use the above declared StoreContext.

0 commit comments

Comments
 (0)