Created
February 7, 2024 15:31
-
-
Save git2thehub/dcc1e0863fa5fc1da050dde0994f8579 to your computer and use it in GitHub Desktop.
Test 1 - 4 in React Vitest
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
import { render, screen } from '@testing-library/react'; | |
import userEvent from '@testing-library/user-event'; | |
import App from '../App'; | |
// Test 1: Write a test that checks to see if our `App` component renders without throwing an error. | |
it('App Component Renders Without Error', () => { | |
render(<App />); | |
}); | |
// Test 2: Write a test that checks if the button within the `App` component renders properly. | |
it('App Component Renders a Button', () => { | |
render(<App />); | |
const button = screen.getByRole('button', { name: 'increment counter' }); | |
expect(button).toBeDefined(); | |
}); | |
// Test 3: Write a test which checks if the counter starts at 0. | |
it('Counter Starts At 0', () => { | |
render(<App />); | |
const count = screen.getByText('0'); | |
expect(count).toBeDefined(); | |
}); | |
// Test 4: Write a test which checks if the increment button increases the count. | |
it('Clicking Increment Button Increases Counter Display', async () => { | |
render(<App />); | |
await userEvent.click( | |
screen.getByRole('button', { name: 'increment counter' }) | |
); | |
const count = screen.getByText('1'); | |
expect(count).toBeDefined(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment