From d4564e158521d3ce2ce2a0a4d210b179e20859ba Mon Sep 17 00:00:00 2001 From: paapa-kusi <166747979+paapa-kusi@users.noreply.github.com> Date: Wed, 20 Nov 2024 01:20:27 -0500 Subject: [PATCH] Completed the unit test for the Counter component --- src/tests/Counter.test.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/tests/Counter.test.js b/src/tests/Counter.test.js index 36cc18aa..157d4270 100644 --- a/src/tests/Counter.test.js +++ b/src/tests/Counter.test.js @@ -1,22 +1,55 @@ // import necessary react testing library helpers here +import { render, screen, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; // import the Counter component here +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 + expect(screen.getByText(/Counter/i)).toBeInTheDocument(); }); test('should render initial count with value of 0', () => { // Complete the unit test below based on the objective in the line above + expect(screen.getByText(/0/i)).toBeInTheDocument(); }); test('clicking + increments the count', () => { // Complete the unit test below based on the objective in the line above + const incrementButton = screen.getByText('+'); + fireEvent.click(incrementButton); + expect(screen.getByText(/1/i)).toBeInTheDocument(); }); test('clicking - decrements the count', () => { // Complete the unit test below based on the objective in the line above + const decrementButton = screen.getByText('-'); + fireEvent.click(decrementButton); + expect(screen.getByText(/-1/i)).toBeInTheDocument(); }); + +test('clicking + multiple times increments the count', () => { + const incrementButton = screen.getByText('+'); + fireEvent.click(incrementButton); + fireEvent.click(incrementButton); + expect(screen.getByText(/2/i)).toBeInTheDocument(); +}); + +test('clicking - multiple times decrements the count', () => { + const decrementButton = screen.getByText('-'); + fireEvent.click(decrementButton); + fireEvent.click(decrementButton); + expect(screen.getByText(/-2/i)).toBeInTheDocument(); +}); + +test('renders increment and decrement buttons', () => { + expect(screen.getByText('+')).toBeInTheDocument(); + expect(screen.getByText('-')).toBeInTheDocument(); +}); + +