|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/svelte'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | + |
| 5 | +// The component to be tested must be imported after the mock setup |
| 6 | +import ProfileEditor from '../../src/lib/components/v2/admin/ProfileEditor.svelte'; |
| 7 | + |
| 8 | +describe('ProfileEditor', () => { |
| 9 | + const mockedResource = /** @type {import('fractal-components/types/api').Resource} */ ({ |
| 10 | + id: 1, |
| 11 | + type: 'slurm_ssh' |
| 12 | + }); |
| 13 | + |
| 14 | + it('Edit SLURM SSH profile - success', async () => { |
| 15 | + /** |
| 16 | + * @type {(profile: any) => Promise<Response>} |
| 17 | + */ |
| 18 | + const mockSaveProfile = vi.fn( |
| 19 | + async () => |
| 20 | + /** @type {Response} */ ({ |
| 21 | + ok: true, |
| 22 | + status: 200, |
| 23 | + json: () => new Promise((resolve) => resolve({ id: 1 })) |
| 24 | + }) |
| 25 | + ); |
| 26 | + |
| 27 | + const user = userEvent.setup(); |
| 28 | + render(ProfileEditor, { |
| 29 | + props: { |
| 30 | + resource: mockedResource, |
| 31 | + profile: /** @type {import('fractal-components/types/api').Profile} */ ({ |
| 32 | + id: 1, |
| 33 | + resource_id: 1, |
| 34 | + resource_type: 'slurm_ssh' |
| 35 | + }), |
| 36 | + saveProfile: mockSaveProfile |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + await user.type(screen.getByRole('textbox', { name: 'Profile name' }), 'profile name'); |
| 41 | + await user.type(screen.getByRole('textbox', { name: 'Username' }), 'foo'); |
| 42 | + await user.type(screen.getByRole('textbox', { name: 'SSH key path' }), '/path/to/key'); |
| 43 | + await user.type(screen.getByRole('textbox', { name: 'Jobs remote dir' }), '/path/to/jobs'); |
| 44 | + await user.type(screen.getByRole('textbox', { name: 'Tasks remote dir' }), '/path/to/tasks'); |
| 45 | + |
| 46 | + await user.click(screen.getByRole('button', { name: 'Save' })); |
| 47 | + |
| 48 | + await screen.findByText('Profile successfully updated'); |
| 49 | + |
| 50 | + expect(mockSaveProfile).toHaveBeenCalledWith({ |
| 51 | + id: 1, |
| 52 | + name: 'profile name', |
| 53 | + username: 'foo', |
| 54 | + ssh_key_path: '/path/to/key', |
| 55 | + jobs_remote_dir: '/path/to/jobs', |
| 56 | + tasks_remote_dir: '/path/to/tasks', |
| 57 | + resource_id: 1, |
| 58 | + resource_type: 'slurm_ssh' |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('Edit SLURM SSH profile - validation error', async () => { |
| 63 | + /** |
| 64 | + * @type {(profile: any) => Promise<Response>} |
| 65 | + */ |
| 66 | + const mockSaveProfile = vi.fn( |
| 67 | + async () => |
| 68 | + /** @type {Response} */ ({ |
| 69 | + ok: false, |
| 70 | + status: 422, |
| 71 | + json: () => |
| 72 | + new Promise((resolve) => |
| 73 | + resolve({ |
| 74 | + detail: [ |
| 75 | + { |
| 76 | + loc: ['body', 'slurm_ssh', 'name'], |
| 77 | + msg: 'mocked_error_name', |
| 78 | + type: 'value_error' |
| 79 | + }, |
| 80 | + { |
| 81 | + loc: ['body', 'slurm_ssh', 'username'], |
| 82 | + msg: 'mocked_error_username', |
| 83 | + type: 'value_error' |
| 84 | + }, |
| 85 | + { |
| 86 | + loc: ['body', 'slurm_ssh', 'ssh_key_path'], |
| 87 | + msg: 'mocked_error_ssh_key_path', |
| 88 | + type: 'value_error' |
| 89 | + }, |
| 90 | + { |
| 91 | + loc: ['body', 'slurm_ssh', 'jobs_remote_dir'], |
| 92 | + msg: 'mocked_error_jobs_remote_dir', |
| 93 | + type: 'value_error' |
| 94 | + }, |
| 95 | + { |
| 96 | + loc: ['body', 'slurm_ssh', 'tasks_remote_dir'], |
| 97 | + msg: 'mocked_error_tasks_remote_dir', |
| 98 | + type: 'value_error' |
| 99 | + } |
| 100 | + ] |
| 101 | + }) |
| 102 | + ) |
| 103 | + }) |
| 104 | + ); |
| 105 | + |
| 106 | + const user = userEvent.setup(); |
| 107 | + render(ProfileEditor, { |
| 108 | + props: { |
| 109 | + resource: mockedResource, |
| 110 | + profile: { |
| 111 | + id: 1, |
| 112 | + resource_id: 1, |
| 113 | + name: 'profile name', |
| 114 | + resource_type: 'slurm_ssh', |
| 115 | + username: 'foo', |
| 116 | + ssh_key_path: '/path/to/key', |
| 117 | + jobs_remote_dir: '/path/to/jobs', |
| 118 | + tasks_remote_dir: '/path/to/tasks' |
| 119 | + }, |
| 120 | + saveProfile: mockSaveProfile |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + await user.clear(screen.getByRole('textbox', { name: 'Profile name' })); |
| 125 | + await user.clear(screen.getByRole('textbox', { name: 'Username' })); |
| 126 | + await user.clear(screen.getByRole('textbox', { name: 'SSH key path' })); |
| 127 | + await user.clear(screen.getByRole('textbox', { name: 'Jobs remote dir' })); |
| 128 | + await user.clear(screen.getByRole('textbox', { name: 'Tasks remote dir' })); |
| 129 | + |
| 130 | + await user.click(screen.getByRole('button', { name: 'Save' })); |
| 131 | + |
| 132 | + await screen.findByText('mocked_error_name'); |
| 133 | + await screen.findByText('mocked_error_username'); |
| 134 | + await screen.findByText('mocked_error_ssh_key_path'); |
| 135 | + await screen.findByText('mocked_error_jobs_remote_dir'); |
| 136 | + await screen.findByText('mocked_error_tasks_remote_dir'); |
| 137 | + |
| 138 | + expect(mockSaveProfile).toHaveBeenCalledWith({ |
| 139 | + id: 1, |
| 140 | + name: '', |
| 141 | + username: '', |
| 142 | + ssh_key_path: '', |
| 143 | + jobs_remote_dir: '', |
| 144 | + tasks_remote_dir: '', |
| 145 | + resource_id: 1, |
| 146 | + resource_type: 'slurm_ssh' |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments