From 34b7cd436b24f8501a57192946e298c0a009bbb0 Mon Sep 17 00:00:00 2001 From: Uri Goldshtein Date: Tue, 21 May 2019 23:16:27 +0300 Subject: [PATCH] Step 6.9: Test ChatRoomScreen child components --- .../ChatRoomScreen/ChatNavbar.test.tsx | 80 +++++++++++++++++++ src/components/ChatRoomScreen/ChatNavbar.tsx | 6 +- .../ChatRoomScreen/MessageInput.test.tsx | 51 ++++++++++++ .../ChatRoomScreen/MessageInput.tsx | 7 +- .../ChatRoomScreen/MessagesList.test.tsx | 41 ++++++++++ .../ChatRoomScreen/MessagesList.tsx | 8 +- 6 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 src/components/ChatRoomScreen/ChatNavbar.test.tsx create mode 100644 src/components/ChatRoomScreen/MessageInput.test.tsx create mode 100644 src/components/ChatRoomScreen/MessagesList.test.tsx diff --git a/src/components/ChatRoomScreen/ChatNavbar.test.tsx b/src/components/ChatRoomScreen/ChatNavbar.test.tsx new file mode 100644 index 000000000..c1f527f9b --- /dev/null +++ b/src/components/ChatRoomScreen/ChatNavbar.test.tsx @@ -0,0 +1,80 @@ +import { createMemoryHistory } from 'history'; +import React from 'react'; +import { cleanup, render, waitFor, fireEvent } from '@testing-library/react'; +import ChatNavbar from './ChatNavbar'; + +describe('ChatNavbar', () => { + afterEach(cleanup); + + it('renders chat data', () => { + const time = new Date('1 Jan 2019 GMT'); + const chat = { + id: '1', + name: 'Foo Bar', + picture: 'https://localhost:4000/picture.jpg', + messages: [ + { + id: '1', + content: 'foo', + createdAt: time, + }, + { + id: '2', + content: 'bar', + createdAt: time, + }, + ], + }; + + const history = createMemoryHistory(); + + { + const { container, getByTestId } = render( + + ); + + expect(getByTestId('chat-name')).toHaveTextContent('Foo Bar'); + expect(getByTestId('chat-picture')).toHaveAttribute( + 'src', + 'https://localhost:4000/picture.jpg' + ); + } + }); + + it('goes back on arrow click', async () => { + const time = new Date('1 Jan 2019 GMT'); + const chat = { + id: '1', + name: 'Foo Bar', + picture: 'https://localhost:4000/picture.jpg', + messages: [ + { + id: '1', + content: 'foo', + createdAt: time, + }, + { + id: '2', + content: 'bar', + createdAt: time, + }, + ], + }; + + const history = createMemoryHistory(); + + history.push('/chats/1'); + + await waitFor(() => expect(history.location.pathname).toEqual('/chats/1')); + + { + const { container, getByTestId } = render( + + ); + + fireEvent.click(getByTestId('back-button')); + + await waitFor(() => expect(history.location.pathname).toEqual('/chats')); + } + }); +}); diff --git a/src/components/ChatRoomScreen/ChatNavbar.tsx b/src/components/ChatRoomScreen/ChatNavbar.tsx index 98d263b07..cb1880fb1 100644 --- a/src/components/ChatRoomScreen/ChatNavbar.tsx +++ b/src/components/ChatRoomScreen/ChatNavbar.tsx @@ -47,11 +47,11 @@ const ChatNavbar: React.FC = ({ chat, history }) => { return ( - + - - {chat.name} + + {chat.name} ); }; diff --git a/src/components/ChatRoomScreen/MessageInput.test.tsx b/src/components/ChatRoomScreen/MessageInput.test.tsx new file mode 100644 index 000000000..78aa21948 --- /dev/null +++ b/src/components/ChatRoomScreen/MessageInput.test.tsx @@ -0,0 +1,51 @@ +import { createMemoryHistory } from 'history'; +import React from 'react'; +import { cleanup, render, waitFor, fireEvent } from '@testing-library/react'; +import MessageInput from './MessageInput'; + +describe('MessageInput;', () => { + afterEach(cleanup); + + it('triggers callback on send button click', async () => { + const onSendMessage = jest.fn(() => {}); + + { + const { container, getByTestId } = render( + + ); + const messageInput = getByTestId('message-input'); + const sendButton = getByTestId('send-button'); + + fireEvent.change(messageInput, { target: { value: 'foo' } }); + + await waitFor(() => messageInput); + + fireEvent.click(sendButton); + + await waitFor(() => expect(onSendMessage.mock.calls.length).toBe(1)); + } + }); + + it('triggers callback on Enter press', async () => { + const onSendMessage = jest.fn(() => {}); + + { + const { container, getByTestId } = render( + + ); + const messageInput = getByTestId('message-input'); + + fireEvent.change(messageInput, { target: { value: 'foo' } }); + + await waitFor(() => messageInput); + + fireEvent.keyPress(messageInput, { + key: 'Enter', + code: 13, + charCode: 13, + }); + + await waitFor(() => expect(onSendMessage.mock.calls.length).toBe(1)); + } + }); +}); diff --git a/src/components/ChatRoomScreen/MessageInput.tsx b/src/components/ChatRoomScreen/MessageInput.tsx index ae4050d69..e28bd55ba 100644 --- a/src/components/ChatRoomScreen/MessageInput.tsx +++ b/src/components/ChatRoomScreen/MessageInput.tsx @@ -70,13 +70,18 @@ const MessageInput: React.FC = ({ onSendMessage }) => { return ( - + diff --git a/src/components/ChatRoomScreen/MessagesList.test.tsx b/src/components/ChatRoomScreen/MessagesList.test.tsx new file mode 100644 index 000000000..08bed87ad --- /dev/null +++ b/src/components/ChatRoomScreen/MessagesList.test.tsx @@ -0,0 +1,41 @@ +import { createMemoryHistory } from 'history'; +import React from 'react'; +import { cleanup, render, getByTestId } from '@testing-library/react'; +import MessagesList from './MessagesList'; + +describe('MessagesList', () => { + afterEach(cleanup); + + const time = new Date('1 Jan 2019 GMT'); + + it('renders messages data', () => { + const messages = [ + { + id: '1', + content: 'foo', + createdAt: time, + }, + { + id: '2', + content: 'bar', + createdAt: time, + }, + ]; + + let message1, message2; + { + const { container, getAllByTestId, getByTestId } = render( + + ); + const match = getAllByTestId('message-item'); + message1 = match[0]; + message2 = match[1]; + } + + expect(getByTestId(message1, 'message-content')).toHaveTextContent('foo'); + expect(getByTestId(message1, 'message-date')).toHaveTextContent('00:00'); + + expect(getByTestId(message2, 'message-content')).toHaveTextContent('bar'); + expect(getByTestId(message2, 'message-date')).toHaveTextContent('00:00'); + }); +}); diff --git a/src/components/ChatRoomScreen/MessagesList.tsx b/src/components/ChatRoomScreen/MessagesList.tsx index a83f41815..3023fa045 100644 --- a/src/components/ChatRoomScreen/MessagesList.tsx +++ b/src/components/ChatRoomScreen/MessagesList.tsx @@ -79,9 +79,11 @@ const MessagesList: React.FC = ({ messages }) => { return ( {messages.map((message: any) => ( - - {message.content} - {moment(message.createdAt).format('HH:mm')} + + {message.content} + + {moment(message.createdAt).format('HH:mm')} + ))}