|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Provider } from 'react-redux'; |
| 3 | +import { render, fireEvent } from '@testing-library/react-native'; |
| 4 | + |
| 5 | +import Reactions from './Reactions'; |
| 6 | +import MessageContext from './Context'; |
| 7 | +import { setUser } from '../../actions/login'; |
| 8 | +import { mockedStore } from '../../reducers/mockedStore'; |
| 9 | +import { IReaction } from '../../definitions'; |
| 10 | + |
| 11 | +const initialMockedStoreState = () => { |
| 12 | + mockedStore.dispatch( |
| 13 | + setUser({ |
| 14 | + settings: { |
| 15 | + preferences: { |
| 16 | + convertAsciiEmoji: true |
| 17 | + } |
| 18 | + } |
| 19 | + }) |
| 20 | + ); |
| 21 | +}; |
| 22 | + |
| 23 | +initialMockedStoreState(); |
| 24 | + |
| 25 | +const TestWrapper = ({ initialReactions }: { initialReactions: IReaction[] }) => { |
| 26 | + const [reactions, setReactions] = useState<IReaction[]>(initialReactions); |
| 27 | + const handleReactionPress = (emoji: string) => { |
| 28 | + setReactions(prev => prev.filter(r => r.emoji !== emoji)); |
| 29 | + }; |
| 30 | + |
| 31 | + return ( |
| 32 | + <Provider store={mockedStore}> |
| 33 | + <MessageContext.Provider |
| 34 | + value={{ |
| 35 | + user: { username: 'john' }, |
| 36 | + reactionInit: jest.fn(), |
| 37 | + onReactionPress: handleReactionPress, |
| 38 | + onReactionLongPress: jest.fn() |
| 39 | + }}> |
| 40 | + <Reactions reactions={reactions} getCustomEmoji={getCustomEmoji} /> |
| 41 | + </MessageContext.Provider> |
| 42 | + </Provider> |
| 43 | + ); |
| 44 | +}; |
| 45 | + |
| 46 | +const getCustomEmoji = jest.fn(); |
| 47 | + |
| 48 | +it('renders all reactions and AddReaction button', () => { |
| 49 | + const reactions: IReaction[] = [ |
| 50 | + { _id: '1', emoji: '👍', usernames: ['john', 'alice'], names: [] }, |
| 51 | + { _id: '2', emoji: '😂', usernames: ['bob'], names: [] } |
| 52 | + ]; |
| 53 | + |
| 54 | + const { getByTestId } = render(<TestWrapper initialReactions={reactions} />); |
| 55 | + |
| 56 | + expect(getByTestId('message-reaction-👍')).toBeTruthy(); |
| 57 | + expect(getByTestId('message-reaction-😂')).toBeTruthy(); |
| 58 | + expect(getByTestId('message-add-reaction')).toBeTruthy(); |
| 59 | +}); |
| 60 | + |
| 61 | +it('should render unicode emoji reaction', () => { |
| 62 | + const reactions = [{ _id: '1', emoji: ':)', usernames: ['john', 'alice'], names: [] }]; |
| 63 | + const { getByTestId } = render(<TestWrapper initialReactions={reactions} />); |
| 64 | + |
| 65 | + expect(getByTestId('message-reaction-:)')).toBeTruthy(); |
| 66 | + expect(getByTestId('message-add-reaction')).toBeTruthy(); |
| 67 | +}); |
| 68 | + |
| 69 | +it('should render custom emoji reaction', () => { |
| 70 | + const reactions = [{ _id: '1', emoji: ':aaaaa:', usernames: ['john', 'alice'], names: [] }]; |
| 71 | + const { getByTestId } = render(<TestWrapper initialReactions={reactions} />); |
| 72 | + |
| 73 | + expect(getByTestId('message-reaction-:aaaaa:')).toBeTruthy(); |
| 74 | + expect(getByTestId('message-add-reaction')).toBeTruthy(); |
| 75 | +}); |
| 76 | + |
| 77 | +it('should remove reaction', () => { |
| 78 | + const reactions = [ |
| 79 | + { _id: '1', emoji: ':thumbsup:', usernames: ['john'], names: [] }, |
| 80 | + { _id: '1', emoji: ':heart_eyes:', usernames: ['john', 'alice'], names: [] } |
| 81 | + ]; |
| 82 | + const { getByTestId, queryByTestId } = render(<TestWrapper initialReactions={reactions} />); |
| 83 | + |
| 84 | + expect(getByTestId('message-reaction-:thumbsup:')).toBeTruthy(); |
| 85 | + expect(getByTestId('message-reaction-:heart_eyes:')).toBeTruthy(); |
| 86 | + fireEvent.press(getByTestId('message-reaction-:thumbsup:')); |
| 87 | + expect(queryByTestId('message-reaction-:thumbsup:')).toBeNull(); |
| 88 | + expect(getByTestId('message-add-reaction')).toBeTruthy(); |
| 89 | +}); |
0 commit comments