|
1 | | -import { useUser, useUserSubscriptions, useRoomAvatarPath } from '@rocket.chat/ui-contexts'; |
| 1 | +import { MockedAppRootBuilder } from '@rocket.chat/mock-providers/dist/MockedAppRootBuilder'; |
| 2 | +import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts'; |
2 | 3 | import { render, screen, waitFor } from '@testing-library/react'; |
3 | 4 | import userEvent from '@testing-library/user-event'; |
4 | 5 |
|
5 | 6 | import UserAndRoomAutoCompleteMultiple from './UserAndRoomAutoCompleteMultiple'; |
| 7 | +import { createFakeSubscription, createFakeUser } from '../../../tests/mocks/data'; |
6 | 8 | import { roomCoordinator } from '../../lib/rooms/roomCoordinator'; |
7 | 9 |
|
8 | | -// Mock dependencies |
9 | | -jest.mock('@rocket.chat/ui-contexts', () => ({ |
10 | | - useUser: jest.fn(), |
11 | | - useUserSubscriptions: jest.fn(), |
12 | | - useRoomAvatarPath: jest.fn(), |
13 | | -})); |
| 10 | +const user = createFakeUser({ |
| 11 | + active: true, |
| 12 | + roles: ['admin'], |
| 13 | + type: 'user', |
| 14 | +}); |
| 15 | + |
| 16 | +const direct = createFakeSubscription({ |
| 17 | + t: 'd', |
| 18 | + name: 'Direct', |
| 19 | +}); |
| 20 | + |
| 21 | +const channel = createFakeSubscription({ |
| 22 | + t: 'c', |
| 23 | + name: 'General', |
| 24 | +}); |
| 25 | + |
| 26 | +const appRoot = new MockedAppRootBuilder() |
| 27 | + .withSubscriptions([ |
| 28 | + { ...direct, ro: false }, |
| 29 | + { ...channel, ro: true }, |
| 30 | + ] as unknown as SubscriptionWithRoom[]) |
| 31 | + .withUser(user); |
| 32 | + |
14 | 33 | jest.mock('../../lib/rooms/roomCoordinator', () => ({ |
15 | | - roomCoordinator: { readOnly: jest.fn() }, |
| 34 | + roomCoordinator: { |
| 35 | + readOnly: jest.fn(), |
| 36 | + }, |
16 | 37 | })); |
17 | 38 |
|
18 | | -const mockUser = { _id: 'user1', username: 'testuser' }; |
| 39 | +beforeEach(() => { |
| 40 | + (roomCoordinator.readOnly as jest.Mock).mockReturnValue(false); |
| 41 | +}); |
19 | 42 |
|
20 | | -const mockRooms = [ |
21 | | - { |
22 | | - rid: 'room1', |
23 | | - fname: 'General', |
24 | | - name: 'general', |
25 | | - t: 'c', |
26 | | - avatarETag: 'etag1', |
27 | | - }, |
28 | | - { |
29 | | - rid: 'room2', |
30 | | - fname: 'Direct', |
31 | | - name: 'direct', |
32 | | - t: 'd', |
33 | | - avatarETag: 'etag2', |
34 | | - blocked: false, |
35 | | - blocker: false, |
36 | | - }, |
37 | | -]; |
38 | | - |
39 | | -describe('UserAndRoomAutoCompleteMultiple', () => { |
40 | | - beforeEach(() => { |
41 | | - (useUser as jest.Mock).mockReturnValue(mockUser); |
42 | | - (useUserSubscriptions as jest.Mock).mockReturnValue(mockRooms); |
43 | | - (useRoomAvatarPath as jest.Mock).mockReturnValue((rid: string) => `/avatar/path/${rid}`); |
44 | | - (roomCoordinator.readOnly as jest.Mock).mockReturnValue(false); |
45 | | - }); |
| 43 | +afterEach(() => jest.clearAllMocks()); |
46 | 44 |
|
47 | | - it('should render options based on user subscriptions', async () => { |
48 | | - render(<UserAndRoomAutoCompleteMultiple value={[]} onChange={jest.fn()} />); |
| 45 | +it('should render options based on user subscriptions', async () => { |
| 46 | + render(<UserAndRoomAutoCompleteMultiple value={[]} onChange={jest.fn()} />, { wrapper: appRoot.build() }); |
49 | 47 |
|
50 | | - const input = screen.getByRole('textbox'); |
51 | | - await userEvent.click(input); |
| 48 | + const input = screen.getByRole('textbox'); |
| 49 | + await userEvent.click(input); |
52 | 50 |
|
53 | | - await waitFor(() => { |
54 | | - expect(screen.getByText('General')).toBeInTheDocument(); |
55 | | - }); |
| 51 | + await waitFor(() => { |
| 52 | + expect(screen.getByText('Direct')).toBeInTheDocument(); |
| 53 | + }); |
56 | 54 |
|
57 | | - await waitFor(() => { |
58 | | - expect(screen.getByText('Direct')).toBeInTheDocument(); |
59 | | - }); |
| 55 | + await waitFor(() => { |
| 56 | + expect(screen.getByText('General')).toBeInTheDocument(); |
60 | 57 | }); |
| 58 | +}); |
61 | 59 |
|
62 | | - it('should filter out read-only rooms', async () => { |
63 | | - (roomCoordinator.readOnly as jest.Mock).mockImplementation((rid) => rid === 'room1'); |
64 | | - render(<UserAndRoomAutoCompleteMultiple value={[]} onChange={jest.fn()} />); |
| 60 | +it('should filter out read-only rooms', async () => { |
| 61 | + (roomCoordinator.readOnly as jest.Mock).mockReturnValueOnce(true); |
65 | 62 |
|
66 | | - const input = screen.getByRole('textbox'); |
67 | | - await userEvent.click(input); |
| 63 | + render(<UserAndRoomAutoCompleteMultiple value={[]} onChange={jest.fn()} />, { wrapper: appRoot.build() }); |
68 | 64 |
|
69 | | - await waitFor(() => { |
70 | | - expect(screen.queryByText('General')).not.toBeInTheDocument(); |
71 | | - }); |
72 | | - await waitFor(() => { |
73 | | - expect(screen.getByText('Direct')).toBeInTheDocument(); |
74 | | - }); |
| 65 | + const input = screen.getByRole('textbox'); |
| 66 | + await userEvent.click(input); |
| 67 | + |
| 68 | + await waitFor(() => { |
| 69 | + expect(screen.getByText('General')).toBeInTheDocument(); |
75 | 70 | }); |
76 | 71 |
|
77 | | - it('should call onChange when selecting an option', async () => { |
78 | | - const handleChange = jest.fn(); |
79 | | - render(<UserAndRoomAutoCompleteMultiple value={[]} onChange={handleChange} />); |
| 72 | + await waitFor(() => { |
| 73 | + expect(screen.queryByText('Direct')).not.toBeInTheDocument(); |
| 74 | + }); |
| 75 | +}); |
80 | 76 |
|
81 | | - const input = screen.getByRole('textbox'); |
82 | | - await userEvent.click(input); |
| 77 | +it('should call onChange when selecting an option', async () => { |
| 78 | + const handleChange = jest.fn(); |
| 79 | + render(<UserAndRoomAutoCompleteMultiple value={[]} onChange={handleChange} />, { wrapper: appRoot.build() }); |
83 | 80 |
|
84 | | - await waitFor(() => { |
85 | | - expect(screen.getByText('General')).toBeInTheDocument(); |
86 | | - }); |
| 81 | + const input = screen.getByRole('textbox'); |
| 82 | + await userEvent.click(input); |
87 | 83 |
|
88 | | - await userEvent.click(screen.getByText('General')); |
89 | | - expect(handleChange).toHaveBeenCalled(); |
| 84 | + await waitFor(() => { |
| 85 | + expect(screen.getByText('General')).toBeInTheDocument(); |
90 | 86 | }); |
| 87 | + |
| 88 | + await userEvent.click(screen.getByText('General')); |
| 89 | + expect(handleChange).toHaveBeenCalled(); |
91 | 90 | }); |
0 commit comments