Skip to content

Commit c5e9f74

Browse files
committed
added state container tests
1 parent 41eb204 commit c5e9f74

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// State.test.tsx
2+
import React from 'react';
3+
import { render, screen, fireEvent } from '@testing-library/react';
4+
import { Provider } from 'react-redux';
5+
import { configureStore } from '@reduxjs/toolkit';
6+
import { MemoryRouter } from 'react-router-dom';
7+
import '@testing-library/jest-dom';
8+
9+
import StateRoute from '../components/StateRoute/StateRoute';
10+
import StateContainer from '../containers/StateContainer';
11+
import { mainSlice } from '../slices/mainSlice';
12+
13+
// Mock ResizeObserver
14+
class ResizeObserverMock {
15+
observe() {}
16+
unobserve() {}
17+
disconnect() {}
18+
}
19+
20+
// Setup global ResizeObserver mock
21+
global.ResizeObserver = ResizeObserverMock;
22+
23+
// Mock child components
24+
jest.mock('../components/StateRoute/Tree', () => () => (
25+
<div data-testid='mock-tree'>Tree Component</div>
26+
));
27+
jest.mock('../components/StateRoute/ComponentMap/ComponentMap', () => () => (
28+
<div data-testid='mock-component-map'>Component Map</div>
29+
));
30+
jest.mock('../components/StateRoute/PerformanceVisx/PerformanceVisx', () => () => (
31+
<div data-testid='mock-performance'>Performance Component</div>
32+
));
33+
jest.mock('../components/StateRoute/WebMetrics/WebMetricsContainer', () => () => (
34+
<div data-testid='mock-web-metrics'>Web Metrics Component</div>
35+
));
36+
jest.mock('../components/StateRoute/AxMap/AxContainer', () => () => (
37+
<div data-testid='mock-ax-container'>Ax Container</div>
38+
));
39+
jest.mock('../components/StateRoute/History', () => ({
40+
default: () => <div data-testid='mock-history'>History Component</div>,
41+
}));
42+
43+
// Mock ParentSize component
44+
jest.mock('@visx/responsive', () => ({
45+
ParentSize: ({ children }) => children({ width: 100, height: 100 }),
46+
}));
47+
48+
// Create mock store factory with proper initial state
49+
const createMockStore = (initialState = {}) => {
50+
return configureStore({
51+
reducer: {
52+
// @ts-ignore
53+
main: mainSlice.reducer,
54+
},
55+
preloadedState: {
56+
main: {
57+
tabs: [
58+
{
59+
hierarchy: {},
60+
sliderIndex: 0,
61+
viewIndex: 0,
62+
},
63+
],
64+
currentTab: 0,
65+
...initialState,
66+
},
67+
},
68+
});
69+
};
70+
71+
describe('State Components', () => {
72+
const defaultProps = {
73+
axSnapshots: [],
74+
snapshot: {},
75+
hierarchy: {},
76+
snapshots: [],
77+
viewIndex: 0,
78+
webMetrics: {},
79+
currLocation: { stateSnapshot: {} },
80+
};
81+
82+
describe('StateRoute Component', () => {
83+
const renderStateRoute = (props = {}, initialState = {}) => {
84+
const store = createMockStore(initialState);
85+
return render(
86+
<Provider store={store}>
87+
<MemoryRouter initialEntries={['/']}>
88+
{/* @ts-ignore */}
89+
<StateRoute {...defaultProps} {...props} />
90+
</MemoryRouter>
91+
</Provider>,
92+
);
93+
};
94+
95+
it('renders navigation links correctly', () => {
96+
renderStateRoute();
97+
98+
expect(screen.getByRole('link', { name: /map/i })).toBeInTheDocument();
99+
expect(screen.getByRole('link', { name: /history/i })).toBeInTheDocument();
100+
expect(screen.getByRole('link', { name: /performance/i })).toBeInTheDocument();
101+
expect(screen.getByRole('link', { name: /web metrics/i })).toBeInTheDocument();
102+
expect(screen.getByRole('link', { name: /tree/i })).toBeInTheDocument();
103+
expect(screen.getByRole('link', { name: /accessibility/i })).toBeInTheDocument();
104+
});
105+
106+
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);
112+
113+
// Check initial state
114+
expect(screen.getByText(/a note to developers/i)).toBeInTheDocument();
115+
116+
// Find and click enable radio button
117+
const enableRadio = screen.getByRole('radio', { name: /enable/i });
118+
fireEvent.click(enableRadio);
119+
120+
// Verify the accessibility container is shown
121+
expect(screen.queryByText(/a note to developers/i)).not.toBeInTheDocument();
122+
expect(screen.getByTestId('mock-ax-container')).toBeInTheDocument();
123+
});
124+
125+
it('renders component map when hierarchy is provided', () => {
126+
renderStateRoute({
127+
hierarchy: { some: 'data' },
128+
currLocation: { stateSnapshot: { some: 'data' } },
129+
});
130+
131+
expect(screen.getByTestId('mock-component-map')).toBeInTheDocument();
132+
});
133+
134+
it('handles route changes correctly', () => {
135+
renderStateRoute({
136+
hierarchy: { some: 'data' },
137+
});
138+
139+
const historyLink = screen.getByRole('link', { name: /history/i });
140+
fireEvent.click(historyLink);
141+
expect(screen.getByTestId('mock-history')).toBeInTheDocument();
142+
143+
const performanceLink = screen.getByRole('link', { name: /performance/i });
144+
fireEvent.click(performanceLink);
145+
expect(screen.getByTestId('mock-performance')).toBeInTheDocument();
146+
});
147+
});
148+
149+
describe('StateContainer Component', () => {
150+
const renderStateContainer = (props = {}) => {
151+
const store = createMockStore();
152+
return render(
153+
<Provider store={store}>
154+
<MemoryRouter>
155+
{/* @ts-ignore */}
156+
<StateContainer {...defaultProps} {...props} />
157+
</MemoryRouter>
158+
</Provider>,
159+
);
160+
};
161+
162+
it('renders without crashing', () => {
163+
renderStateContainer();
164+
expect(screen.getByTestId('mock-state-route')).toBeInTheDocument();
165+
});
166+
167+
it('renders structural navbar container', () => {
168+
renderStateContainer();
169+
expect(document.querySelector('.main-navbar-container--structural')).toBeInTheDocument();
170+
});
171+
172+
it('passes props correctly to StateRoute', () => {
173+
const testProps = {
174+
snapshot: { test: 'snapshot' },
175+
hierarchy: { test: 'hierarchy' },
176+
snapshots: [{ test: 'snapshots' }],
177+
viewIndex: 1,
178+
webMetrics: { test: 'metrics' },
179+
currLocation: { test: 'location' },
180+
axSnapshots: [{ test: 'ax' }],
181+
};
182+
183+
renderStateContainer(testProps);
184+
expect(screen.getByTestId('mock-state-route')).toBeInTheDocument();
185+
});
186+
187+
it('handles nested routes correctly', () => {
188+
renderStateContainer();
189+
expect(screen.getByTestId('mock-state-route')).toBeInTheDocument();
190+
});
191+
});
192+
});

0 commit comments

Comments
 (0)