From e3e5fc79300683a4810d571e772fba084948ea6d Mon Sep 17 00:00:00 2001 From: khaidao000 Date: Tue, 9 Apr 2024 20:16:47 -0500 Subject: [PATCH] Fix tests to ensure 100% coverage --- src/tests/Counter.test.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/tests/Counter.test.js b/src/tests/Counter.test.js index 36cc18aa..b47ecacf 100644 --- a/src/tests/Counter.test.js +++ b/src/tests/Counter.test.js @@ -1,22 +1,28 @@ -// import necessary react testing library helpers here -// import the Counter component here +import { render, screen, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import Counter from '../components/Counter'; beforeEach(() => { - // Render the Counter component here -}) - -test('renders counter message', () => { - // Complete the unit test below based on the objective in the line above + // Render the Counter component before each test + render(); }); -test('should render initial count with value of 0', () => { - // Complete the unit test below based on the objective in the line above +test('renders initial count', () => { + // This test checks if the initial count (0) is rendered + const countElement = screen.getByTestId('count'); + expect(countElement).toHaveTextContent("0"); }); test('clicking + increments the count', () => { - // Complete the unit test below based on the objective in the line above + // This simulates a click on the "+" button and checks if the count increments + fireEvent.click(screen.getByText('+')); + const countElement = screen.getByTestId('count'); + expect(countElement).toHaveTextContent("1"); }); test('clicking - decrements the count', () => { - // Complete the unit test below based on the objective in the line above + fireEvent.click(screen.getByText('+')); // Increment to 1 + fireEvent.click(screen.getByText('-')); // Decrement back to 0 + const countElement = screen.getByTestId('count'); + expect(countElement).toHaveTextContent("0"); });