Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Go script generated gist.
import React from 'react'
import { render, fireEvent } from 'react-testing-library'
import CommentFeed from './CommentFeed'
// props factory to help us arrange tests for this component
const createProps = props => ({
header: 'Comment Feed',
comments: [
{
author: 'Ian Wilson',
text: 'A boats a boat but a mystery box could be anything.'
},
{
author: 'Max Powers Jr',
text: 'Krypton sucks.'
}
],
createComment: jest.fn(),
...props
})
describe('CommentFeed', () => {
/* ... */
it('allows the user to add a comment', () => {
// Arrange - create props and locate elements
const newComment = { author: 'Socrates', text: 'Why?' }
let props = createProps()
const { container, getByLabelText } = render(
<CommentFeed {...props} />
)
const authorNode = getByLabelText('Author')
const textNode = getByLabelText('Comment')
const formNode = container.querySelector('form')
// Act - simulate changes to elements
fireEvent.change(authorNode, {
target: { value: newComment.author }
})
fireEvent.change(textNode, {
target: { value: newComment.text }
})
fireEvent.submit(formNode)
// Assert - check whether the desired functions were called
expect(props.createComment).toHaveBeenCalledTimes(1)
expect(props.createComment).toHaveBeenCalledWith(newComment)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment