|
| 1 | +import React from 'react'; |
| 2 | +import { act } from '@testing-library/react'; |
| 3 | +import { renderHook } from '@testing-library/react-hooks'; |
| 4 | +import { |
| 5 | + dispatchMessageNewEvent, |
| 6 | + generateMessage, |
| 7 | + generateUser, |
| 8 | + getTestClientWithUser, |
| 9 | +} from '../../../../mock-builders'; |
| 10 | + |
| 11 | +import { useGiphyPreview } from '../VirtualizedMessageList'; |
| 12 | +import { ChatProvider } from '../../../../context'; |
| 13 | + |
| 14 | +const me = generateUser(); |
| 15 | +const otherUser = generateUser(); |
| 16 | + |
| 17 | +const ownGiphyMessage = generateMessage({ command: 'giphy', user: me }); |
| 18 | +const foreignGiphyMessage = generateMessage({ command: 'giphy', user: otherUser }); |
| 19 | +const ownNonGiphyMessage = generateMessage({ user: me }); |
| 20 | +const foreignNonGiphyMessage = generateMessage({ user: otherUser }); |
| 21 | + |
| 22 | +const render = ({ client, separateGiphyPreview }) => { |
| 23 | + const wrapper = ({ children }) => <ChatProvider value={{ client }}>{children}</ChatProvider>; |
| 24 | + return renderHook(() => useGiphyPreview(separateGiphyPreview), { wrapper }); |
| 25 | +}; |
| 26 | +describe('useGiphyPreview', () => { |
| 27 | + it('does not expose setGiphyPreviewMessage function if separateGiphyPreview is disabled', async () => { |
| 28 | + const client = await getTestClientWithUser(me); |
| 29 | + const { result } = render({ client, separateGiphyPreview: false }); |
| 30 | + expect(result.current.giphyPreviewMessage).toBeUndefined(); |
| 31 | + expect(result.current.setGiphyPreviewMessage).toBeUndefined(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('exposes setGiphyPreviewMessage function if separateGiphyPreview is enabled', async () => { |
| 35 | + const client = await getTestClientWithUser(me); |
| 36 | + const { result } = render({ client, separateGiphyPreview: true }); |
| 37 | + expect(result.current.giphyPreviewMessage).toBeUndefined(); |
| 38 | + expect(result.current.setGiphyPreviewMessage).toStrictEqual(expect.any(Function)); |
| 39 | + }); |
| 40 | + |
| 41 | + it('unsets giphy preview with new own giphy message when separateGiphyPreview is enabled', async () => { |
| 42 | + const client = await getTestClientWithUser(me); |
| 43 | + const { result } = render({ client, separateGiphyPreview: true }); |
| 44 | + await act(() => { |
| 45 | + result.current.setGiphyPreviewMessage(ownGiphyMessage); |
| 46 | + }); |
| 47 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 48 | + await act(() => { |
| 49 | + dispatchMessageNewEvent(client, ownGiphyMessage); |
| 50 | + }); |
| 51 | + expect(result.current.giphyPreviewMessage).toBeUndefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not unset giphy preview with new foreign giphy message when separateGiphyPreview is enabled', async () => { |
| 55 | + const client = await getTestClientWithUser(me); |
| 56 | + const { result } = render({ client, separateGiphyPreview: true }); |
| 57 | + await act(() => { |
| 58 | + result.current.setGiphyPreviewMessage(ownGiphyMessage); |
| 59 | + }); |
| 60 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 61 | + await act(() => { |
| 62 | + dispatchMessageNewEvent(client, foreignGiphyMessage); |
| 63 | + }); |
| 64 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 65 | + }); |
| 66 | + |
| 67 | + it('does not unset giphy preview with new own non-giphy message when separateGiphyPreview is enabled', async () => { |
| 68 | + const client = await getTestClientWithUser(me); |
| 69 | + const { result } = render({ client, separateGiphyPreview: true }); |
| 70 | + await act(() => { |
| 71 | + result.current.setGiphyPreviewMessage(ownGiphyMessage); |
| 72 | + }); |
| 73 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 74 | + await act(() => { |
| 75 | + dispatchMessageNewEvent(client, ownNonGiphyMessage); |
| 76 | + }); |
| 77 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not unset giphy preview with new foreign non-giphy message when separateGiphyPreview is enabled', async () => { |
| 81 | + const client = await getTestClientWithUser(me); |
| 82 | + const { result } = render({ client, separateGiphyPreview: true }); |
| 83 | + await act(() => { |
| 84 | + result.current.setGiphyPreviewMessage(ownGiphyMessage); |
| 85 | + }); |
| 86 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 87 | + await act(() => { |
| 88 | + dispatchMessageNewEvent(client, foreignNonGiphyMessage); |
| 89 | + }); |
| 90 | + expect(result.current.giphyPreviewMessage.id).toBe(ownGiphyMessage.id); |
| 91 | + }); |
| 92 | +}); |
0 commit comments