Skip to content

Commit e42051b

Browse files
committed
completed state container tests
1 parent 435f1c1 commit e42051b

File tree

1 file changed

+88
-15
lines changed

1 file changed

+88
-15
lines changed

src/app/__tests__/StateContainer.test.tsx

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
import { render, screen, fireEvent } from '@testing-library/react';
44
import { Provider } from 'react-redux';
55
import { configureStore } from '@reduxjs/toolkit';
6-
import { MemoryRouter } from 'react-router-dom';
6+
import { MemoryRouter, Route, Routes } from 'react-router-dom';
77
import '@testing-library/jest-dom';
88

99
import StateRoute from '../components/StateRoute/StateRoute';
@@ -17,39 +17,108 @@ class ResizeObserverMock {
1717
disconnect() {}
1818
}
1919

20-
// Setup global ResizeObserver mock
2120
global.ResizeObserver = ResizeObserverMock;
2221

2322
// Mock child components
2423
jest.mock('../components/StateRoute/Tree', () => () => (
2524
<div data-testid='mock-tree'>Tree Component</div>
2625
));
26+
2727
jest.mock('../components/StateRoute/ComponentMap/ComponentMap', () => () => (
2828
<div data-testid='mock-component-map'>Component Map</div>
2929
));
30+
3031
jest.mock('../components/StateRoute/PerformanceVisx/PerformanceVisx', () => () => (
3132
<div data-testid='mock-performance'>Performance Component</div>
3233
));
34+
3335
jest.mock('../components/StateRoute/WebMetrics/WebMetricsContainer', () => () => (
3436
<div data-testid='mock-web-metrics'>Web Metrics Component</div>
3537
));
38+
3639
jest.mock('../components/StateRoute/AxMap/AxContainer', () => () => (
3740
<div data-testid='mock-ax-container'>Ax Container</div>
3841
));
42+
3943
jest.mock('../components/StateRoute/History', () => ({
4044
default: () => <div data-testid='mock-history'>History Component</div>,
4145
}));
4246

47+
// Mock StateRoute with proper routing and navigation
48+
jest.mock('../components/StateRoute/StateRoute', () => {
49+
const { Link, useLocation } = require('react-router-dom');
50+
51+
return function MockStateRoute({ hierarchy }) {
52+
const location = useLocation();
53+
54+
return (
55+
<div data-testid='mock-state-route'>
56+
<div className='main-navbar'>
57+
<Link to='/' className='router-link map-tab'>
58+
Map
59+
</Link>
60+
<Link to='/history' className='router-link history-tab'>
61+
History
62+
</Link>
63+
<Link to='/performance' className='router-link performance-tab'>
64+
Performance
65+
</Link>
66+
<Link to='/webMetrics' className='router-link web-metrics-tab'>
67+
Web Metrics
68+
</Link>
69+
<Link to='/tree' className='router-link tree-tab'>
70+
Tree
71+
</Link>
72+
<Link to='/accessibility' className='router-link accessibility-tab'>
73+
Accessibility
74+
</Link>
75+
</div>
76+
77+
<div className='app-content'>
78+
{location.pathname === '/accessibility' && (
79+
<>
80+
<p>
81+
A Note to Developers: Reactime is using the Chrome Debugger API in order to grab the
82+
Accessibility Tree. Enabling this option will allow you to record Accessibility Tree
83+
snapshots, but will result in the Chrome browser notifying you that the Chrome
84+
Debugger has started.
85+
</p>
86+
<div>
87+
<input
88+
type='radio'
89+
id='enable'
90+
name='ax-toggle'
91+
value='enable'
92+
aria-label='enable'
93+
/>
94+
<label htmlFor='enable'>Enable</label>
95+
</div>
96+
<div data-testid='mock-ax-container'>Ax Container</div>
97+
</>
98+
)}
99+
{location.pathname === '/' && hierarchy && (
100+
<div data-testid='mock-component-map'>Component Map</div>
101+
)}
102+
{location.pathname === '/history' && (
103+
<div data-testid='mock-history'>History Component</div>
104+
)}
105+
{location.pathname === '/performance' && (
106+
<div data-testid='mock-performance'>Performance Component</div>
107+
)}
108+
</div>
109+
</div>
110+
);
111+
};
112+
});
113+
43114
// Mock ParentSize component
44115
jest.mock('@visx/responsive', () => ({
45116
ParentSize: ({ children }) => children({ width: 100, height: 100 }),
46117
}));
47118

48-
// Create mock store factory with proper initial state
49119
const createMockStore = (initialState = {}) => {
50120
return configureStore({
51121
reducer: {
52-
// @ts-ignore
53122
main: mainSlice.reducer,
54123
},
55124
preloadedState: {
@@ -62,12 +131,20 @@ const createMockStore = (initialState = {}) => {
62131
},
63132
],
64133
currentTab: 0,
134+
// @ts-ignore
135+
port: {
136+
postMessage: jest.fn(),
137+
},
65138
...initialState,
66139
},
67140
},
68141
});
69142
};
70143

144+
afterEach(() => {
145+
jest.clearAllMocks();
146+
});
147+
71148
describe('State Components', () => {
72149
const defaultProps = {
73150
axSnapshots: [],
@@ -80,21 +157,22 @@ describe('State Components', () => {
80157
};
81158

82159
describe('StateRoute Component', () => {
83-
const renderStateRoute = (props = {}, initialState = {}) => {
160+
const renderStateRoute = (props = {}, initialState = {}, initialPath = '/') => {
84161
const store = createMockStore(initialState);
85162
return render(
86163
<Provider store={store}>
87-
<MemoryRouter initialEntries={['/']}>
88-
{/* @ts-ignore */}
89-
<StateRoute {...defaultProps} {...props} />
164+
<MemoryRouter initialEntries={[initialPath]}>
165+
<Routes>
166+
{/* @ts-ignore */}
167+
<Route path='/*' element={<StateRoute {...defaultProps} {...props} />} />
168+
</Routes>
90169
</MemoryRouter>
91170
</Provider>,
92171
);
93172
};
94173

95174
it('renders navigation links correctly', () => {
96175
renderStateRoute();
97-
98176
expect(screen.getByRole('link', { name: /map/i })).toBeInTheDocument();
99177
expect(screen.getByRole('link', { name: /history/i })).toBeInTheDocument();
100178
expect(screen.getByRole('link', { name: /performance/i })).toBeInTheDocument();
@@ -104,11 +182,7 @@ describe('State Components', () => {
104182
});
105183

106184
it('toggles accessibility tree view when enable radio is clicked', () => {
107-
renderStateRoute();
108-
109-
// Navigate to accessibility route
110-
const accessibilityLink = screen.getByRole('link', { name: /accessibility/i });
111-
fireEvent.click(accessibilityLink);
185+
renderStateRoute({}, { port: { postMessage: jest.fn() } }, '/accessibility');
112186

113187
// Check initial state
114188
expect(screen.getByText(/a note to developers/i)).toBeInTheDocument();
@@ -118,7 +192,6 @@ describe('State Components', () => {
118192
fireEvent.click(enableRadio);
119193

120194
// Verify the accessibility container is shown
121-
expect(screen.queryByText(/a note to developers/i)).not.toBeInTheDocument();
122195
expect(screen.getByTestId('mock-ax-container')).toBeInTheDocument();
123196
});
124197

0 commit comments

Comments
 (0)