|
| 1 | +import { screen, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import React from 'react'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | + |
| 6 | +import { mockMatchMedia, renderWithTheme } from 'src/utilities/testHelpers'; |
| 7 | + |
| 8 | +import { UpdateDelegationForm } from './UpdateDelegationForm'; |
| 9 | + |
| 10 | +import type { ChildAccountWithDelegates, User } from '@linode/api-v4'; |
| 11 | + |
| 12 | +beforeAll(() => mockMatchMedia()); |
| 13 | + |
| 14 | +const mocks = vi.hoisted(() => ({ |
| 15 | + useAccountUsersInfiniteQuery: vi.fn(), |
| 16 | + mockUseUpdateChildAccountDelegatesQuery: vi.fn(), |
| 17 | + mockMutateAsync: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('@linode/queries', async () => { |
| 21 | + const actual = await vi.importActual('@linode/queries'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + useAccountUsersInfiniteQuery: mocks.useAccountUsersInfiniteQuery, |
| 25 | + useUpdateChildAccountDelegatesQuery: |
| 26 | + mocks.mockUseUpdateChildAccountDelegatesQuery, |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +const mockUsers: User[] = [ |
| 31 | + { |
| 32 | + email: 'user1@example.com', |
| 33 | + last_login: null, |
| 34 | + password_created: null, |
| 35 | + restricted: false, |
| 36 | + ssh_keys: [], |
| 37 | + tfa_enabled: false, |
| 38 | + user_type: 'default', |
| 39 | + username: 'user1', |
| 40 | + verified_phone_number: null, |
| 41 | + }, |
| 42 | + { |
| 43 | + email: 'user2@example.com', |
| 44 | + last_login: null, |
| 45 | + password_created: null, |
| 46 | + restricted: false, |
| 47 | + ssh_keys: [], |
| 48 | + tfa_enabled: false, |
| 49 | + user_type: 'default', |
| 50 | + username: 'user2', |
| 51 | + verified_phone_number: null, |
| 52 | + }, |
| 53 | +]; |
| 54 | + |
| 55 | +const mockChildAccountWithDelegates: ChildAccountWithDelegates = { |
| 56 | + company: 'Test Company', |
| 57 | + euuid: 'E1234567-89AB-CDEF-0123-456789ABCDEF', |
| 58 | + users: ['user1'], |
| 59 | +}; |
| 60 | + |
| 61 | +const defaultProps = { |
| 62 | + delegation: mockChildAccountWithDelegates, |
| 63 | + formattedCurrentUsers: [ |
| 64 | + { label: mockUsers[0].username, value: mockUsers[0].username }, |
| 65 | + ], |
| 66 | + onClose: vi.fn(), |
| 67 | +}; |
| 68 | + |
| 69 | +describe('UpdateDelegationsDrawer', () => { |
| 70 | + beforeEach(() => { |
| 71 | + vi.clearAllMocks(); |
| 72 | + |
| 73 | + mocks.useAccountUsersInfiniteQuery.mockReturnValue({ |
| 74 | + data: { pages: [{ data: mockUsers }] }, |
| 75 | + isFetching: false, |
| 76 | + }); |
| 77 | + |
| 78 | + mocks.mockUseUpdateChildAccountDelegatesQuery.mockReturnValue({ |
| 79 | + mutateAsync: mocks.mockMutateAsync, |
| 80 | + }); |
| 81 | + |
| 82 | + mocks.mockMutateAsync.mockResolvedValue({}); |
| 83 | + }); |
| 84 | + |
| 85 | + it('renders the drawer with current delegates', () => { |
| 86 | + renderWithTheme(<UpdateDelegationForm {...defaultProps} />); |
| 87 | + |
| 88 | + const companyName = screen.getByText(/test company/i); |
| 89 | + expect(companyName).toBeInTheDocument(); |
| 90 | + const userName = screen.getByText(/user1/i); |
| 91 | + expect(userName).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('allows adding a new delegate', async () => { |
| 95 | + renderWithTheme(<UpdateDelegationForm {...defaultProps} />); |
| 96 | + |
| 97 | + const user = userEvent.setup(); |
| 98 | + |
| 99 | + const autocompleteInput = screen.getByRole('combobox'); |
| 100 | + await user.click(autocompleteInput); |
| 101 | + |
| 102 | + await waitFor(async () => { |
| 103 | + screen.getByRole('option', { name: 'user2' }); |
| 104 | + }); |
| 105 | + |
| 106 | + const user2Option = screen.getByRole('option', { name: 'user2' }); |
| 107 | + await user.click(user2Option); |
| 108 | + |
| 109 | + const submitButton = screen.getByRole('button', { name: /save changes/i }); |
| 110 | + await user.click(submitButton); |
| 111 | + |
| 112 | + await waitFor(() => { |
| 113 | + expect(mocks.mockMutateAsync).toHaveBeenCalledWith({ |
| 114 | + euuid: mockChildAccountWithDelegates.euuid, |
| 115 | + users: ['user1', 'user2'], |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('allows sending an empty payload', async () => { |
| 121 | + renderWithTheme(<UpdateDelegationForm {...defaultProps} />); |
| 122 | + |
| 123 | + const user = userEvent.setup(); |
| 124 | + |
| 125 | + // Open the autocomplete and deselect the preselected user (user1) |
| 126 | + const autocompleteInput = screen.getByRole('combobox'); |
| 127 | + await user.click(autocompleteInput); |
| 128 | + |
| 129 | + await waitFor(() => { |
| 130 | + // Ensure options are rendered |
| 131 | + expect(screen.getByRole('option', { name: 'user1' })).toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + const user1Option = screen.getByRole('option', { name: 'user1' }); |
| 135 | + await user.click(user1Option); // toggles off the selected user |
| 136 | + |
| 137 | + // Submit with no users selected |
| 138 | + const submitButton = screen.getByRole('button', { name: /save changes/i }); |
| 139 | + await user.click(submitButton); |
| 140 | + |
| 141 | + await waitFor(() => { |
| 142 | + expect(mocks.mockMutateAsync).toHaveBeenCalledWith({ |
| 143 | + euuid: mockChildAccountWithDelegates.euuid, |
| 144 | + users: [], |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments