@@ -1,82 +1,34 @@
|
|||||||
import { render, screen, act } from '@testing-library/react';
|
import { render, screen } from '@testing-library/react';
|
||||||
import userEvent from '@testing-library/user-event';
|
import userEvent from '@testing-library/user-event';
|
||||||
|
|
||||||
import { Counter } from './counter';
|
import { Counter } from './counter';
|
||||||
|
|
||||||
import '@testing-library/jest-dom/vitest';
|
import '@testing-library/jest-dom/vitest';
|
||||||
|
|
||||||
describe('Counter ', () => {
|
describe.todo('Counter ', () => {
|
||||||
it('renders with an initial count of 0', () => {
|
beforeEach(() => {
|
||||||
render(<Counter />);
|
render(<Counter />);
|
||||||
const counter = screen.getByTestId('counter-count');
|
|
||||||
expect(counter).toHaveTextContent('0');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('disables the "Decrement" and "Reset" buttons when the count is 0', () => {
|
it('renders with an initial count of 0');
|
||||||
render(<Counter />);
|
|
||||||
const decrementButton = screen.getByRole('button', { name: /decrement/i });
|
|
||||||
const resetButton = screen.getByRole('button', { name: /reset/i });
|
|
||||||
|
|
||||||
expect(decrementButton).toBeDisabled();
|
it('disables the "Decrement" and "Reset" buttons when the count is 0');
|
||||||
expect(resetButton).toBeDisabled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('displays "days" when the count is 0', () => {
|
it.todo('displays "days" when the count is 0', () => {});
|
||||||
render(<Counter />);
|
|
||||||
const unit = screen.getByTestId('counter-unit');
|
|
||||||
expect(unit).toHaveTextContent('days');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('increments the count when the "Increment" button is clicked', async () => {
|
it.todo(
|
||||||
render(<Counter />);
|
'increments the count when the "Increment" button is clicked',
|
||||||
const incrementButton = screen.getByRole('button', { name: /increment/i });
|
async () => {},
|
||||||
const counter = screen.getByTestId('counter-count');
|
);
|
||||||
|
|
||||||
await act(async () => {
|
it.todo('displays "day" when the count is 1', async () => {});
|
||||||
await userEvent.click(incrementButton);
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(counter).toHaveTextContent('1');
|
it.todo(
|
||||||
});
|
'decrements the count when the "Decrement" button is clicked',
|
||||||
|
async () => {},
|
||||||
|
);
|
||||||
|
|
||||||
it('displays "day" when the count is 1', async () => {
|
it.todo('does not allow decrementing below 0', async () => {});
|
||||||
render(<Counter />);
|
|
||||||
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(<Counter initialCount={1} />);
|
|
||||||
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(<Counter />);
|
|
||||||
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(
|
it.todo(
|
||||||
'resets the count when the "Reset" button is clicked',
|
'resets the count when the "Reset" button is clicked',
|
||||||
@@ -88,14 +40,5 @@ describe('Counter ', () => {
|
|||||||
() => {},
|
() => {},
|
||||||
);
|
);
|
||||||
|
|
||||||
it('updates the document title based on the count', async () => {
|
it.todo('updates the document title based on the count', async () => {});
|
||||||
const { getByRole } = render(<Counter />);
|
|
||||||
const incrementButton = getByRole('button', { name: /increment/i });
|
|
||||||
|
|
||||||
await act(async () => {
|
|
||||||
await userEvent.click(incrementButton);
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(document.title).toEqual(expect.stringContaining('1 day'));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import React from 'react';
|
|||||||
import { useReducer, useEffect } from 'react';
|
import { useReducer, useEffect } from 'react';
|
||||||
import { reducer } from './reducer';
|
import { reducer } from './reducer';
|
||||||
|
|
||||||
export const Counter = ({ initialCount = 0 }) => {
|
export const Counter = () => {
|
||||||
const [state, dispatch] = useReducer(reducer, { count: initialCount });
|
const [state, dispatch] = useReducer(reducer, { count: 0 });
|
||||||
const unit = state.count === 1 ? 'day' : 'days';
|
const unit = state.count === 1 ? 'day' : 'days';
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
2
examples/accident-counter/tests/counter.spec.js
Normal file
2
examples/accident-counter/tests/counter.spec.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { createServer } from 'vite';
|
||||||
Reference in New Issue
Block a user