Skip to content

Instantly share code, notes, and snippets.

@mariocsantos
Last active January 13, 2021 21:01
Show Gist options
  • Save mariocsantos/7ba916542633a14cd322738517e84730 to your computer and use it in GitHub Desktop.
Save mariocsantos/7ba916542633a14cd322738517e84730 to your computer and use it in GitHub Desktop.
Favorite food test mock prop
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { FavoriteFood } from '../FavoriteFood';
describe('FavoriteFood', () => {
it('should submit form', () => {
const onSubmitMock = jest.fn();
render(<FavoriteFood onSubmit={onSubmitMock} />);
const input = screen.getByLabelText(/Favorite food/i);
userEvent.type(input, 'Taco 🌮');
const button = screen.getByRole('button');
userEvent.click(button);
expect(onSubmitMock).toHaveBeenCalled();
expect(onSubmitMock).toHaveBeenCalledTimes(1);
expect(onSubmitMock).toHaveBeenCalledWith('Taco 🌮');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment