-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 6.9: Test ChatRoomScreen child components
- Loading branch information
Showing
6 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
<ChatNavbar chat={chat} history={history} /> | ||
); | ||
|
||
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( | ||
<ChatNavbar chat={chat} history={history} /> | ||
); | ||
|
||
fireEvent.click(getByTestId('back-button')); | ||
|
||
await waitFor(() => expect(history.location.pathname).toEqual('/chats')); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
<MessageInput onSendMessage={onSendMessage} /> | ||
); | ||
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( | ||
<MessageInput onSendMessage={onSendMessage} /> | ||
); | ||
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)); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
<MessagesList messages={messages} /> | ||
); | ||
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters