|
| 1 | +import { screen, fireEvent, waitFor } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import { useComponents } from '../../../hooks/useComponents'; |
| 4 | +import { k8sPatchResource } from '../../../k8s/k8s-fetch'; |
| 5 | +import { formikRenderer } from '../../../utils/test-utils'; |
| 6 | +import { EditContextsModal } from '../EditContextsModal'; |
| 7 | +import { IntegrationTestFormValues } from '../IntegrationTestForm/types'; |
| 8 | +import { MockIntegrationTests } from '../IntegrationTestsListView/__data__/mock-integration-tests'; |
| 9 | +import { contextOptions } from '../utils'; |
| 10 | + |
| 11 | +// Mock external dependencies |
| 12 | +jest.mock('../../../k8s/k8s-fetch', () => ({ |
| 13 | + k8sPatchResource: jest.fn(), |
| 14 | +})); |
| 15 | +jest.mock('../../../hooks/useComponents', () => ({ |
| 16 | + useComponents: jest.fn(), |
| 17 | +})); |
| 18 | +jest.mock('../../Workspace/useWorkspaceInfo', () => ({ |
| 19 | + useWorkspaceInfo: jest.fn(() => ({ namespace: 'test-ns', workspace: 'test-ws' })), |
| 20 | +})); |
| 21 | + |
| 22 | +const useComponentsMock = useComponents as jest.Mock; |
| 23 | +const patchResourceMock = k8sPatchResource as jest.Mock; |
| 24 | +const onCloseMock = jest.fn(); |
| 25 | + |
| 26 | +const intTest = MockIntegrationTests[0]; |
| 27 | +const initialValues: IntegrationTestFormValues = { |
| 28 | + name: intTest.metadata.name, |
| 29 | + url: 'test-url', |
| 30 | + optional: true, |
| 31 | + contexts: intTest.spec.contexts, |
| 32 | +}; |
| 33 | + |
| 34 | +const setup = () => |
| 35 | + formikRenderer(<EditContextsModal intTest={intTest} onClose={onCloseMock} />, initialValues); |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + useComponentsMock.mockReturnValue([[], true]); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('EditContextsModal', () => { |
| 43 | + it('should render correct contexts', () => { |
| 44 | + setup(); |
| 45 | + const contextOptionNames = contextOptions.map((ctx) => ctx.name); |
| 46 | + |
| 47 | + screen.getByText('Contexts'); |
| 48 | + contextOptionNames.forEach((ctxName) => screen.queryByText(ctxName)); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should show Save and Cancel buttons', () => { |
| 52 | + setup(); |
| 53 | + // Save |
| 54 | + screen.getByTestId('update-contexts'); |
| 55 | + // Cancel |
| 56 | + screen.getByTestId('cancel-update-contexts'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should call onClose callback when cancel button is clicked', () => { |
| 60 | + setup(); |
| 61 | + fireEvent.click(screen.getByRole('button', { name: 'Cancel' })); |
| 62 | + expect(onCloseMock).toHaveBeenCalledWith(null, { submitClicked: false }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('prevents form submission when pressing Enter', () => { |
| 66 | + setup(); |
| 67 | + const form = screen.getByTestId('edit-contexts-modal'); |
| 68 | + fireEvent.keyDown(form, { key: 'Enter', code: 'Enter' }); |
| 69 | + expect(k8sPatchResource).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('calls updateIntegrationTest and onClose on form submission', async () => { |
| 73 | + patchResourceMock.mockResolvedValue({}); |
| 74 | + |
| 75 | + setup(); |
| 76 | + const clearButton = screen.getByTestId('clear-button'); |
| 77 | + // Clear all selections |
| 78 | + fireEvent.click(clearButton); |
| 79 | + // Save button should now be active |
| 80 | + fireEvent.click(screen.getByRole('button', { name: 'Save' })); |
| 81 | + |
| 82 | + await waitFor(() => { |
| 83 | + expect(patchResourceMock).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + expect(patchResourceMock).toHaveBeenCalledWith( |
| 87 | + expect.objectContaining({ |
| 88 | + queryOptions: { name: 'test-app-test-1', ns: 'test-namespace' }, |
| 89 | + patches: [{ op: 'replace', path: '/spec/contexts', value: null }], |
| 90 | + }), |
| 91 | + ); |
| 92 | + expect(onCloseMock).toHaveBeenCalledWith(null, { submitClicked: true }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('displays an error message if k8sPatchResource fails', async () => { |
| 96 | + patchResourceMock.mockRejectedValue('Failed to update contexts'); |
| 97 | + setup(); |
| 98 | + |
| 99 | + const clearButton = screen.getByTestId('clear-button'); |
| 100 | + // Clear all selections |
| 101 | + fireEvent.click(clearButton); |
| 102 | + // Click Save button |
| 103 | + fireEvent.click(screen.getByRole('button', { name: 'Save' })); |
| 104 | + |
| 105 | + // wait for the error message to appear |
| 106 | + await waitFor(() => { |
| 107 | + expect(patchResourceMock).toHaveBeenCalledTimes(1); |
| 108 | + expect(screen.getByText('An error occurred')).toBeInTheDocument(); |
| 109 | + expect(screen.queryByText('Failed to update contexts')).toBeInTheDocument(); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments