Files
orangecat/tests/unit/Button.test.tsx
CatoandClaude Opus 5 ebc9dadb24 perf(test): stop building a browser for tests that never touch one (#917)
The suite ran EVERY file in a jsdom environment. Its own numbers:

  Duration 1257s — environment 2285s, setup 465s, import 318s, tests 166s

Actual test execution was 166 seconds. The dominant term was constructing a
fake DOM for API-route, service and domain tests that never use one — 240 of
291 files.

Now node by default; the 51 files that need a DOM declare it per-file with
`// @vitest-environment jsdom`. Full suite after: 291 files, 2719 tests, 0
failures, environment 2285s -> 307s. A controlled A/B on one unchanged
directory (23 files, 177 tests) isolates the effect from machine load:
environment 178s -> 0.03s, wall 120s -> 84s.

This runs on every push (the pre-push hook) and in CI, so it is paid
constantly.

NOT environmentMatchGlobs, which is the obvious one-line version: Vitest 4
ignores it SILENTLY — no warning, no error. It ran every component test under
node and failed 20 of them, and the only reason that was caught is that a .tsx
test was checked afterwards rather than trusting the clean config load. The
config now says so, so the next person does not spend the same hour.

vitest.setup.ts already guarded its DOM access behind IS_JSDOM, which is what
made this safe to do at all.

One file needed jsdom for a non-obvious reason and the full run is what caught
it: rate-failure-cooldown.test.ts pins browser behaviour of rates.ts, which
gates its fetch on `typeof window` — the test never mentions `window`, only
the module under test does. Grepping the tests for DOM usage cannot see that;
running them can.


Claude-Session: https://claude.ai/code/session_019ns25hxd1ExZsmCJz9nNhe

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-07 05:37:30 -07:00

114 lines
3.9 KiB
TypeScript

// @vitest-environment jsdom
/**
* Button Component Tests — behavior, not class strings.
*
* Earlier this file asserted specific Tailwind class combinations like
* "bg-tiffany-500 hover:bg-tiffany-600" that the rebrand removed. The
* test failed every time anyone touched the design system. Rewritten
* 2026-06-03 to assert what consumers CARE about: variant + size
* routing produce *some* className change, clicks fire, disabled stops
* clicks, loading hides the children and shows the spinner. Now the
* design system can evolve without breaking these tests.
*/
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import Button from '@/components/ui/Button';
vi.mock('next/link', () => ({
default: function MockLink({
children,
href,
...props
}: React.PropsWithChildren<React.AnchorHTMLAttributes<HTMLAnchorElement>>) {
return (
<a href={href} {...props}>
{children}
</a>
);
},
}));
describe('Button', () => {
describe('rendering', () => {
it('renders its children as a button by default', () => {
render(<Button>Click me</Button>);
expect(screen.getByRole('button', { name: 'Click me' })).toBeInTheDocument();
});
it('renders as a link when href is provided', () => {
render(<Button href="/foo">Link button</Button>);
const link = screen.getByRole('link', { name: 'Link button' });
expect(link).toHaveAttribute('href', '/foo');
});
it('passes className through to the rendered element', () => {
render(<Button className="my-custom-class">x</Button>);
expect(screen.getByRole('button')).toHaveClass('my-custom-class');
});
it('has a stable displayName for devtools', () => {
expect(Button.displayName).toBe('Button');
});
});
describe('variant + size routing', () => {
// We don't assert specific Tailwind classes (those move with the
// design system). Instead we verify that different variant / size
// props produce DIFFERENT className strings — proves the routing
// through COMPONENT_STYLES works.
const variants = ['primary', 'secondary', 'ghost', 'danger', 'outline', 'gradient'] as const;
it.each(variants)('variant=%s produces a non-empty className', variant => {
const { container } = render(<Button variant={variant}>Variant</Button>);
const btn = container.querySelector('button');
expect(btn?.className.length).toBeGreaterThan(0);
});
const sizes = ['sm', 'md', 'lg', 'xl'] as const;
it.each(sizes)('size=%s produces a non-empty className', size => {
const { container } = render(<Button size={size}>Sized</Button>);
const btn = container.querySelector('button');
expect(btn?.className.length).toBeGreaterThan(0);
});
});
describe('interactivity', () => {
it('calls onClick when clicked', () => {
const onClick = vi.fn();
render(<Button onClick={onClick}>Click</Button>);
fireEvent.click(screen.getByRole('button'));
expect(onClick).toHaveBeenCalledTimes(1);
});
it('does not call onClick when disabled', () => {
const onClick = vi.fn();
render(
<Button onClick={onClick} disabled>
Disabled
</Button>
);
fireEvent.click(screen.getByRole('button'));
expect(onClick).not.toHaveBeenCalled();
});
it('reflects the disabled prop in the DOM', () => {
render(<Button disabled>Disabled</Button>);
expect(screen.getByRole('button')).toBeDisabled();
});
it('applies the type attribute (defaults to button)', () => {
render(<Button type="submit">Submit</Button>);
expect(screen.getByRole('button')).toHaveAttribute('type', 'submit');
});
});
describe('loading state', () => {
it('disables the button while loading', () => {
render(<Button isLoading>Loading</Button>);
expect(screen.getByRole('button')).toBeDisabled();
});
});
});