|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | + |
| 4 | +import DeploymentNetworkConsoleButton from '../src/console/pages/components/DeploymentNetworkConsoleButton'; |
| 5 | + |
| 6 | +const mockUseK8sWatchResource = vi.hoisted(() => vi.fn()); |
| 7 | +const mockUseMutationImpl = vi.hoisted(() => vi.fn()); |
| 8 | +const mockRESTApiCreateDeployment = vi.hoisted(() => vi.fn()); |
| 9 | +const mockRESTApiDeleteDeployment = vi.hoisted(() => vi.fn()); |
| 10 | +const mockNamespaceManagerGetNamespace = vi.hoisted(() => vi.fn()); |
| 11 | + |
| 12 | +vi.mock('@openshift-console/dynamic-plugin-sdk', () => ({ |
| 13 | + useK8sWatchResource: mockUseK8sWatchResource |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../src/console/API/REST.api', () => ({ |
| 17 | + RESTApi: { |
| 18 | + createDeployment: mockRESTApiCreateDeployment, |
| 19 | + deleteDeployment: mockRESTApiDeleteDeployment |
| 20 | + } |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('../src/console/config/db', () => ({ |
| 24 | + NamespaceManager: { |
| 25 | + getNamespace: mockNamespaceManagerGetNamespace |
| 26 | + } |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock('@tanstack/react-query', () => ({ |
| 30 | + useMutation: mockUseMutationImpl |
| 31 | +})); |
| 32 | + |
| 33 | +const defaultMutationMock = { |
| 34 | + mutate: vi.fn(), |
| 35 | + isLoading: false, |
| 36 | + isError: false, |
| 37 | + error: null |
| 38 | +}; |
| 39 | + |
| 40 | +type ResourceOptions = { name?: string; selector?: { matchLabels?: Record<string, string> } }; |
| 41 | + |
| 42 | +const loadedResourceMock = (resourceName: string) => (options: ResourceOptions) => { |
| 43 | + if (options.name === resourceName) { |
| 44 | + return [{ spec: { host: 'example.com', port: { targetPort: '8080' } } }, false, undefined]; |
| 45 | + } |
| 46 | + if (options.selector?.matchLabels?.['app.kubernetes.io/name'] === 'network-observer') { |
| 47 | + return [{ status: { phase: 'Running' } }, false, undefined]; |
| 48 | + } |
| 49 | + |
| 50 | + return [undefined, false, undefined]; |
| 51 | +}; |
| 52 | + |
| 53 | +const BUTTON_LABELS = { |
| 54 | + deploy: 'Deploy the Network Console', |
| 55 | + open: 'Open the Network Console', |
| 56 | + delete: 'Delete the Network Console' |
| 57 | +}; |
| 58 | + |
| 59 | +const setupMocks = (namespace = 'test-namespace') => { |
| 60 | + vi.clearAllMocks(); |
| 61 | + mockNamespaceManagerGetNamespace.mockReturnValue(namespace); |
| 62 | +}; |
| 63 | + |
| 64 | +describe('DeploymentNetworkConsoleButton', () => { |
| 65 | + beforeEach(() => setupMocks()); |
| 66 | + |
| 67 | + it('renders the deploy button when not loaded', () => { |
| 68 | + mockUseK8sWatchResource.mockReturnValue([undefined, false, undefined]); |
| 69 | + mockUseMutationImpl.mockReturnValue(defaultMutationMock); |
| 70 | + |
| 71 | + render(<DeploymentNetworkConsoleButton />); |
| 72 | + expect(screen.getByText(BUTTON_LABELS.deploy)).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('renders the open and delete buttons when loaded', () => { |
| 76 | + mockUseK8sWatchResource.mockImplementation(loadedResourceMock('network-observer')); |
| 77 | + mockUseMutationImpl.mockReturnValue(defaultMutationMock); |
| 78 | + |
| 79 | + render(<DeploymentNetworkConsoleButton />); |
| 80 | + expect(screen.getByText(BUTTON_LABELS.open)).toBeInTheDocument(); |
| 81 | + expect(screen.getByText(BUTTON_LABELS.delete)).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('calls the create mutation when the deploy button is clicked', () => { |
| 85 | + const mutateMock = vi.fn(); |
| 86 | + mockUseK8sWatchResource.mockReturnValue([undefined, false, undefined]); |
| 87 | + mockUseMutationImpl.mockReturnValue({ ...defaultMutationMock, mutate: mutateMock }); |
| 88 | + |
| 89 | + render(<DeploymentNetworkConsoleButton />); |
| 90 | + fireEvent.click(screen.getByText(BUTTON_LABELS.deploy)); |
| 91 | + expect(mutateMock).toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('calls the delete mutation when the delete button is clicked and calls onSuccess', () => { |
| 95 | + const mutateMock = vi.fn(); |
| 96 | + let onSuccessCallback: (() => void) | undefined; |
| 97 | + |
| 98 | + mockUseK8sWatchResource.mockImplementation(loadedResourceMock('network-observer')); |
| 99 | + mockUseMutationImpl.mockImplementation(({ onSuccess }) => { |
| 100 | + onSuccessCallback = onSuccess; |
| 101 | + |
| 102 | + return { ...defaultMutationMock, mutate: mutateMock }; |
| 103 | + }); |
| 104 | + |
| 105 | + render(<DeploymentNetworkConsoleButton />); |
| 106 | + fireEvent.click(screen.getByText(BUTTON_LABELS.delete)); |
| 107 | + expect(mutateMock).toHaveBeenCalled(); |
| 108 | + |
| 109 | + onSuccessCallback?.(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('updates URL when route data changes', () => { |
| 113 | + mockUseK8sWatchResource.mockImplementation(loadedResourceMock('network-observer')); |
| 114 | + mockUseMutationImpl.mockReturnValue(defaultMutationMock); |
| 115 | + |
| 116 | + render(<DeploymentNetworkConsoleButton />); |
| 117 | + expect(screen.getByText(BUTTON_LABELS.open)).toBeInTheDocument(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments