@@ -3,7 +3,7 @@ import React from 'react';
3
3
import { render , screen , fireEvent } from '@testing-library/react' ;
4
4
import { Provider } from 'react-redux' ;
5
5
import { configureStore } from '@reduxjs/toolkit' ;
6
- import { MemoryRouter } from 'react-router-dom' ;
6
+ import { MemoryRouter , Route , Routes } from 'react-router-dom' ;
7
7
import '@testing-library/jest-dom' ;
8
8
9
9
import StateRoute from '../components/StateRoute/StateRoute' ;
@@ -17,39 +17,108 @@ class ResizeObserverMock {
17
17
disconnect ( ) { }
18
18
}
19
19
20
- // Setup global ResizeObserver mock
21
20
global . ResizeObserver = ResizeObserverMock ;
22
21
23
22
// Mock child components
24
23
jest . mock ( '../components/StateRoute/Tree' , ( ) => ( ) => (
25
24
< div data-testid = 'mock-tree' > Tree Component</ div >
26
25
) ) ;
26
+
27
27
jest . mock ( '../components/StateRoute/ComponentMap/ComponentMap' , ( ) => ( ) => (
28
28
< div data-testid = 'mock-component-map' > Component Map</ div >
29
29
) ) ;
30
+
30
31
jest . mock ( '../components/StateRoute/PerformanceVisx/PerformanceVisx' , ( ) => ( ) => (
31
32
< div data-testid = 'mock-performance' > Performance Component</ div >
32
33
) ) ;
34
+
33
35
jest . mock ( '../components/StateRoute/WebMetrics/WebMetricsContainer' , ( ) => ( ) => (
34
36
< div data-testid = 'mock-web-metrics' > Web Metrics Component</ div >
35
37
) ) ;
38
+
36
39
jest . mock ( '../components/StateRoute/AxMap/AxContainer' , ( ) => ( ) => (
37
40
< div data-testid = 'mock-ax-container' > Ax Container</ div >
38
41
) ) ;
42
+
39
43
jest . mock ( '../components/StateRoute/History' , ( ) => ( {
40
44
default : ( ) => < div data-testid = 'mock-history' > History Component</ div > ,
41
45
} ) ) ;
42
46
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
+
43
114
// Mock ParentSize component
44
115
jest . mock ( '@visx/responsive' , ( ) => ( {
45
116
ParentSize : ( { children } ) => children ( { width : 100 , height : 100 } ) ,
46
117
} ) ) ;
47
118
48
- // Create mock store factory with proper initial state
49
119
const createMockStore = ( initialState = { } ) => {
50
120
return configureStore ( {
51
121
reducer : {
52
- // @ts -ignore
53
122
main : mainSlice . reducer ,
54
123
} ,
55
124
preloadedState : {
@@ -62,12 +131,20 @@ const createMockStore = (initialState = {}) => {
62
131
} ,
63
132
] ,
64
133
currentTab : 0 ,
134
+ // @ts -ignore
135
+ port : {
136
+ postMessage : jest . fn ( ) ,
137
+ } ,
65
138
...initialState ,
66
139
} ,
67
140
} ,
68
141
} ) ;
69
142
} ;
70
143
144
+ afterEach ( ( ) => {
145
+ jest . clearAllMocks ( ) ;
146
+ } ) ;
147
+
71
148
describe ( 'State Components' , ( ) => {
72
149
const defaultProps = {
73
150
axSnapshots : [ ] ,
@@ -80,21 +157,22 @@ describe('State Components', () => {
80
157
} ;
81
158
82
159
describe ( 'StateRoute Component' , ( ) => {
83
- const renderStateRoute = ( props = { } , initialState = { } ) => {
160
+ const renderStateRoute = ( props = { } , initialState = { } , initialPath = '/' ) => {
84
161
const store = createMockStore ( initialState ) ;
85
162
return render (
86
163
< 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 >
90
169
</ MemoryRouter >
91
170
</ Provider > ,
92
171
) ;
93
172
} ;
94
173
95
174
it ( 'renders navigation links correctly' , ( ) => {
96
175
renderStateRoute ( ) ;
97
-
98
176
expect ( screen . getByRole ( 'link' , { name : / m a p / i } ) ) . toBeInTheDocument ( ) ;
99
177
expect ( screen . getByRole ( 'link' , { name : / h i s t o r y / i } ) ) . toBeInTheDocument ( ) ;
100
178
expect ( screen . getByRole ( 'link' , { name : / p e r f o r m a n c e / i } ) ) . toBeInTheDocument ( ) ;
@@ -104,11 +182,7 @@ describe('State Components', () => {
104
182
} ) ;
105
183
106
184
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 : / a c c e s s i b i l i t y / i } ) ;
111
- fireEvent . click ( accessibilityLink ) ;
185
+ renderStateRoute ( { } , { port : { postMessage : jest . fn ( ) } } , '/accessibility' ) ;
112
186
113
187
// Check initial state
114
188
expect ( screen . getByText ( / a n o t e t o d e v e l o p e r s / i) ) . toBeInTheDocument ( ) ;
@@ -118,7 +192,6 @@ describe('State Components', () => {
118
192
fireEvent . click ( enableRadio ) ;
119
193
120
194
// Verify the accessibility container is shown
121
- expect ( screen . queryByText ( / a n o t e t o d e v e l o p e r s / i) ) . not . toBeInTheDocument ( ) ;
122
195
expect ( screen . getByTestId ( 'mock-ax-container' ) ) . toBeInTheDocument ( ) ;
123
196
} ) ;
124
197
0 commit comments