Skip to content

Commit 84c282d

Browse files
committed
error container test file converted to RTK
1 parent 5a613ef commit 84c282d

File tree

1 file changed

+137
-11
lines changed

1 file changed

+137
-11
lines changed

src/app/__tests__/ErrorContainer.test.tsx

Lines changed: 137 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,152 @@
11
import React from 'react';
2-
import { render, screen } from '@testing-library/react';
2+
import { render as rtlRender, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect'; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
44
import ErrorContainer from '../containers/ErrorContainer';
5-
import { useStoreContext } from '../store';
5+
// import { useStoreContext } from '../store';
6+
import { configureStore } from '@reduxjs/toolkit';
7+
import { mainSlice } from '../RTKslices'
8+
import { useDispatch, Provider } from 'react-redux';
69

7-
const state = {
8-
currentTab: null,
9-
currentTitle: 'No Target',
10-
tabs: {},
10+
const customTabs = {
11+
87: {
12+
snapshots: [1, 2, 3, 4],
13+
hierarchy: {
14+
index: 0,
15+
name: 1,
16+
branch: 0,
17+
stateSnapshot: {
18+
state: {},
19+
children: [
20+
{
21+
state: { test: 'test' },
22+
name: 'App',
23+
componentData: { actualDuration: 3.5 },
24+
},
25+
],
26+
route: {
27+
id: 1,
28+
url: 'http://localhost:8080/',
29+
},
30+
},
31+
children: [
32+
{
33+
index: 1,
34+
name: 2,
35+
branch: 0,
36+
stateSnapshot: {
37+
state: {},
38+
children: [
39+
{
40+
state: { test: 'test' },
41+
name: 'App',
42+
componentData: { actualDuration: 3.5 },
43+
},
44+
],
45+
route: {
46+
id: 2,
47+
url: 'http://localhost:8080/',
48+
},
49+
},
50+
children: [
51+
{
52+
index: 2,
53+
name: 3,
54+
branch: 0,
55+
stateSnapshot: {
56+
state: {},
57+
children: [
58+
{
59+
state: { test: 'test' },
60+
name: 'App',
61+
componentData: { actualDuration: 3.5 },
62+
},
63+
],
64+
route: {
65+
id: 3,
66+
url: 'http://localhost:8080/',
67+
},
68+
},
69+
children: [
70+
{
71+
index: 3,
72+
name: 4,
73+
branch: 0,
74+
stateSnapshot: {
75+
state: {},
76+
children: [
77+
{
78+
state: { test: 'test' },
79+
name: 'App',
80+
componentData: { actualDuration: 3.5 },
81+
},
82+
],
83+
route: {
84+
id: 4,
85+
url: 'http://localhost:8080/test/',
86+
},
87+
},
88+
children: [],
89+
},
90+
],
91+
},
92+
],
93+
},
94+
],
95+
},
96+
currLocation: {
97+
index: 0,
98+
name: 1,
99+
branch: 0,
100+
},
101+
sliderIndex: 0,
102+
viewIndex: -1,
103+
},
104+
}
105+
106+
const customInitialState = {
107+
main: {
108+
port: null,
109+
currentTab: 87, // Update with your desired value
110+
currentTitle: 'No Target', //I updated this to 'No Target' to match the initialState in RTKslices.tsx. It used to be null, but that doesn't make sense because if it doesn't get updated, we would render null instead of No Target
111+
tabs: customTabs, // Replace with the actual (testing) tab data
112+
currentTabInApp: 'null',
113+
connectionStatus: false,
114+
connectRequested: true,
115+
},
11116
};
12117

118+
const customStore = configureStore({
119+
reducer: {
120+
main: mainSlice.reducer,
121+
},
122+
preloadedState: customInitialState, // Provide custom initial state
123+
middleware: (getDefaultMiddleware) =>
124+
getDefaultMiddleware({ serializableCheck: false }),
125+
});
126+
127+
const render = component => rtlRender(
128+
<Provider store={customStore}>
129+
{component}
130+
</Provider>
131+
);
132+
133+
// const state = {
134+
// currentTab: null,
135+
// currentTitle: 'No Target',
136+
// tabs: {},
137+
// };
138+
13139
const MockErrorMsg = jest.fn();
14140
jest.mock('../components/ErrorMsg', () => () => {
15141
MockErrorMsg();
16142
return <div>MockErrorMsg</div>;
17143
});
18144

19-
jest.mock('../store');
20-
const mockeduseStoreContext = jest.mocked(useStoreContext);
145+
// jest.mock('../store');
146+
// const mockeduseStoreContext = jest.mocked(useStoreContext);
21147

22-
const dispatch = jest.fn();
23-
mockeduseStoreContext.mockImplementation(() => [state, dispatch]);
148+
// const dispatch = jest.fn();
149+
// mockeduseStoreContext.mockImplementation(() => [state, dispatch]);
24150

25151
describe('unit testing for ErrorContainer.tsx', () => {
26152
test('logo image renders as expected', () => {
@@ -65,7 +191,7 @@ describe('unit testing for ErrorContainer.tsx', () => {
65191
});
66192

67193
test('When currentTitle has a target title', () => {
68-
state.currentTitle = 'Test Page';
194+
customInitialState.main.currentTitle = 'Test Page';
69195
render(<ErrorContainer />);
70196
expect(screen.getByText(`Launching Reactime on tab: Test Page`)).toBeInTheDocument();
71197
expect(screen.queryByText(`Launching Reactime on tab: No Target`)).not.toBeInTheDocument();

0 commit comments

Comments
 (0)