From 01bc17a5c92daca4e04b18d55de4b9438291d519 Mon Sep 17 00:00:00 2001 From: Sam Chen <76010610+SamchenUF@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:18:09 -0500 Subject: [PATCH] Added 100% test coverage --- src/tests/Counter.test.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/tests/Counter.test.js b/src/tests/Counter.test.js index 36cc18aa..353dbf05 100644 --- a/src/tests/Counter.test.js +++ b/src/tests/Counter.test.js @@ -1,22 +1,32 @@ -// 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 counterHeading = screen.getByRole('heading', {name: /Counter/i}); + expect(counterHeading).toBeInTheDocument(); }); test('should render initial count with value of 0', () => { - // Complete the unit test below based on the objective in the line above + const counterCount = screen.getByTestId('count'); + expect(counterCount).toHaveTextContent('0'); }); test('clicking + increments the count', () => { - // Complete the unit test below based on the objective in the line above + const increment = screen.getByRole('button', {name: '+'}); + fireEvent.click(increment); + const counterCount = screen.getByTestId('count'); + expect(counterCount).toHaveTextContent('1'); }); test('clicking - decrements the count', () => { - // Complete the unit test below based on the objective in the line above + const increment = screen.getByRole('button', {name: '+'}); + fireEvent.click(increment); + const decrement = screen.getByRole('button', {name: '-'}); + fireEvent.click(decrement); + const counterCount = screen.getByTestId('count'); + expect(counterCount).toHaveTextContent('0'); });