Skip to content

Commit b455af6

Browse files
authored
Merge pull request #33 from oslabs-beta/drewfeature
converted WebMetrics.test.tsx to work with RTK
2 parents 545b749 + 3088b32 commit b455af6

File tree

1 file changed

+138
-6
lines changed

1 file changed

+138
-6
lines changed

src/app/__tests__/WebMetrics.test.tsx

Lines changed: 138 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,147 @@
11
import React from 'react';
2-
import { render } from '@testing-library/react';
2+
import { render as rtlRender } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44
import WebMetrics from '../components/WebMetrics';
5-
import { useStoreContext } from '../store';
5+
// import { useStoreContext } from '../store';
6+
import { useDispatch, Provider } from 'react-redux';
7+
import { configureStore } from '@reduxjs/toolkit';
8+
import { mainSlice } from '../RTKslices'
69

10+
jest.mock('react-redux', () => ({
11+
...jest.requireActual('react-redux'), // Use the actual react-redux module except for the functions you want to mock
12+
useDispatch: jest.fn(), // set up a mock function for useDispatch
13+
}));
14+
const useDispatchMock = useDispatch as jest.Mock; //getting a reference to the mock function you setup during jest.mock configuration on line 18
15+
const dummyDispatch = jest.fn(); //separate mock function created because we need to explicitly define on line 30 what
16+
useDispatchMock.mockReturnValue(dummyDispatch);//exactly useDispatchMock returns (which is a jest.fn())
17+
const customTabs = {
18+
87: {
19+
snapshots: [1, 2, 3, 4],
20+
hierarchy: {
21+
index: 0,
22+
name: 1,
23+
branch: 0,
24+
stateSnapshot: {
25+
state: {},
26+
children: [
27+
{
28+
state: { test: 'test' },
29+
name: 'App',
30+
componentData: { actualDuration: 3.5 },
31+
},
32+
],
33+
route: {
34+
id: 1,
35+
url: 'http://localhost:8080/',
36+
},
37+
},
38+
children: [
39+
{
40+
index: 1,
41+
name: 2,
42+
branch: 0,
43+
stateSnapshot: {
44+
state: {},
45+
children: [
46+
{
47+
state: { test: 'test' },
48+
name: 'App',
49+
componentData: { actualDuration: 3.5 },
50+
},
51+
],
52+
route: {
53+
id: 2,
54+
url: 'http://localhost:8080/',
55+
},
56+
},
57+
children: [
58+
{
59+
index: 2,
60+
name: 3,
61+
branch: 0,
62+
stateSnapshot: {
63+
state: {},
64+
children: [
65+
{
66+
state: { test: 'test' },
67+
name: 'App',
68+
componentData: { actualDuration: 3.5 },
69+
},
70+
],
71+
route: {
72+
id: 3,
73+
url: 'http://localhost:8080/',
74+
},
75+
},
76+
children: [
77+
{
78+
index: 3,
79+
name: 4,
80+
branch: 0,
81+
stateSnapshot: {
82+
state: {},
83+
children: [
84+
{
85+
state: { test: 'test' },
86+
name: 'App',
87+
componentData: { actualDuration: 3.5 },
88+
},
89+
],
90+
route: {
91+
id: 4,
92+
url: 'http://localhost:8080/test/',
93+
},
94+
},
95+
children: [],
96+
},
97+
],
98+
},
99+
],
100+
},
101+
],
102+
},
103+
currLocation: {
104+
index: 0,
105+
name: 1,
106+
branch: 0,
107+
},
108+
sliderIndex: 0,
109+
viewIndex: -1,
110+
},
111+
}
112+
113+
const customInitialState = {
114+
main: {
115+
port: null,
116+
currentTab: 87, // Update with your desired value
117+
currentTitle: null,
118+
tabs: customTabs, // Replace with the actual (testing) tab data
119+
currentTabInApp: null,
120+
connectionStatus: false,
121+
connectRequested: true,
122+
},
123+
};
124+
125+
const customStore = configureStore({
126+
reducer: {
127+
main: mainSlice.reducer,
128+
},
129+
preloadedState: customInitialState, // Provide custom initial state
130+
middleware: (getDefaultMiddleware) =>
131+
getDefaultMiddleware({ serializableCheck: false }),
132+
});
133+
134+
const render = component => rtlRender(
135+
<Provider store={customStore}>
136+
{component}
137+
</Provider>
138+
);
7139
jest.mock('react-apexcharts', () => ({ __esModule: true, default: () => <div /> }));
8-
const dispatch = jest.fn();
140+
// const dispatch = jest.fn();
9141
// jest.spyOn(React, 'useEffect').mockImplementation(() => jest.fn());
10-
jest.mock('../store');
11-
const mockedStoreContext = jest.mocked(useStoreContext);
12-
mockedStoreContext.mockImplementation(() => [, dispatch]);
142+
// jest.mock('../store');
143+
// const mockedStoreContext = jest.mocked(useStoreContext);
144+
// mockedStoreContext.mockImplementation(() => [, dummyDispatch]);
13145

14146
describe('WebMetrics graph testing', () => {
15147
test('should have 1 div with class name "metric" ', () => {

0 commit comments

Comments
 (0)