From 8129cb2f38f5334a4b877867f517d952c9b3376b Mon Sep 17 00:00:00 2001 From: Aleksander Nicacio da Silva Date: Tue, 20 Aug 2024 10:38:27 -0300 Subject: [PATCH] test: added VoIP unit tests for AssignExtensionModal --- .../users/voip/AssignExtensionModal.spec.tsx | 121 ++++++++++++++++++ .../admin/users/voip/AssignExtensionModal.tsx | 11 +- 2 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 apps/meteor/client/views/admin/users/voip/AssignExtensionModal.spec.tsx diff --git a/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.spec.tsx b/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.spec.tsx new file mode 100644 index 0000000000000..f4d4d8cb24475 --- /dev/null +++ b/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.spec.tsx @@ -0,0 +1,121 @@ +import { mockAppRoot } from '@rocket.chat/mock-providers'; +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import React from 'react'; + +import '@testing-library/jest-dom'; +import AssignExtensionModal from './AssignExtensionModal'; + +const root = mockAppRoot() + .withJohnDoe() + .withEndpoint('POST', '/v1/voip-freeswitch.extension.assign', () => null) + .withEndpoint('GET', '/v1/voip-freeswitch.extension.list', () => ({ + extensions: [ + { + extension: '1000', + context: 'default', + domain: '172.31.38.45', + groups: ['default', 'sales'], + status: 'UNREGISTERED' as const, + contact: 'error/user_not_registered', + callGroup: 'techsupport', + callerName: 'Extension 1000', + callerNumber: '1000', + }, + ], + success: true, + })) + .withEndpoint('GET', '/v1/users.autocomplete', () => ({ + items: [ + { + _id: 'janedoe', + score: 2, + name: 'Jane Doe', + username: 'jane.doe', + nickname: null, + status: 'offline', + statusText: '', + avatarETag: null, + } as any, + ], + success: true, + })); + +// TODO: it('should load with default user', async () => {}); + +// TODO: it('should load with default extension', async () => {}); + +it('should only enable "Free Extension Numbers" field if username is informed', async () => { + render( undefined} />, { + wrapper: root.build(), + }); + + expect(screen.getByTestId('input-free-extension-numbers')).toHaveClass('disabled'); + expect(screen.getByLabelText('User_Without_Extensions')).toBeEnabled(); + + screen.getByLabelText('User_Without_Extensions').focus(); + const userOption = await screen.findByRole('option', { name: 'Jane Doe' }); + userEvent.click(userOption); + + await waitFor(() => expect(screen.getByTestId('input-free-extension-numbers')).not.toHaveClass('disabled')); +}); + +it('should only enable "Associate" button both username and extension is informed', async () => { + render( undefined} />, { + wrapper: root.build(), + }); + + expect(screen.getByRole('button', { name: /Associate/i, hidden: true })).toBeDisabled(); + + screen.getByLabelText('User_Without_Extensions').focus(); + const userOption = await screen.findByRole('option', { name: 'Jane Doe' }); + userEvent.click(userOption); + + await waitFor(() => expect(screen.getByTestId('input-free-extension-numbers')).not.toHaveClass('disabled')); + + screen.getByTestId('input-free-extension-numbers').click(); + const extOption = await screen.findByRole('option', { name: '1000' }); + userEvent.click(extOption); + + expect(screen.getByRole('button', { name: /Associate/i, hidden: true })).toBeEnabled(); +}); + +it('should call onClose when extension is associated', async () => { + const closeFn = jest.fn(); + render(, { + wrapper: root.build(), + }); + + screen.getByLabelText('User_Without_Extensions').focus(); + const userOption = await screen.findByRole('option', { name: 'Jane Doe' }); + userEvent.click(userOption); + + await waitFor(() => expect(screen.getByTestId('input-free-extension-numbers')).not.toHaveClass('disabled')); + + screen.getByTestId('input-free-extension-numbers').click(); + const extOption = await screen.findByRole('option', { name: '1000' }); + userEvent.click(extOption); + + screen.getByRole('button', { name: /Associate/i, hidden: true }).click(); + await waitFor(() => expect(closeFn).toHaveBeenCalled()); +}); + +it('should call onClose when cancel button is clicked', () => { + const closeFn = jest.fn(); + render(, { + wrapper: root.build(), + }); + + screen.getByRole('button', { name: /Cancel/i, hidden: true }).click(); + expect(closeFn).toHaveBeenCalled(); +}); + +it('should call onClose when cancel button is clicked', () => { + const closeFn = jest.fn(); + render(, { + wrapper: root.build(), + }); + + screen.getByRole('button', { name: /Close/i, hidden: true }).click(); + expect(closeFn).toHaveBeenCalled(); +}); diff --git a/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.tsx b/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.tsx index d9877977515ea..e89d0cac1fced 100644 --- a/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.tsx +++ b/apps/meteor/client/views/admin/users/voip/AssignExtensionModal.tsx @@ -29,6 +29,7 @@ const AssignExtensionModal = ({ defaultExtension, defaultUsername, onClose }: As const assignUser = useEndpoint('POST', '/v1/voip-freeswitch.extension.assign'); const getAvailableExtensions = useEndpoint('GET', '/v1/voip-freeswitch.extension.list'); + const modalTitleId = useUniqueId(); const usersWithoutExtensionsId = useUniqueId(); const freeExtensionNumberId = useUniqueId(); @@ -78,10 +79,13 @@ const AssignExtensionModal = ({ defaultExtension, defaultUsername, onClose }: As }); return ( - handleAssignment.mutateAsync(data))} {...props} />}> + handleAssignment.mutateAsync(data))} {...props} />} + > - {t('Associate_User_to_Extension')} - + {t('Associate_User_to_Extension')} + @@ -123,6 +127,7 @@ const AssignExtensionModal = ({ defaultExtension, defaultUsername, onClose }: As placeholder={t('Select_an_option')} value={field.value} onChange={field.onChange} + data-testid='input-free-extension-numbers' /> )} />