|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 3 | +import { CommandChip } from '../CommandChip'; |
| 4 | + |
| 5 | +const setCommandMock = vi.fn(); |
| 6 | +const focusMock = vi.fn(); |
| 7 | + |
| 8 | +vi.mock('../hooks', () => ({ |
| 9 | + useMessageComposerController: () => ({ |
| 10 | + textComposer: { |
| 11 | + setCommand: setCommandMock, |
| 12 | + }, |
| 13 | + }), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../../../context', () => ({ |
| 17 | + useMessageComposerContext: () => ({ |
| 18 | + textareaRef: { |
| 19 | + current: { |
| 20 | + focus: focusMock, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }), |
| 24 | +})); |
| 25 | + |
| 26 | +describe('CommandChip', () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not render when command is undefined', () => { |
| 32 | + const { container } = render(<CommandChip command={undefined} />); |
| 33 | + expect(container.firstChild).toBeNull(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders command name when command is defined', () => { |
| 37 | + render(<CommandChip command={{ name: 'giphy' }} />); |
| 38 | + expect(screen.getByText('giphy')).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('clears command and focuses textarea on close click', () => { |
| 42 | + const { container } = render(<CommandChip command={{ name: 'giphy' }} />); |
| 43 | + const closeButton = container.querySelector('.str-chat__command-chip__close-button'); |
| 44 | + |
| 45 | + expect(closeButton).toBeInTheDocument(); |
| 46 | + fireEvent.click(closeButton); |
| 47 | + |
| 48 | + expect(setCommandMock).toHaveBeenCalledWith(null); |
| 49 | + expect(focusMock).toHaveBeenCalledTimes(1); |
| 50 | + }); |
| 51 | +}); |
0 commit comments