|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { screen } from '@testing-library/vue'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { defineComponent } from 'vue'; |
| 5 | + |
| 6 | +import SbaButton from './sba-button.vue'; |
| 7 | + |
| 8 | +import { render } from '@/test-utils'; |
| 9 | + |
| 10 | +describe('SbaButton', () => { |
| 11 | + it('renders as a button element by default', () => { |
| 12 | + render(SbaButton, { slots: { default: 'Click me' } }); |
| 13 | + expect( |
| 14 | + screen.getByRole('button', { name: 'Click me' }), |
| 15 | + ).toBeInTheDocument(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('renders slot content', () => { |
| 19 | + render(SbaButton, { slots: { default: 'Submit' } }); |
| 20 | + expect(screen.getByText('Submit')).toBeVisible(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('renders as an anchor element when as="a"', () => { |
| 24 | + render(SbaButton, { |
| 25 | + props: { as: 'a', href: '#' }, |
| 26 | + slots: { default: 'Link' }, |
| 27 | + }); |
| 28 | + expect(screen.getByRole('link', { name: 'Link' })).toBeInTheDocument(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('sets href on anchor element', () => { |
| 32 | + render(SbaButton, { |
| 33 | + props: { as: 'a', href: 'https://example.com' }, |
| 34 | + slots: { default: 'Link' }, |
| 35 | + }); |
| 36 | + expect(screen.getByRole('link', { name: 'Link' })).toHaveAttribute( |
| 37 | + 'href', |
| 38 | + 'https://example.com', |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('sets title attribute', () => { |
| 43 | + render(SbaButton, { |
| 44 | + props: { title: 'My tooltip' }, |
| 45 | + slots: { default: 'Btn' }, |
| 46 | + }); |
| 47 | + expect(screen.getByRole('button', { name: 'Btn' })).toHaveAttribute( |
| 48 | + 'title', |
| 49 | + 'My tooltip', |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('is disabled when disabled prop is true', () => { |
| 54 | + render(SbaButton, { |
| 55 | + props: { disabled: true }, |
| 56 | + slots: { default: 'Disabled' }, |
| 57 | + }); |
| 58 | + expect(screen.getByRole('button', { name: 'Disabled' })).toBeDisabled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('is not disabled by default', () => { |
| 62 | + render(SbaButton, { slots: { default: 'Active' } }); |
| 63 | + expect(screen.getByRole('button', { name: 'Active' })).not.toBeDisabled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('emits click event when button is clicked', async () => { |
| 67 | + const { emitted } = render(SbaButton, { slots: { default: 'Click' } }); |
| 68 | + await userEvent.click(screen.getByRole('button', { name: 'Click' })); |
| 69 | + expect(emitted().click).toHaveLength(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it('does not emit click event when rendered as anchor', async () => { |
| 73 | + const { emitted } = render(SbaButton, { |
| 74 | + props: { as: 'a', href: '#' }, |
| 75 | + slots: { default: 'Link' }, |
| 76 | + }); |
| 77 | + await userEvent.click(screen.getByRole('link', { name: 'Link' })); |
| 78 | + expect(emitted().click).toBeUndefined(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('accepts a Vue component as the as prop and emits click', async () => { |
| 82 | + const StubComponent = defineComponent({ |
| 83 | + template: '<button v-bind="$attrs"><slot /></button>', |
| 84 | + }); |
| 85 | + const { emitted } = render(SbaButton, { |
| 86 | + props: { as: StubComponent }, |
| 87 | + slots: { default: 'Component' }, |
| 88 | + }); |
| 89 | + await userEvent.click(screen.getByRole('button', { name: 'Component' })); |
| 90 | + expect(emitted().click).toHaveLength(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('passes attrs through to a component passed as the as prop', () => { |
| 94 | + const StubComponent = defineComponent({ |
| 95 | + template: '<span v-bind="$attrs"><slot /></span>', |
| 96 | + }); |
| 97 | + render(SbaButton, { |
| 98 | + props: { as: StubComponent, primary: true }, |
| 99 | + slots: { default: 'Component' }, |
| 100 | + }); |
| 101 | + expect(screen.getByText('Component')).toHaveClass('is-primary'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('applies is-primary class when primary prop is true', () => { |
| 105 | + render(SbaButton, { |
| 106 | + props: { primary: true }, |
| 107 | + slots: { default: 'Primary' }, |
| 108 | + }); |
| 109 | + expect(screen.getByRole('button', { name: 'Primary' })).toHaveClass( |
| 110 | + 'is-primary', |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('does not apply is-primary class by default', () => { |
| 115 | + render(SbaButton, { slots: { default: 'Default' } }); |
| 116 | + expect(screen.getByRole('button', { name: 'Default' })).not.toHaveClass( |
| 117 | + 'is-primary', |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it.each([ |
| 122 | + ['2xs', 'px-1.5'], |
| 123 | + ['xs', 'px-2'], |
| 124 | + ['sm', 'px-3'], |
| 125 | + ['base', 'px-4'], |
| 126 | + ])('applies correct padding class for size="%s"', (size, expectedClass) => { |
| 127 | + render(SbaButton, { props: { size }, slots: { default: 'Btn' } }); |
| 128 | + expect(screen.getByRole('button', { name: 'Btn' })).toHaveClass( |
| 129 | + expectedClass, |
| 130 | + ); |
| 131 | + }); |
| 132 | +}); |
0 commit comments