|
| 1 | +import { MockedServerContext } from '@rocket.chat/mock-providers'; |
| 2 | +import type { MultiUsersSelectElement as MultiUsersSelectElementType } from '@rocket.chat/ui-kit'; |
| 3 | +import { BlockContext } from '@rocket.chat/ui-kit'; |
| 4 | +import { render, screen } from '@testing-library/react'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | + |
| 7 | +import { contextualBarParser } from '../../surfaces'; |
| 8 | +import MultiUsersSelectElement from './MultiUsersSelectElement'; |
| 9 | +import { useUsersData } from './hooks/useUsersData'; |
| 10 | + |
| 11 | +const usersBlock: MultiUsersSelectElementType = { |
| 12 | + type: 'multi_users_select', |
| 13 | + appId: 'test', |
| 14 | + blockId: 'test', |
| 15 | + actionId: 'test', |
| 16 | +}; |
| 17 | + |
| 18 | +jest.mock('./hooks/useUsersData'); |
| 19 | + |
| 20 | +const mockedOptions = [ |
| 21 | + { |
| 22 | + value: 'user1_id', |
| 23 | + label: 'User 1', |
| 24 | + }, |
| 25 | + { |
| 26 | + value: 'user2_id', |
| 27 | + label: 'User 2', |
| 28 | + }, |
| 29 | + { |
| 30 | + value: 'user3_id', |
| 31 | + label: 'User 3', |
| 32 | + }, |
| 33 | +]; |
| 34 | + |
| 35 | +const mockUseUsersData = jest.mocked(useUsersData); |
| 36 | +mockUseUsersData.mockReturnValue(mockedOptions); |
| 37 | + |
| 38 | +describe('UiKit MultiUsersSelect Element', () => { |
| 39 | + beforeAll(() => { |
| 40 | + jest.useFakeTimers(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterAll(() => { |
| 44 | + jest.useRealTimers(); |
| 45 | + }); |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + render( |
| 49 | + <MockedServerContext> |
| 50 | + <MultiUsersSelectElement |
| 51 | + index={0} |
| 52 | + block={usersBlock} |
| 53 | + context={BlockContext.FORM} |
| 54 | + surfaceRenderer={contextualBarParser} |
| 55 | + /> |
| 56 | + </MockedServerContext> |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should render a UiKit multiple users selector', async () => { |
| 61 | + expect(await screen.findByRole('textbox')).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should open the users selector', async () => { |
| 65 | + const input = await screen.findByRole('textbox'); |
| 66 | + input.focus(); |
| 67 | + |
| 68 | + expect(await screen.findByRole('listbox')).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should select users', async () => { |
| 72 | + const input = await screen.findByRole('textbox'); |
| 73 | + |
| 74 | + input.focus(); |
| 75 | + |
| 76 | + const option1 = (await screen.findAllByRole('option'))[0]; |
| 77 | + await userEvent.click(option1, { delay: null }); |
| 78 | + |
| 79 | + const option2 = (await screen.findAllByRole('option'))[2]; |
| 80 | + await userEvent.click(option2, { delay: null }); |
| 81 | + |
| 82 | + const selected = await screen.findAllByRole('button'); |
| 83 | + expect(selected[0]).toHaveValue('user1_id'); |
| 84 | + expect(selected[1]).toHaveValue('user3_id'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should remove a user', async () => { |
| 88 | + const input = await screen.findByRole('textbox'); |
| 89 | + |
| 90 | + input.focus(); |
| 91 | + |
| 92 | + const option1 = (await screen.findAllByRole('option'))[0]; |
| 93 | + await userEvent.click(option1, { delay: null }); |
| 94 | + |
| 95 | + const option2 = (await screen.findAllByRole('option'))[2]; |
| 96 | + await userEvent.click(option2, { delay: null }); |
| 97 | + |
| 98 | + const selected1 = (await screen.findAllByRole('button'))[0]; |
| 99 | + expect(selected1).toHaveValue('user1_id'); |
| 100 | + await userEvent.click(selected1, { delay: null }); |
| 101 | + |
| 102 | + const remainingSelected = (await screen.findAllByRole('button'))[0]; |
| 103 | + expect(remainingSelected).toHaveValue('user3_id'); |
| 104 | + }); |
| 105 | +}); |
0 commit comments