ATProto social network
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import { describe, it, expect } from 'vitest';import { render, screen } from '@testing-library/svelte';import { createRawSnippet } from 'svelte';import Button from './Button.svelte';
function textSnippet(text: string) { return createRawSnippet(() => ({ render: () => `<span>${text}</span>` }));}
describe('Button', () => { it('renders a button element by default', () => { render(Button, { props: { children: textSnippet('Click me') } }); const btn = screen.getByTestId('button'); expect(btn.tagName).toBe('BUTTON'); expect(btn.textContent).toContain('Click me'); });
it('renders an anchor when href is provided', () => { render(Button, { props: { href: '/test', children: textSnippet('Link') } }); const el = screen.getByTestId('button'); expect(el.tagName).toBe('A'); expect(el).toHaveAttribute('href', '/test'); });
it('renders a button (not anchor) when disabled even with href', () => { render(Button, { props: { href: '/test', disabled: true, children: textSnippet('Disabled') } }); const el = screen.getByTestId('button'); expect(el.tagName).toBe('BUTTON'); expect(el).toBeDisabled(); });
it('applies variant classes', () => { render(Button, { props: { variant: 'accent', children: textSnippet('Accent') } }); const el = screen.getByTestId('button'); expect(el.className).toContain('btn-accent'); });
it('applies size classes', () => { render(Button, { props: { size: 'lg', children: textSnippet('Large') } }); const el = screen.getByTestId('button'); expect(el.className).toContain('btn-lg'); });
it('disables button when loading', () => { render(Button, { props: { loading: true, children: textSnippet('Loading') } }); const el = screen.getByTestId('button'); expect(el).toBeDisabled(); });
it('shows spinner when loading', () => { render(Button, { props: { loading: true, children: textSnippet('Loading') } }); const spinner = document.querySelector('.loading-spinner'); expect(spinner).not.toBeNull(); });});