|
| 1 | +import { act, cleanup } from '@testing-library/react'; |
| 2 | +import { superRenderHook } from '@actiontech/shared/lib/testUtil/superRender'; |
| 3 | +import { useDispatch, useSelector } from 'react-redux'; |
| 4 | +import { |
| 5 | + baseMockApi, |
| 6 | + createSpySuccessResponse |
| 7 | +} from '@actiontech/shared/lib/testUtil/mockApi'; |
| 8 | +import useNavigateToWorkbench from '.'; |
| 9 | +import { mockUseRecentlySelectedZone } from '../../testUtils/mockHooks/mockUseRecentlySelectedZone'; |
| 10 | +import { mockGatewayTipsData } from '@actiontech/shared/lib/testUtil/mockApi/base/gateway/data'; |
| 11 | + |
| 12 | +jest.mock('react-redux', () => ({ |
| 13 | + ...jest.requireActual('react-redux'), |
| 14 | + useDispatch: jest.fn(), |
| 15 | + useSelector: jest.fn() |
| 16 | +})); |
| 17 | + |
| 18 | +describe('useNavigateToWorkbench', () => { |
| 19 | + const dispatchSpy = jest.fn(); |
| 20 | + let getGatewayTipsSpy: jest.SpyInstance; |
| 21 | + let getSQLQueryConfigurationSpy: jest.SpyInstance; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + (useDispatch as jest.Mock).mockImplementation(() => dispatchSpy); |
| 25 | + (useSelector as jest.Mock).mockImplementation((selector) => { |
| 26 | + return selector({ |
| 27 | + availabilityZone: { |
| 28 | + availabilityZoneTips: mockGatewayTipsData |
| 29 | + } |
| 30 | + }); |
| 31 | + }); |
| 32 | + mockUseRecentlySelectedZone(); |
| 33 | + getGatewayTipsSpy = baseMockApi.gateway.getGatewayTips(); |
| 34 | + getSQLQueryConfigurationSpy = baseMockApi.cloudBeaver.getSqlQueryUrl(); |
| 35 | + jest.useFakeTimers(); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + jest.useRealTimers(); |
| 40 | + jest.clearAllMocks(); |
| 41 | + cleanup(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should initialize with correct default values', () => { |
| 45 | + const { result } = superRenderHook(() => useNavigateToWorkbench()); |
| 46 | + |
| 47 | + expect(result.current.navigateToWorkbenchLoading).toBeFalsy(); |
| 48 | + expect(result.current.getAvailabilityZoneTipsLoading).toBeFalsy(); |
| 49 | + expect(typeof result.current.navigateToWorkbenchAsync).toBe('function'); |
| 50 | + expect(typeof result.current.getAvailabilityZoneTipsAsync).toBe('function'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should fetch availability zone tips successfully', async () => { |
| 54 | + const { result } = superRenderHook(() => useNavigateToWorkbench()); |
| 55 | + expect(result.current.getAvailabilityZoneTipsLoading).toBeFalsy(); |
| 56 | + |
| 57 | + act(() => { |
| 58 | + result.current.getAvailabilityZoneTipsAsync(); |
| 59 | + }); |
| 60 | + expect(result.current.getAvailabilityZoneTipsLoading).toBeTruthy(); |
| 61 | + await act(async () => jest.advanceTimersByTime(3000)); |
| 62 | + expect(result.current.getAvailabilityZoneTipsLoading).toBeFalsy(); |
| 63 | + |
| 64 | + expect(getGatewayTipsSpy).toHaveBeenCalledTimes(1); |
| 65 | + expect(dispatchSpy).toHaveBeenCalledWith({ |
| 66 | + type: 'availabilityZone/updateAvailabilityZoneTips', |
| 67 | + payload: { availabilityZoneTips: mockGatewayTipsData } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should fetch SQL query configuration successfully', async () => { |
| 72 | + const mockUpdateRecentlySelectedZone = jest.fn(); |
| 73 | + getSQLQueryConfigurationSpy.mockImplementation(() => |
| 74 | + createSpySuccessResponse({ |
| 75 | + data: { |
| 76 | + enable_sql_query: true, |
| 77 | + sql_query_root_uri: '/cloudbeaver' |
| 78 | + } |
| 79 | + }) |
| 80 | + ); |
| 81 | + mockUseRecentlySelectedZone({ |
| 82 | + availabilityZone: undefined, |
| 83 | + updateRecentlySelectedZone: mockUpdateRecentlySelectedZone |
| 84 | + }); |
| 85 | + const { result } = superRenderHook(() => useNavigateToWorkbench()); |
| 86 | + expect(result.current.navigateToWorkbenchLoading).toBeFalsy(); |
| 87 | + |
| 88 | + act(() => { |
| 89 | + result.current.navigateToWorkbenchAsync(); |
| 90 | + }); |
| 91 | + expect(result.current.navigateToWorkbenchLoading).toBeTruthy(); |
| 92 | + await act(async () => jest.advanceTimersByTime(3000)); |
| 93 | + expect(result.current.navigateToWorkbenchLoading).toBeFalsy(); |
| 94 | + |
| 95 | + expect(getSQLQueryConfigurationSpy).toHaveBeenCalledTimes(1); |
| 96 | + expect(mockUpdateRecentlySelectedZone).toHaveBeenCalledWith( |
| 97 | + mockGatewayTipsData[0] |
| 98 | + ); |
| 99 | + }); |
| 100 | +}); |
0 commit comments