|
| 1 | +import type { SlashCommand } from '@rocket.chat/core-typings'; |
| 2 | +import { mockAppRoot, type StreamControllerRef } from '@rocket.chat/mock-providers'; |
| 3 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 4 | + |
| 5 | +import { useAppSlashCommands } from './useAppSlashCommands'; |
| 6 | +import { slashCommands } from '../../app/utils/client/slashCommand'; |
| 7 | + |
| 8 | +const mockSlashCommands: SlashCommand[] = [ |
| 9 | + { |
| 10 | + command: '/test', |
| 11 | + description: 'Test command', |
| 12 | + params: 'param1 param2', |
| 13 | + clientOnly: false, |
| 14 | + providesPreview: false, |
| 15 | + appId: 'test-app-1', |
| 16 | + permission: undefined, |
| 17 | + }, |
| 18 | + { |
| 19 | + command: '/weather', |
| 20 | + description: 'Get weather information', |
| 21 | + params: 'city', |
| 22 | + clientOnly: false, |
| 23 | + providesPreview: true, |
| 24 | + appId: 'weather-app', |
| 25 | + permission: undefined, |
| 26 | + }, |
| 27 | +]; |
| 28 | + |
| 29 | +const mockApiResponse = { |
| 30 | + commands: mockSlashCommands, |
| 31 | + total: mockSlashCommands.length, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('useAppSlashCommands', () => { |
| 35 | + let mockGetSlashCommands: jest.Mock; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + mockGetSlashCommands = jest.fn().mockResolvedValue(mockApiResponse); |
| 39 | + |
| 40 | + slashCommands.commands = {}; |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + jest.clearAllMocks(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should not fetch data when user ID is not available', () => { |
| 48 | + renderHook(() => useAppSlashCommands(), { |
| 49 | + wrapper: mockAppRoot().withEndpoint('GET', '/v1/commands.list', mockGetSlashCommands).build(), |
| 50 | + }); |
| 51 | + |
| 52 | + expect(mockGetSlashCommands).not.toHaveBeenCalled(); |
| 53 | + expect(slashCommands.commands).toEqual({}); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should fetch slash commands when user ID is available', async () => { |
| 57 | + renderHook(() => useAppSlashCommands(), { |
| 58 | + wrapper: mockAppRoot().withEndpoint('GET', '/v1/commands.list', mockGetSlashCommands).withJohnDoe().build(), |
| 59 | + }); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + expect(Object.keys(slashCommands.commands)).toHaveLength(mockSlashCommands.length); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle command/removed event by invalidating queries', async () => { |
| 67 | + const streamRef: StreamControllerRef<'apps'> = {}; |
| 68 | + |
| 69 | + renderHook(() => useAppSlashCommands(), { |
| 70 | + wrapper: mockAppRoot() |
| 71 | + .withJohnDoe() |
| 72 | + .withStream('apps', streamRef) |
| 73 | + .withEndpoint('GET', '/v1/commands.list', mockGetSlashCommands) |
| 74 | + .build(), |
| 75 | + }); |
| 76 | + |
| 77 | + expect(streamRef.controller).toBeDefined(); |
| 78 | + |
| 79 | + await waitFor(() => { |
| 80 | + expect(Object.keys(slashCommands.commands)).toHaveLength(mockSlashCommands.length); |
| 81 | + }); |
| 82 | + |
| 83 | + streamRef.controller?.emit('apps', [['command/removed', ['/test']]]); |
| 84 | + |
| 85 | + expect(slashCommands.commands['/test']).toBeUndefined(); |
| 86 | + expect(slashCommands.commands['/weather']).toBeDefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle command/added event by invalidating queries', async () => { |
| 90 | + const streamRef: StreamControllerRef<'apps'> = {}; |
| 91 | + |
| 92 | + renderHook(() => useAppSlashCommands(), { |
| 93 | + wrapper: mockAppRoot() |
| 94 | + .withJohnDoe() |
| 95 | + .withStream('apps', streamRef) |
| 96 | + .withEndpoint('GET', '/v1/commands.list', mockGetSlashCommands) |
| 97 | + .build(), |
| 98 | + }); |
| 99 | + |
| 100 | + expect(streamRef.controller).toBeDefined(); |
| 101 | + |
| 102 | + await waitFor(() => { |
| 103 | + expect(Object.keys(slashCommands.commands)).toHaveLength(mockSlashCommands.length); |
| 104 | + }); |
| 105 | + |
| 106 | + mockGetSlashCommands.mockResolvedValue({ |
| 107 | + commands: [ |
| 108 | + ...mockSlashCommands, |
| 109 | + { |
| 110 | + command: '/newcommand', |
| 111 | + description: 'New command', |
| 112 | + params: 'param1 param2', |
| 113 | + clientOnly: false, |
| 114 | + }, |
| 115 | + ], |
| 116 | + total: mockSlashCommands.length + 1, |
| 117 | + }); |
| 118 | + |
| 119 | + streamRef.controller?.emit('apps', [['command/added', ['/newcommand']]]); |
| 120 | + |
| 121 | + await waitFor(() => { |
| 122 | + expect(slashCommands.commands['/newcommand']).toBeDefined(); |
| 123 | + }); |
| 124 | + |
| 125 | + expect(slashCommands.commands['/test']).toBeDefined(); |
| 126 | + expect(slashCommands.commands['/weather']).toBeDefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle command/updated event by invalidating queries', async () => { |
| 130 | + const streamRef: StreamControllerRef<'apps'> = {}; |
| 131 | + |
| 132 | + renderHook(() => useAppSlashCommands(), { |
| 133 | + wrapper: mockAppRoot() |
| 134 | + .withJohnDoe() |
| 135 | + .withStream('apps', streamRef) |
| 136 | + .withEndpoint('GET', '/v1/commands.list', mockGetSlashCommands) |
| 137 | + .build(), |
| 138 | + }); |
| 139 | + |
| 140 | + expect(streamRef.controller).toBeDefined(); |
| 141 | + |
| 142 | + await waitFor(() => { |
| 143 | + expect(Object.keys(slashCommands.commands)).toHaveLength(mockSlashCommands.length); |
| 144 | + }); |
| 145 | + |
| 146 | + streamRef.controller?.emit('apps', [['command/updated', ['/test']]]); |
| 147 | + |
| 148 | + expect(slashCommands.commands['/test']).toBeUndefined(); |
| 149 | + expect(slashCommands.commands['/weather']).toBeDefined(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should ignore command/disabled event', async () => { |
| 153 | + const streamRef: StreamControllerRef<'apps'> = {}; |
| 154 | + |
| 155 | + renderHook(() => useAppSlashCommands(), { |
| 156 | + wrapper: mockAppRoot() |
| 157 | + .withJohnDoe() |
| 158 | + .withStream('apps', streamRef) |
| 159 | + .withEndpoint('GET', '/v1/commands.list', mockGetSlashCommands) |
| 160 | + .build(), |
| 161 | + }); |
| 162 | + |
| 163 | + expect(streamRef.controller).toBeDefined(); |
| 164 | + |
| 165 | + await waitFor(() => { |
| 166 | + expect(Object.keys(slashCommands.commands)).toHaveLength(mockSlashCommands.length); |
| 167 | + }); |
| 168 | + |
| 169 | + streamRef.controller?.emit('apps', [['command/disabled', ['/test']]]); |
| 170 | + |
| 171 | + expect(slashCommands.commands['/test']).toBeDefined(); |
| 172 | + expect(slashCommands.commands['/weather']).toBeDefined(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should not set up stream listener when user ID is not available', () => { |
| 176 | + const streamRef: StreamControllerRef<'apps'> = {}; |
| 177 | + |
| 178 | + renderHook(() => useAppSlashCommands(), { |
| 179 | + wrapper: mockAppRoot().withStream('apps', streamRef).build(), |
| 180 | + }); |
| 181 | + |
| 182 | + expect(streamRef.controller).toBeDefined(); |
| 183 | + expect(streamRef.controller?.has('apps')).toBe(false); |
| 184 | + }); |
| 185 | +}); |
0 commit comments