|
| 1 | +import { mockAppRoot } from '@rocket.chat/mock-providers'; |
| 2 | +import { createAutocompletePopupConfig } from '@rocket.chat/ui-client'; |
| 3 | +import { act, fireEvent, renderHook, waitFor } from '@testing-library/react'; |
| 4 | + |
| 5 | +import { useComposerBoxPopup } from './useComposerBoxPopup'; |
| 6 | +import type { ChatAPI } from '../../../../lib/chats/ChatAPI'; |
| 7 | +import { ChatContext } from '../../contexts/ChatContext'; |
| 8 | + |
| 9 | +const keys = { ENTER: 13, ESC: 27, ARROW_UP: 38, ARROW_DOWN: 40 }; |
| 10 | + |
| 11 | +type Item = { _id: string; username?: string }; |
| 12 | + |
| 13 | +const createComposer = (text: string, caret = text.length) => ({ |
| 14 | + substring: (start: number, end?: number) => text.substring(start, end), |
| 15 | + selection: { start: caret, end: caret }, |
| 16 | + replaceText: jest.fn(), |
| 17 | +}); |
| 18 | + |
| 19 | +const userOption = createAutocompletePopupConfig<Item>({ |
| 20 | + trigger: '@', |
| 21 | + getItemsFromLocal: async () => [ |
| 22 | + { _id: 'u1', username: 'alice' }, |
| 23 | + { _id: 'u2', username: 'bob' }, |
| 24 | + ], |
| 25 | + getItemsFromServer: async () => [], |
| 26 | + getValue: (item) => item.username ?? '', |
| 27 | +}); |
| 28 | + |
| 29 | +const emojiOption = createAutocompletePopupConfig<Item>({ |
| 30 | + trigger: ':', |
| 31 | + triggerLength: 2, |
| 32 | + getItemsFromLocal: async (filter: string) => [{ _id: `:${filter}a:` }, { _id: `:${filter}b:` }], |
| 33 | + getItemsFromServer: async () => [], |
| 34 | + getValue: (item) => item._id.substring(1), |
| 35 | +}); |
| 36 | + |
| 37 | +const commandOption = createAutocompletePopupConfig<Item>({ |
| 38 | + trigger: '/', |
| 39 | + triggerAnywhere: false, |
| 40 | + getItemsFromLocal: async () => [{ _id: 'archive' }], |
| 41 | + getItemsFromServer: async () => [], |
| 42 | + getValue: (item) => item._id, |
| 43 | +}); |
| 44 | + |
| 45 | +const options = [userOption, emojiOption, commandOption]; |
| 46 | + |
| 47 | +const setup = (text: string, caret = text.length) => { |
| 48 | + const composer = createComposer(text, caret); |
| 49 | + const node = document.createElement('textarea'); |
| 50 | + |
| 51 | + const { result } = renderHook(() => useComposerBoxPopup(options), { |
| 52 | + wrapper: mockAppRoot() |
| 53 | + .wrap((children) => <ChatContext.Provider value={{ composer } as unknown as ChatAPI}>{children}</ChatContext.Provider>) |
| 54 | + .build(), |
| 55 | + }); |
| 56 | + act(() => result.current.callbackRef(node)); |
| 57 | + |
| 58 | + const fire = (type: 'keyUp' | 'keyDown', which: number) => fireEvent[type](node, { which, keyCode: which }); |
| 59 | + |
| 60 | + return { result, composer, fire }; |
| 61 | +}; |
| 62 | + |
| 63 | +describe('useComposerBoxPopup (characterization)', () => { |
| 64 | + it('opens no popup when the text has no trigger', () => { |
| 65 | + const { result, fire } = setup('hello'); |
| 66 | + fire('keyUp', 0); |
| 67 | + expect(result.current.option).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('detects the `@` trigger and extracts the filter', () => { |
| 71 | + const { result, fire } = setup('hi @al'); |
| 72 | + fire('keyUp', 0); |
| 73 | + expect(result.current.option?.trigger).toBe('@'); |
| 74 | + expect(result.current.filter).toBe('al'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('respects triggerLength: `:` alone does not open, `:ab` does', () => { |
| 78 | + const closed = setup('x :'); |
| 79 | + closed.fire('keyUp', 0); |
| 80 | + expect(closed.result.current.option).toBeUndefined(); |
| 81 | + |
| 82 | + const open = setup('x :ab'); |
| 83 | + open.fire('keyUp', 0); |
| 84 | + expect(open.result.current.option?.trigger).toBe(':'); |
| 85 | + expect(open.result.current.filter).toBe('ab'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('only triggers a start-only (`/`) option at the very start of the text', () => { |
| 89 | + const mid = setup('hey /arch'); |
| 90 | + mid.fire('keyUp', 0); |
| 91 | + expect(mid.result.current.option).toBeUndefined(); |
| 92 | + |
| 93 | + const start = setup('/arch'); |
| 94 | + start.fire('keyUp', 0); |
| 95 | + expect(start.result.current.option?.trigger).toBe('/'); |
| 96 | + expect(start.result.current.filter).toBe('arch'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('inserts prefix + value + suffix over the trigger range on select', async () => { |
| 100 | + const { result, composer, fire } = setup('hi @al'); |
| 101 | + fire('keyUp', 0); |
| 102 | + await waitFor(() => expect(result.current.focused).toBeDefined()); |
| 103 | + |
| 104 | + act(() => result.current.select?.({ _id: 'u1', username: 'alice' })); |
| 105 | + |
| 106 | + expect(composer.replaceText).toHaveBeenCalledWith('@alice ', { start: 3, end: 6 }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('moves focus with arrow keys and wraps around', async () => { |
| 110 | + const { result, fire } = setup('hi @'); |
| 111 | + fire('keyUp', 0); |
| 112 | + await waitFor(() => expect(result.current.focused?._id).toBe('u1')); |
| 113 | + |
| 114 | + fire('keyDown', keys.ARROW_DOWN); |
| 115 | + expect(result.current.focused?._id).toBe('u2'); |
| 116 | + |
| 117 | + fire('keyDown', keys.ARROW_DOWN); |
| 118 | + expect(result.current.focused?._id).toBe('u1'); |
| 119 | + |
| 120 | + fire('keyDown', keys.ARROW_UP); |
| 121 | + expect(result.current.focused?._id).toBe('u2'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('selects the focused item on Enter', async () => { |
| 125 | + const { result, composer, fire } = setup('hi @'); |
| 126 | + fire('keyUp', 0); |
| 127 | + await waitFor(() => expect(result.current.focused?._id).toBe('u1')); |
| 128 | + |
| 129 | + fire('keyDown', keys.ENTER); |
| 130 | + expect(composer.replaceText).toHaveBeenCalledWith('@alice ', expect.objectContaining({ end: 4 })); |
| 131 | + }); |
| 132 | + |
| 133 | + it('closes the popup on Escape', async () => { |
| 134 | + const { result, fire } = setup('hi @al'); |
| 135 | + fire('keyUp', 0); |
| 136 | + await waitFor(() => expect(result.current.option?.trigger).toBe('@')); |
| 137 | + |
| 138 | + fire('keyUp', keys.ESC); |
| 139 | + expect(result.current.option).toBeUndefined(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments