|
1 | | -import {render, screen} from '@testing-library/react'; |
2 | | -import {MemoryRouter} from 'react-router-dom'; |
| 1 | +import {screen, waitFor} from '@testing-library/react'; |
| 2 | +import {Router} from 'react-router-dom'; |
| 3 | +import {QueryParamProvider} from 'use-query-params'; |
| 4 | +import {ReactRouter5Adapter} from 'use-query-params/adapters/react-router-5'; |
3 | 5 |
|
| 6 | +import {configureStore} from '../../../store'; |
4 | 7 | import type {PreparedVDisk} from '../../../utils/disks/types'; |
| 8 | +import {renderWithStore} from '../../../utils/tests/providers'; |
5 | 9 | import {VDiskInfo} from '../VDiskInfo'; |
6 | 10 |
|
7 | | -jest.mock('../../../routes', () => ({ |
8 | | - getPDiskPagePath: (pDiskId: string | number, nodeId: string | number) => |
9 | | - `/pdisk-page?nodeId=${nodeId}&pDiskId=${pDiskId}`, |
10 | | - useVDiskPagePath: () => () => '/vdisk-page', |
11 | | -})); |
| 11 | +function renderWithWhoami({ |
| 12 | + data, |
| 13 | + isViewerAllowed = true, |
| 14 | + withVDiskPageLink, |
| 15 | +}: { |
| 16 | + data: PreparedVDisk; |
| 17 | + isViewerAllowed?: boolean; |
| 18 | + withVDiskPageLink?: boolean; |
| 19 | +}) { |
| 20 | + const whoami = jest.fn().mockResolvedValue({IsViewerAllowed: isViewerAllowed}); |
| 21 | + const api = { |
| 22 | + viewer: { |
| 23 | + whoami, |
| 24 | + }, |
| 25 | + }; |
12 | 26 |
|
13 | | -jest.mock('../../../utils/developerUI/developerUI', () => ({ |
14 | | - useHasDeveloperUi: () => false, |
15 | | -})); |
| 27 | + const storeConfiguration = configureStore({api: api as never}); |
| 28 | + |
| 29 | + renderWithStore( |
| 30 | + <Router history={storeConfiguration.history}> |
| 31 | + <QueryParamProvider adapter={ReactRouter5Adapter}> |
| 32 | + <VDiskInfo data={data} withVDiskPageLink={withVDiskPageLink} /> |
| 33 | + </QueryParamProvider> |
| 34 | + </Router>, |
| 35 | + {storeConfiguration}, |
| 36 | + ); |
| 37 | + |
| 38 | + return {whoami}; |
| 39 | +} |
16 | 40 |
|
17 | 41 | describe('VDiskInfo', () => { |
18 | 42 | test('renders VDisk page link as an internal link', () => { |
19 | | - render( |
20 | | - <MemoryRouter> |
21 | | - <VDiskInfo |
22 | | - data={{NodeId: 1, StringifiedId: '1-2-3-4-5'} as PreparedVDisk} |
23 | | - withVDiskPageLink |
24 | | - /> |
25 | | - </MemoryRouter>, |
26 | | - ); |
| 43 | + renderWithWhoami({ |
| 44 | + data: {NodeId: 1, StringifiedId: '1-2-3-4-5'} as PreparedVDisk, |
| 45 | + withVDiskPageLink: true, |
| 46 | + }); |
27 | 47 |
|
28 | 48 | const link = screen.getByRole('link', {name: /VDisk page/}); |
29 | 49 |
|
30 | | - expect(link).toHaveAttribute('href', '/vdisk-page'); |
| 50 | + expect(link).toHaveAttribute('href', '/vDisk?nodeId=1&vDiskId=1-2-3-4-5'); |
31 | 51 | expect(link).not.toHaveAttribute('target', '_blank'); |
32 | 52 | }); |
33 | | - test('renders PDisk id as an internal link when PDiskId and NodeId are defined', () => { |
34 | | - render( |
35 | | - <MemoryRouter> |
36 | | - <VDiskInfo data={{NodeId: 1, PDiskId: 2} as PreparedVDisk} /> |
37 | | - </MemoryRouter>, |
38 | | - ); |
39 | 53 |
|
40 | | - const link = screen.getByRole('link', {name: '2'}); |
| 54 | + test('renders PDisk id as an internal link when whoami allows viewer access', async () => { |
| 55 | + const {whoami} = renderWithWhoami({ |
| 56 | + data: {NodeId: 1, PDiskId: 2} as PreparedVDisk, |
| 57 | + isViewerAllowed: true, |
| 58 | + }); |
| 59 | + |
| 60 | + const link = await screen.findByRole('link', {name: '2'}); |
| 61 | + |
| 62 | + expect(whoami).toHaveBeenCalledWith({database: undefined}); |
| 63 | + expect(link).toHaveAttribute('href', '/pDisk?nodeId=1&pDiskId=2'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('renders PDisk id as plain text when whoami denies viewer access', async () => { |
| 67 | + const {whoami} = renderWithWhoami({ |
| 68 | + data: {NodeId: 1, PDiskId: 2} as PreparedVDisk, |
| 69 | + isViewerAllowed: false, |
| 70 | + }); |
| 71 | + |
| 72 | + await waitFor(() => expect(whoami).toHaveBeenCalledWith({database: undefined})); |
41 | 73 |
|
42 | | - expect(link).toHaveAttribute('href', '/pdisk-page?nodeId=1&pDiskId=2'); |
| 74 | + expect(screen.getByText('2')).toBeVisible(); |
| 75 | + expect(screen.queryByRole('link', {name: '2'})).not.toBeInTheDocument(); |
43 | 76 | }); |
44 | 77 | }); |
0 commit comments