From d22ef26d7ebc81c4f32e4066344efe1462e3b5a9 Mon Sep 17 00:00:00 2001 From: Jeris Gonzales Date: Tue, 9 Apr 2024 23:15:00 -0400 Subject: [PATCH] added unit test for counter --- src/tests/Counter.test.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/tests/Counter.test.js b/src/tests/Counter.test.js index 36cc18aa..9218e514 100644 --- a/src/tests/Counter.test.js +++ b/src/tests/Counter.test.js @@ -1,22 +1,34 @@ -// import necessary react testing library helpers here -// import the Counter component here +import { render, screen, fireEvent } from '@testing-library/react'; +import Counter from '../components/Counter'; beforeEach(() => { - // Render the Counter component here -}) + render(); +}); test('renders counter message', () => { - // Complete the unit test below based on the objective in the line above + const counterMessage = screen.getByText(/Counter/i); + expect(counterMessage).toBeInTheDocument(); }); test('should render initial count with value of 0', () => { - // Complete the unit test below based on the objective in the line above + const initialCountDisplay = screen.getByText(/0/); + expect(initialCountDisplay).toBeInTheDocument(); }); test('clicking + increments the count', () => { - // Complete the unit test below based on the objective in the line above + const incrementButton = screen.getByRole('button', { name: '+' }); + fireEvent.click(incrementButton); + fireEvent.click(incrementButton); + const countDisplay = screen.getByText(/2/); + expect(countDisplay).toBeInTheDocument(); }); test('clicking - decrements the count', () => { - // Complete the unit test below based on the objective in the line above + const incrementButton = screen.getByRole('button', { name: '+' }); + fireEvent.click(incrementButton); + fireEvent.click(incrementButton); + const decrementButton = screen.getByRole('button', { name: '-' }); + fireEvent.click(decrementButton); + const countDisplay = screen.getByText(/1/); + expect(countDisplay).toBeInTheDocument(); });