|
5 | 5 | import { render, screen, waitFor } from '@testing-library/react'
|
6 | 6 | import { MemoryRouter, Route, Routes } from 'react-router-dom-v5-compat'
|
7 | 7 | import { RecoilRoot } from 'recoil'
|
| 8 | +import { settingsState } from '../../../atoms' |
8 | 9 | import { nockGet, nockIgnoreApiPaths, nockIgnoreRBAC, nockPostRequest } from '../../../lib/nock-util'
|
9 | 10 | import { waitForNocks } from '../../../lib/test-util'
|
10 | 11 | import { NavigationPath } from '../../../NavigationPath'
|
11 | 12 | import Search from '../Search'
|
12 | 13 | import { getResourceParams } from './DetailsPage'
|
13 | 14 |
|
14 |
| -jest.mock('react-router-dom-v5-compat', () => { |
15 |
| - const originalModule = jest.requireActual('react-router-dom-v5-compat') |
16 |
| - return { |
17 |
| - __esModule: true, |
18 |
| - ...originalModule, |
19 |
| - useLocation: () => ({ |
20 |
| - pathname: '/multicloud/search/resources', |
21 |
| - search: |
22 |
| - '?cluster=local-cluster&kind=Pod&apiversion=v1&namespace=testNamespace&name=testLocalPod&_hubClusterResource=true', |
23 |
| - state: { |
24 |
| - from: '/multicloud/search', |
25 |
| - fromSearch: '?filters={%22textsearch%22:%22kind%3APod%22}', |
26 |
| - }, |
27 |
| - }), |
28 |
| - useNavigate: () => jest.fn(), |
29 |
| - } |
30 |
| -}) |
31 |
| -Object.defineProperty(window, 'location', { |
32 |
| - value: { |
33 |
| - pathname: '/multicloud/search/resources', |
34 |
| - search: |
35 |
| - '?cluster=local-cluster&kind=Pod&apiversion=v1&namespace=testNamespace&name=testLocalPod&_hubClusterResource=true', |
36 |
| - state: { |
37 |
| - from: '/multicloud/search', |
38 |
| - fromSearch: '?filters={%22textsearch%22:%22kind%3APod%2', |
39 |
| - }, |
40 |
| - }, |
41 |
| -}) |
42 |
| - |
43 | 15 | const mockLocalClusterPod = {
|
44 | 16 | kind: 'Pod',
|
45 | 17 | apiVersion: 'v1',
|
@@ -91,12 +63,47 @@ const mockLocalClusterPod = {
|
91 | 63 |
|
92 | 64 | describe('DetailsPage', () => {
|
93 | 65 | beforeEach(async () => {
|
| 66 | + // jest.resetAllMocks() |
94 | 67 | nockIgnoreRBAC()
|
95 | 68 | nockIgnoreApiPaths()
|
96 | 69 | })
|
| 70 | + afterEach(() => { |
| 71 | + jest.resetAllMocks() |
| 72 | + // Object.defineProperty(window, 'location', { |
| 73 | + // value: {}, |
| 74 | + // }) |
| 75 | + }) |
97 | 76 | const metricNock = nockPostRequest('/metrics?search-details', {})
|
98 | 77 |
|
99 | 78 | it('should render local-cluster resource details correctly', async () => {
|
| 79 | + jest.mock('react-router-dom-v5-compat', () => { |
| 80 | + const originalModule = jest.requireActual('react-router-dom-v5-compat') |
| 81 | + return { |
| 82 | + __esModule: true, |
| 83 | + ...originalModule, |
| 84 | + useLocation: () => ({ |
| 85 | + pathname: '/multicloud/search/resources', |
| 86 | + search: |
| 87 | + '?cluster=local-cluster&kind=Pod&apiversion=v1&namespace=testNamespace&name=testLocalPod&_hubClusterResource=true', |
| 88 | + state: { |
| 89 | + from: '/multicloud/search', |
| 90 | + fromSearch: '?filters={%22textsearch%22:%22kind%3APod%22}', |
| 91 | + }, |
| 92 | + }), |
| 93 | + useNavigate: () => jest.fn(), |
| 94 | + } |
| 95 | + }) |
| 96 | + Object.defineProperty(window, 'location', { |
| 97 | + value: { |
| 98 | + pathname: '/multicloud/search/resources', |
| 99 | + search: |
| 100 | + '?cluster=local-cluster&kind=Pod&apiversion=v1&namespace=testNamespace&name=testLocalPod&_hubClusterResource=true', |
| 101 | + state: { |
| 102 | + from: '/multicloud/search', |
| 103 | + fromSearch: '?filters={%22textsearch%22:%22kind%3APod%2', |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
100 | 107 | render(
|
101 | 108 | <RecoilRoot>
|
102 | 109 | <MemoryRouter initialEntries={[NavigationPath.resources]}>
|
@@ -148,4 +155,88 @@ describe('DetailsPage', () => {
|
148 | 155 | const res = getResourceParams()
|
149 | 156 | expect(res).toMatchSnapshot()
|
150 | 157 | })
|
| 158 | + |
| 159 | + it('should render VirtualMachine resource details correctly', async () => { |
| 160 | + jest.mock('react-router-dom-v5-compat', () => { |
| 161 | + const originalModule = jest.requireActual('react-router-dom-v5-compat') |
| 162 | + return { |
| 163 | + __esModule: true, |
| 164 | + ...originalModule, |
| 165 | + useLocation: () => ({ |
| 166 | + pathname: '/multicloud/search/resources', |
| 167 | + search: |
| 168 | + '?cluster=local-cluster&kind=VirtualMachine&apiversion=kubevirt.io/v1&namespace=openshift-cnv&name=test-vm&_hubClusterResource=true', |
| 169 | + state: { |
| 170 | + from: '/multicloud/search', |
| 171 | + fromSearch: '?filters={%22textsearch%22:%22kind%3AVirtualMachine%22}', |
| 172 | + }, |
| 173 | + }), |
| 174 | + useNavigate: () => jest.fn(), |
| 175 | + } |
| 176 | + }) |
| 177 | + Object.defineProperty(window, 'location', { |
| 178 | + value: { |
| 179 | + pathname: '/multicloud/search/resources', |
| 180 | + search: |
| 181 | + '?cluster=local-cluster&kind=VirtualMachine&apiversion=kubevirt.io/v1&namespace=openshift-cnv&name=test-vm&_hubClusterResource=true', |
| 182 | + state: { |
| 183 | + from: '/multicloud/search', |
| 184 | + fromSearch: '?filters={%22textsearch%22:%22kind%3AVirtualMachine%2', |
| 185 | + }, |
| 186 | + }, |
| 187 | + }) |
| 188 | + render( |
| 189 | + <RecoilRoot |
| 190 | + initializeState={(snapshot) => { |
| 191 | + snapshot.set(settingsState, { VIRTUAL_MACHINE_ACTIONS: 'enabled' }) |
| 192 | + }} |
| 193 | + > |
| 194 | + <MemoryRouter initialEntries={[NavigationPath.resources]}> |
| 195 | + <Routes> |
| 196 | + <Route path={`${NavigationPath.search}/*`} element={<Search />} /> |
| 197 | + </Routes> |
| 198 | + </MemoryRouter> |
| 199 | + </RecoilRoot> |
| 200 | + ) |
| 201 | + |
| 202 | + // Wait for delete resource requests to finish |
| 203 | + await waitForNocks([ |
| 204 | + metricNock, |
| 205 | + nockGet({ |
| 206 | + apiVersion: 'kubevirt.io/v1', |
| 207 | + kind: 'VirtualMachine', |
| 208 | + metadata: { |
| 209 | + creationTimestamp: '2024-10-02T20:02:14Z', |
| 210 | + name: 'test-vm', |
| 211 | + namespace: 'openshift-cnv', |
| 212 | + }, |
| 213 | + spec: { |
| 214 | + running: true, |
| 215 | + template: { |
| 216 | + metadata: { |
| 217 | + creationTimestamp: null, |
| 218 | + }, |
| 219 | + spec: {}, |
| 220 | + }, |
| 221 | + status: { |
| 222 | + created: true, |
| 223 | + desiredGeneration: 9, |
| 224 | + observedGeneration: 9, |
| 225 | + printableStatus: 'Running', |
| 226 | + ready: true, |
| 227 | + runStrategy: 'Always', |
| 228 | + }, |
| 229 | + }, |
| 230 | + }), |
| 231 | + ]) |
| 232 | + |
| 233 | + // Test that the component has rendered correctly with data |
| 234 | + await waitFor(() => |
| 235 | + expect( |
| 236 | + screen.getByRole('heading', { |
| 237 | + name: /test-vm/i, |
| 238 | + }) |
| 239 | + ).toBeTruthy() |
| 240 | + ) |
| 241 | + }) |
151 | 242 | })
|
0 commit comments