Skip to content

Commit e291eba

Browse files
committed
ActionContainer testing file complete, all tests passing
1 parent b85e4b9 commit e291eba

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

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

0 commit comments

Comments
 (0)