From 9cb1a0c7395a7e15c754604eb4d6cf1c8b76293c Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Wed, 2 Oct 2024 15:38:16 -0500 Subject: [PATCH] Accident counter --- .../accident-counter/src/counter.test.jsx | 91 +++++++++++++++---- examples/accident-counter/src/counter.tsx | 4 +- .../accident-counter/tests/counter.spec.js | 2 - 3 files changed, 76 insertions(+), 21 deletions(-) delete mode 100644 examples/accident-counter/tests/counter.spec.js diff --git a/examples/accident-counter/src/counter.test.jsx b/examples/accident-counter/src/counter.test.jsx index 4fe79f8..097c366 100644 --- a/examples/accident-counter/src/counter.test.jsx +++ b/examples/accident-counter/src/counter.test.jsx @@ -1,34 +1,82 @@ -import { render, screen } from '@testing-library/react'; +import { render, screen, act } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { Counter } from './counter'; import '@testing-library/jest-dom/vitest'; -describe.todo('Counter ', () => { - beforeEach(() => { +describe('Counter ', () => { + it('renders with an initial count of 0', () => { render(); + const counter = screen.getByTestId('counter-count'); + expect(counter).toHaveTextContent('0'); }); - it('renders with an initial count of 0'); + it('disables the "Decrement" and "Reset" buttons when the count is 0', () => { + render(); + const decrementButton = screen.getByRole('button', { name: /decrement/i }); + const resetButton = screen.getByRole('button', { name: /reset/i }); - it('disables the "Decrement" and "Reset" buttons when the count is 0'); + expect(decrementButton).toBeDisabled(); + expect(resetButton).toBeDisabled(); + }); - it.todo('displays "days" when the count is 0', () => {}); + it('displays "days" when the count is 0', () => { + render(); + const unit = screen.getByTestId('counter-unit'); + expect(unit).toHaveTextContent('days'); + }); - it.todo( - 'increments the count when the "Increment" button is clicked', - async () => {}, - ); + it('increments the count when the "Increment" button is clicked', async () => { + render(); + const incrementButton = screen.getByRole('button', { name: /increment/i }); + const counter = screen.getByTestId('counter-count'); - it.todo('displays "day" when the count is 1', async () => {}); + await act(async () => { + await userEvent.click(incrementButton); + }); - it.todo( - 'decrements the count when the "Decrement" button is clicked', - async () => {}, - ); + expect(counter).toHaveTextContent('1'); + }); - it.todo('does not allow decrementing below 0', async () => {}); + it('displays "day" when the count is 1', async () => { + render(); + const incrementButton = screen.getByRole('button', { name: /increment/i }); + const unit = screen.getByTestId('counter-unit'); + + await act(async () => { + await userEvent.click(incrementButton); + }); + + expect(unit).toHaveTextContent('day'); + }); + + it('decrements the count when the "Decrement" button is clicked', async () => { + render(); + const decrementButton = screen.getByRole('button', { name: /decrement/i }); + const count = screen.getByTestId('counter-count'); + + expect(decrementButton).not.toBeDisabled(); + + await act(async () => { + await userEvent.click(decrementButton); + }); + + expect(count).toHaveTextContent('0'); + expect(decrementButton).toBeDisabled(); + }); + + it('does not allow decrementing below 0', async () => { + render(); + const decrementButton = screen.getByRole('button', { name: /decrement/i }); + const count = screen.getByTestId('counter-count'); + + await act(async () => { + await userEvent.click(decrementButton); + }); + + expect(count).toHaveTextContent('0'); + }); it.todo( 'resets the count when the "Reset" button is clicked', @@ -40,5 +88,14 @@ describe.todo('Counter ', () => { () => {}, ); - it.todo('updates the document title based on the count', async () => {}); + it('updates the document title based on the count', async () => { + const { getByRole } = render(); + const incrementButton = getByRole('button', { name: /increment/i }); + + await act(async () => { + await userEvent.click(incrementButton); + }); + + expect(document.title).toEqual(expect.stringContaining('1 day')); + }); }); diff --git a/examples/accident-counter/src/counter.tsx b/examples/accident-counter/src/counter.tsx index 7998413..7aa6179 100644 --- a/examples/accident-counter/src/counter.tsx +++ b/examples/accident-counter/src/counter.tsx @@ -2,8 +2,8 @@ import React from 'react'; import { useReducer, useEffect } from 'react'; import { reducer } from './reducer'; -export const Counter = () => { - const [state, dispatch] = useReducer(reducer, { count: 0 }); +export const Counter = ({ initialCount = 0 }) => { + const [state, dispatch] = useReducer(reducer, { count: initialCount }); const unit = state.count === 1 ? 'day' : 'days'; useEffect(() => { diff --git a/examples/accident-counter/tests/counter.spec.js b/examples/accident-counter/tests/counter.spec.js deleted file mode 100644 index e924d05..0000000 --- a/examples/accident-counter/tests/counter.spec.js +++ /dev/null @@ -1,2 +0,0 @@ -import { test, expect } from '@playwright/test'; -import { createServer } from 'vite';