|
| 1 | +// test case for ChatMessage component |
| 2 | +import { fireEvent } from '@testing-library/react'; |
| 3 | +import { ChatMessage } from '../ChatMessage'; |
| 4 | +import renderWithSSR from '~utils/testing/renderWithSSR.web'; |
| 5 | +import { RayIcon } from '~components/Icons'; |
| 6 | +import { Card, CardBody } from '~components/Card'; |
| 7 | +import { Box } from '~components/Box'; |
| 8 | +import { Text } from '~components/Typography'; |
| 9 | +import { Radio, RadioGroup } from '~components/Radio'; |
| 10 | + |
| 11 | +describe('<ChatMessage/>', () => { |
| 12 | + it('should render last message correctly', () => { |
| 13 | + const { container } = renderWithSSR( |
| 14 | + <ChatMessage senderType="self" messageType="last"> |
| 15 | + {' '} |
| 16 | + This is a demo message{' '} |
| 17 | + </ChatMessage>, |
| 18 | + ); |
| 19 | + expect(container).toMatchSnapshot(); |
| 20 | + }); |
| 21 | + it('should render last message correctly', () => { |
| 22 | + const { container } = renderWithSSR( |
| 23 | + <ChatMessage messageType="default" senderType="self"> |
| 24 | + {' '} |
| 25 | + This is another demo message{' '} |
| 26 | + </ChatMessage>, |
| 27 | + ); |
| 28 | + expect(container).toMatchSnapshot(); |
| 29 | + }); |
| 30 | + it('should render last message correctly', () => { |
| 31 | + const { container } = renderWithSSR( |
| 32 | + <ChatMessage |
| 33 | + senderType="other" |
| 34 | + leading={<RayIcon size="xlarge" color="surface.icon.onSea.onSubtle" />} |
| 35 | + > |
| 36 | + {' '} |
| 37 | + This is another demo message{' '} |
| 38 | + </ChatMessage>, |
| 39 | + ); |
| 40 | + expect(container).toMatchSnapshot(); |
| 41 | + }); |
| 42 | + it('should render last message correctly', () => { |
| 43 | + const { container } = renderWithSSR( |
| 44 | + <ChatMessage |
| 45 | + senderType="other" |
| 46 | + leading={<RayIcon size="xlarge" color="surface.icon.onSea.onSubtle" />} |
| 47 | + loadingText="Analyzing your response..." |
| 48 | + > |
| 49 | + <Card> |
| 50 | + <CardBody> |
| 51 | + <Box display="flex" gap="8px" flexDirection="column"> |
| 52 | + <Text variant="body" size="medium"> |
| 53 | + Where do you want to collect payments? |
| 54 | + </Text> |
| 55 | + <RadioGroup> |
| 56 | + <Radio value="website">Website</Radio> |
| 57 | + <Radio value="android">Android App</Radio> |
| 58 | + <Radio value="ios">iOS App</Radio> |
| 59 | + </RadioGroup> |
| 60 | + </Box> |
| 61 | + </CardBody> |
| 62 | + </Card> |
| 63 | + </ChatMessage>, |
| 64 | + ); |
| 65 | + expect(container).toMatchSnapshot(); |
| 66 | + }); |
| 67 | + it('it should fire onClick event when user clicks on message button', () => { |
| 68 | + const onClick = jest.fn(); |
| 69 | + const { getByText } = renderWithSSR( |
| 70 | + <ChatMessage |
| 71 | + senderType="self" |
| 72 | + messageType="last" |
| 73 | + validationState="error" |
| 74 | + errorText="Message not sent. Tap to retry." |
| 75 | + onClick={onClick} |
| 76 | + > |
| 77 | + Can you help me with the docs? |
| 78 | + </ChatMessage>, |
| 79 | + ); |
| 80 | + const message = getByText('Can you help me with the docs?'); |
| 81 | + fireEvent.click(message); |
| 82 | + expect(onClick).toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + it('should render loading message correctly when loadingText is passed as prop and children is Card component', () => { |
| 85 | + const { getByText } = renderWithSSR( |
| 86 | + <ChatMessage |
| 87 | + senderType="other" |
| 88 | + leading={<RayIcon size="xlarge" color="surface.icon.onSea.onSubtle" />} |
| 89 | + isLoading={true} |
| 90 | + loadingText="Analyzing your response..." |
| 91 | + > |
| 92 | + <Card> |
| 93 | + <CardBody> |
| 94 | + <Box display="flex" gap="8px" flexDirection="column"> |
| 95 | + <Text variant="body" size="medium"> |
| 96 | + Where do you want to collect payments? |
| 97 | + </Text> |
| 98 | + <RadioGroup> |
| 99 | + <Radio value="website">Website</Radio> |
| 100 | + <Radio value="android">Android App</Radio> |
| 101 | + <Radio value="ios">iOS App</Radio> |
| 102 | + </RadioGroup> |
| 103 | + </Box> |
| 104 | + </CardBody> |
| 105 | + </Card> |
| 106 | + </ChatMessage>, |
| 107 | + ); |
| 108 | + const loadingTextElement = getByText('Analyzing your response...'); |
| 109 | + expect(loadingTextElement).toBeInTheDocument(); |
| 110 | + }); |
| 111 | + it('should render footer actions correctly when footerActions prop is passed', () => { |
| 112 | + const { getByText } = renderWithSSR( |
| 113 | + <ChatMessage |
| 114 | + senderType="other" |
| 115 | + footerActions={ |
| 116 | + <Box> |
| 117 | + <Box key={1}>Action 1</Box>,<Box key={2}>Action 2</Box>,<Box key={3}>Action 3</Box>, |
| 118 | + </Box> |
| 119 | + } |
| 120 | + > |
| 121 | + Can you help me with the docs? |
| 122 | + </ChatMessage>, |
| 123 | + ); |
| 124 | + const action1 = getByText('Action 1'); |
| 125 | + const action2 = getByText('Action 2'); |
| 126 | + const action3 = getByText('Action 3'); |
| 127 | + expect(action1).toBeInTheDocument(); |
| 128 | + expect(action2).toBeInTheDocument(); |
| 129 | + expect(action3).toBeInTheDocument(); |
| 130 | + }); |
| 131 | +}); |
0 commit comments