Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('Select component', async () => {
let optionElement
const {getByDisplayValue, getByText} = render(Select)
// Get the Select element by using the initially displayed value.
const select = getByDisplayValue('Tyrannosaurus')
expect(select.value).toBe('dino1')
// Update it by manually sending a valid option value.
await fireEvent.update(select, 'dino2')
expect(select.value).toBe('dino2')
// We can trigger an update event by directly getting the <option> element.
optionElement = getByText('Deinonychus')
await fireEvent.update(optionElement)
expect(select.value).toBe('dino3')
// ...even if option is within an </option><optgroup>.
optionElement = getByText('Diplodocus')
await fireEvent.update(optionElement)
expect(select.value).toBe('dino4')
})
</optgroup>
test('Select component', async () => {
let optionElement
const {getByDisplayValue, getByText} = render(Select)
// Get the Select element by using the initially displayed value.
const select = getByDisplayValue('Tyrannosaurus')
expect(select.value).toBe('dino1')
// Update it by manually sending a valid option value.
await fireEvent.update(select, 'dino2')
expect(select.value).toBe('dino2')
// We can trigger an update event by directly getting the <option> element.
optionElement = getByText('Deinonychus')
await fireEvent.update(optionElement)
expect(select.value).toBe('dino3')
// ...even if option is within an </option><optgroup>.
optionElement = getByText('Diplodocus')
await fireEvent.update(optionElement)
expect(select.value).toBe('dino4')
})
</optgroup>
getByText,
getByRole,
getByPlaceholderText,
emitted,
} = render(Form)
const submitButton = getByText('Submit')
// Initially the submit button should be disabled.
expect(submitButton).toBeDisabled()
const titleInput = getByLabelText(/title of the movie/i)
await fireEvent.update(titleInput, fakeReview.title)
const reviewTextarea = getByPlaceholderText('Write an awesome review')
await fireEvent.update(reviewTextarea, fakeReview.review)
// Rating Radio buttons.
const initiallySelectedInput = getByLabelText('Awful')
const ratingSelect = getByLabelText('Wonderful')
expect(initiallySelectedInput.checked).toBe(true)
expect(ratingSelect.checked).toBe(false)
await fireEvent.update(ratingSelect)
expect(ratingSelect.checked).toBe(true)
expect(initiallySelectedInput.checked).toBe(false)
// Get the Input element by its implicit ARIA role.
const recommendInput = getByRole('checkbox')
const {
getByLabelText,
getByText,
getByRole,
getByPlaceholderText,
emitted,
} = render(Form)
const submitButton = getByText('Submit')
// Initially the submit button should be disabled.
expect(submitButton).toBeDisabled()
const titleInput = getByLabelText(/title of the movie/i)
await fireEvent.update(titleInput, fakeReview.title)
const reviewTextarea = getByPlaceholderText('Write an awesome review')
await fireEvent.update(reviewTextarea, fakeReview.review)
// Rating Radio buttons.
const initiallySelectedInput = getByLabelText('Awful')
const ratingSelect = getByLabelText('Wonderful')
expect(initiallySelectedInput.checked).toBe(true)
expect(ratingSelect.checked).toBe(false)
await fireEvent.update(ratingSelect)
expect(ratingSelect.checked).toBe(true)
expect(initiallySelectedInput.checked).toBe(false)
const initiallySelectedInput = getByLabelText('Awful')
const ratingSelect = getByLabelText('Wonderful')
expect(initiallySelectedInput.checked).toBe(true)
expect(ratingSelect.checked).toBe(false)
await fireEvent.update(ratingSelect)
expect(ratingSelect.checked).toBe(true)
expect(initiallySelectedInput.checked).toBe(false)
// Get the Input element by its implicit ARIA role.
const recommendInput = getByRole('checkbox')
expect(recommendInput.checked).toBe(false)
await fireEvent.update(recommendInput)
expect(recommendInput.checked).toBe(true)
// Make sure the submit button is enabled.
expect(submitButton).toBeEnabled()
expect(submitButton).toHaveAttribute('type', 'submit')
await fireEvent.click(submitButton)
// Assert the right event has been emitted.
expect(emitted()).toHaveProperty('submit')
expect(emitted().submit[0][0]).toMatchObject(fakeReview)
})
expect(submitButton).toBeDisabled()
const titleInput = getByLabelText(/title of the movie/i)
await fireEvent.update(titleInput, fakeReview.title)
const reviewTextarea = getByPlaceholderText('Write an awesome review')
await fireEvent.update(reviewTextarea, fakeReview.review)
// Rating Radio buttons.
const initiallySelectedInput = getByLabelText('Awful')
const ratingSelect = getByLabelText('Wonderful')
expect(initiallySelectedInput.checked).toBe(true)
expect(ratingSelect.checked).toBe(false)
await fireEvent.update(ratingSelect)
expect(ratingSelect.checked).toBe(true)
expect(initiallySelectedInput.checked).toBe(false)
// Get the Input element by its implicit ARIA role.
const recommendInput = getByRole('checkbox')
expect(recommendInput.checked).toBe(false)
await fireEvent.update(recommendInput)
expect(recommendInput.checked).toBe(true)
// Make sure the submit button is enabled.
expect(submitButton).toBeEnabled()
expect(submitButton).toHaveAttribute('type', 'submit')
await fireEvent.click(submitButton)
test('fireEvent.update does not crash if non-input element is passed in', async () => {
const {getByText} = render({
template: `<div>Hi</div>`,
})
await fireEvent.update(getByText('Hi'))
expect(getByText('Hi')).toMatchInlineSnapshot(`
<div>
Hi
</div>
`)
})