|
| 1 | +import React from 'react'; |
| 2 | +import {render, screen, fireEvent, waitFor, within} from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { I18nextProvider } from 'react-i18next'; |
| 5 | +import i18n from '../../../../i18n'; |
| 6 | +import ImageBuildDialog from '../ImageBuildDialog'; |
| 7 | +import { BuildImage } from '../../../../model'; |
| 8 | +import {act} from "react-dom/test-utils"; |
| 9 | + |
| 10 | +jest.mock('../../../../model', () => ({ |
| 11 | + BuildImage: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | + |
| 15 | +const mockUseState = jest.fn() |
| 16 | +const mockSetState = jest.fn() |
| 17 | +const mockGetState = jest.fn() |
| 18 | +const mockClearState = jest.fn() |
| 19 | + |
| 20 | + |
| 21 | +jest.mock('../../../../store', () => ({ |
| 22 | + ...(jest.requireActual('../../../../store') as any), |
| 23 | + setState: (...args: unknown[]) => mockSetState(...args), |
| 24 | + useState: (...args: unknown[]) => mockUseState(...args), |
| 25 | + getState: (...args: unknown[]) => mockGetState(...args), |
| 26 | + clearState: (...args: unknown[]) => mockClearState(...args), |
| 27 | + |
| 28 | +})) |
| 29 | + |
| 30 | + |
| 31 | +jest.mock('../../../../components/FileChooser', () => ({ |
| 32 | + __esModule: true, |
| 33 | + default: () => <button>Upload File</button>, |
| 34 | +})); |
| 35 | + |
| 36 | +describe('ImageBuildDialog', () => { |
| 37 | + beforeEach(() => { |
| 38 | + mockUseState.mockReturnValue(['3.12.0', '3.6.0', '3.7.0']) |
| 39 | + |
| 40 | + }); |
| 41 | + |
| 42 | + it('renders correctly when open', () => { |
| 43 | + render( |
| 44 | + <I18nextProvider i18n={i18n}> |
| 45 | + <ImageBuildDialog /> |
| 46 | + </I18nextProvider> |
| 47 | + ); |
| 48 | + |
| 49 | + expect(screen.getByText('Build image')).toBeTruthy(); |
| 50 | + expect(screen.getByPlaceholderText('Enter image AMI ID')).toBeTruthy(); |
| 51 | + expect(screen.getByText('Upload File')).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('handles image ID input', async () => { |
| 55 | + render( |
| 56 | + <I18nextProvider i18n={i18n}> |
| 57 | + <ImageBuildDialog /> |
| 58 | + </I18nextProvider> |
| 59 | + ); |
| 60 | + |
| 61 | + const input = screen.getByPlaceholderText('Enter image AMI ID'); |
| 62 | + |
| 63 | + await act(async () => { |
| 64 | + fireEvent.change(input, { |
| 65 | + target: { value: 'test-image-id' }, |
| 66 | + detail: { value: 'test-image-id' }, |
| 67 | + bubbles: true, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + await waitFor(() => { |
| 72 | + expect(mockSetState).toHaveBeenCalledWith( |
| 73 | + ['app', 'customImages', 'imageBuild', 'imageId'], |
| 74 | + 'test-image-id' |
| 75 | + ); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('handles version selection', async () => { |
| 80 | + render( |
| 81 | + <I18nextProvider i18n={i18n}> |
| 82 | + <ImageBuildDialog /> |
| 83 | + </I18nextProvider> |
| 84 | + ); |
| 85 | + |
| 86 | + const versionSelect = screen.getByLabelText('Version'); |
| 87 | + await userEvent.click(versionSelect); |
| 88 | + |
| 89 | + const version = await screen.findByText('3.7.0'); |
| 90 | + await userEvent.click(version); |
| 91 | + |
| 92 | + expect(screen.getByText('3.7.0')).toBeTruthy(); |
| 93 | + |
| 94 | + await userEvent.click(versionSelect); |
| 95 | + |
| 96 | + const version2 = await screen.findByText('3.12.0'); |
| 97 | + await userEvent.click(version2); |
| 98 | + |
| 99 | + expect(screen.getByText('3.12.0')).toBeTruthy(); |
| 100 | + |
| 101 | + }); |
| 102 | + |
| 103 | + it('handles build button click', async () => { |
| 104 | + (mockUseState as jest.Mock).mockImplementation((path) => { |
| 105 | + if (path.join('.') === 'app.version.full') return ['3.12.0', '3.6.0', '3.7.0']; |
| 106 | + if (path.join('.') === 'app.customImages.imageBuild.imageId') return 'test-image-id' |
| 107 | + if (path.join('.') === 'app.customImages.imageBuild.config') return 'test-config'; |
| 108 | + |
| 109 | + return null; |
| 110 | + }); |
| 111 | + |
| 112 | + mockGetState.mockImplementation((path: string[]) => { |
| 113 | + if (Array.isArray(path)) { |
| 114 | + switch (path.join('.')) { |
| 115 | + case 'app.customImages.imageBuild.imageId': |
| 116 | + return 'test-image-id'; |
| 117 | + case 'app.customImages.imageBuild.config': |
| 118 | + return 'test-config'; |
| 119 | + } |
| 120 | + } |
| 121 | + return undefined; |
| 122 | + }); |
| 123 | + |
| 124 | + render( |
| 125 | + <I18nextProvider i18n={i18n}> |
| 126 | + <ImageBuildDialog /> |
| 127 | + </I18nextProvider> |
| 128 | + ); |
| 129 | + |
| 130 | + const buildButton = screen.getByText('Build image'); |
| 131 | + expect(buildButton).toBeTruthy(); |
| 132 | + |
| 133 | + await act(async () => { |
| 134 | + await userEvent.click(buildButton); |
| 135 | + }); |
| 136 | + |
| 137 | + await waitFor(() => { |
| 138 | + expect(BuildImage).toHaveBeenCalledWith('test-image-id', 'test-config', '3.12.0'); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('closes the dialog', async () => { |
| 143 | + render( |
| 144 | + <I18nextProvider i18n={i18n}> |
| 145 | + <ImageBuildDialog /> |
| 146 | + </I18nextProvider> |
| 147 | + ); |
| 148 | + |
| 149 | + const cancelButton = screen.getByText('Cancel'); |
| 150 | + await userEvent.click(cancelButton); |
| 151 | + |
| 152 | + expect(mockSetState).toHaveBeenCalledWith(['app', 'customImages', 'imageBuild', 'dialog'], false); |
| 153 | + expect(mockClearState).toHaveBeenCalledWith(['app', 'customImages', 'imageBuild', 'errors']); |
| 154 | + }); |
| 155 | +}); |
0 commit comments