|
| 1 | +import React from 'react'; |
| 2 | +import { renderWithRedux, patientlyWaitFor, fireEvent } from 'react-testing-lib-wrapper'; |
| 3 | +import { nockInstance, assertNockRequest } from '../../../../test-utils/nockWrapper'; |
| 4 | +import FlatpakRemotesForm from '../FlatpakRemoteform'; |
| 5 | +import api from '../../../../services/api'; |
| 6 | + |
| 7 | +const mockFn = jest.fn(); |
| 8 | +delete window.location; |
| 9 | +window.location = { assign: mockFn }; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + mockFn.mockClear(); |
| 13 | +}); |
| 14 | + |
| 15 | +const createFlatpakPath = api.getApiUrl('/flatpak_remotes'); |
| 16 | +const updateFlatpakPath = id => api.getApiUrl(`/flatpak_remotes/${id}`); |
| 17 | + |
| 18 | +const mockRemoteData = { |
| 19 | + id: 1, |
| 20 | + name: 'Test Remote', |
| 21 | + url: 'https://example.com', |
| 22 | + username: 'testuser', |
| 23 | + upstream_password_exists: true, |
| 24 | +}; |
| 25 | + |
| 26 | +test('Renders FlatpakRemotesForm fields correctly', () => { |
| 27 | + const { getByText } = renderWithRedux(<FlatpakRemotesForm setModalOpen={mockFn} />); |
| 28 | + expect(getByText('Name')).toBeInTheDocument(); |
| 29 | + expect(getByText('URL')).toBeInTheDocument(); |
| 30 | + expect(getByText('Username')).toBeInTheDocument(); |
| 31 | + expect(getByText('Password')).toBeInTheDocument(); |
| 32 | +}); |
| 33 | + |
| 34 | +test('Can save Flatpak remote from form', async (done) => { |
| 35 | + const createScope = nockInstance |
| 36 | + .post(createFlatpakPath, { |
| 37 | + organization_id: 1, |
| 38 | + name: 'New Remote', |
| 39 | + url: 'https://example.com', |
| 40 | + username: 'testuser', |
| 41 | + token: 'password123', |
| 42 | + }) |
| 43 | + .reply(201, { id: 2 }); |
| 44 | + |
| 45 | + const { getByLabelText, getByText } = renderWithRedux(<FlatpakRemotesForm |
| 46 | + setModalOpen={mockFn} |
| 47 | + />); |
| 48 | + fireEvent.change(getByLabelText('input_name'), { target: { value: 'New Remote' } }); |
| 49 | + fireEvent.change(getByLabelText('input_url'), { target: { value: 'https://example.com' } }); |
| 50 | + fireEvent.change(getByLabelText('input_username'), { target: { value: 'testuser' } }); |
| 51 | + fireEvent.change(getByLabelText('input_password'), { target: { value: 'password123' } }); |
| 52 | + |
| 53 | + getByText('Create').click(); |
| 54 | + |
| 55 | + await patientlyWaitFor(() => { |
| 56 | + expect(window.location.assign).toHaveBeenCalledWith('/flatpak_remotes/2'); |
| 57 | + }); |
| 58 | + |
| 59 | + assertNockRequest(createScope); |
| 60 | + done(); |
| 61 | +}); |
| 62 | + |
| 63 | +test('Can update Flatpak remote from form', async (done) => { |
| 64 | + const updateScope = nockInstance |
| 65 | + .put(updateFlatpakPath(mockRemoteData.id), { |
| 66 | + name: 'Updated Remote', |
| 67 | + url: 'https://updated.com', |
| 68 | + username: 'updateduser', |
| 69 | + }) |
| 70 | + .reply(200); |
| 71 | + |
| 72 | + const { getByLabelText, getByText } = renderWithRedux(<FlatpakRemotesForm |
| 73 | + setModalOpen={mockFn} |
| 74 | + remoteData={mockRemoteData} |
| 75 | + />); |
| 76 | + |
| 77 | + fireEvent.change(getByLabelText('input_name'), { target: { value: 'Updated Remote' } }); |
| 78 | + fireEvent.change(getByLabelText('input_url'), { target: { value: 'https://updated.com' } }); |
| 79 | + fireEvent.change(getByLabelText('input_username'), { target: { value: 'updateduser' } }); |
| 80 | + |
| 81 | + getByText('Update').click(); |
| 82 | + |
| 83 | + await patientlyWaitFor(() => { |
| 84 | + expect(window.location.assign).toHaveBeenCalledWith('/flatpak_remotes/1'); |
| 85 | + }); |
| 86 | + |
| 87 | + assertNockRequest(updateScope); |
| 88 | + done(); |
| 89 | +}); |
| 90 | + |
| 91 | +test('Can update Flatpak remote password from placeholder', async (done) => { |
| 92 | + const updateScope = nockInstance |
| 93 | + .put(updateFlatpakPath(mockRemoteData.id), { |
| 94 | + name: mockRemoteData.name, |
| 95 | + url: mockRemoteData.url, |
| 96 | + username: mockRemoteData.username, |
| 97 | + token: 'newpassword123', |
| 98 | + }) |
| 99 | + .reply(200); |
| 100 | + |
| 101 | + const { getByLabelText, getByText } = renderWithRedux(<FlatpakRemotesForm |
| 102 | + setModalOpen={mockFn} |
| 103 | + remoteData={mockRemoteData} |
| 104 | + />); |
| 105 | + |
| 106 | + // Simulate changing the password from the placeholder to a new value |
| 107 | + fireEvent.change(getByLabelText('input_password'), { target: { value: 'newpassword123' } }); |
| 108 | + |
| 109 | + getByText('Update').click(); |
| 110 | + |
| 111 | + await patientlyWaitFor(() => { |
| 112 | + expect(window.location.assign).toHaveBeenCalledWith('/flatpak_remotes/1'); |
| 113 | + }); |
| 114 | + |
| 115 | + assertNockRequest(updateScope); |
| 116 | + done(); |
| 117 | +}); |
0 commit comments