|
| 1 | +/* eslint-disable linebreak-style */ |
| 2 | +import React from 'react'; |
| 3 | +import { Button } from '@/components/ui/button'; |
| 4 | + |
| 5 | +describe('Button Component', () => { |
| 6 | + it('renders with default variant and size', () => { |
| 7 | + cy.mount(<Button>Click me</Button>); |
| 8 | + |
| 9 | + cy.get('button') |
| 10 | + .should('have.class', 'bg-primary') |
| 11 | + .and('have.class', 'h-9') |
| 12 | + .and('have.class', 'px-4') |
| 13 | + .and('have.class', 'py-2'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('renders with different variants', () => { |
| 17 | + cy.mount( |
| 18 | + <div className='flex gap-2'> |
| 19 | + <Button variant='default'>Default</Button> |
| 20 | + <Button variant='destructive'>Destructive</Button> |
| 21 | + <Button variant='outline'>Outline</Button> |
| 22 | + <Button variant='secondary'>Secondary</Button> |
| 23 | + <Button variant='ghost'>Ghost</Button> |
| 24 | + <Button variant='link'>Link</Button> |
| 25 | + </div>, |
| 26 | + ); |
| 27 | + |
| 28 | + cy.get('button').eq(0).should('have.class', 'bg-primary'); |
| 29 | + cy.get('button').eq(1).should('have.class', 'bg-destructive'); |
| 30 | + cy.get('button').eq(2).should('have.class', 'border'); |
| 31 | + cy.get('button').eq(3).should('have.class', 'bg-secondary'); |
| 32 | + cy.get('button').eq(4).should('have.class', 'hover:bg-accent'); |
| 33 | + cy.get('button').eq(5).should('have.class', 'text-primary'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders with different sizes', () => { |
| 37 | + cy.mount( |
| 38 | + <div className='flex gap-2'> |
| 39 | + <Button size='default'>Default</Button> |
| 40 | + <Button size='sm'>Small</Button> |
| 41 | + <Button size='lg'>Large</Button> |
| 42 | + <Button size='icon'>Icon</Button> |
| 43 | + </div>, |
| 44 | + ); |
| 45 | + |
| 46 | + cy.get('button').eq(0).should('have.class', 'h-9'); |
| 47 | + cy.get('button').eq(1).should('have.class', 'h-8'); |
| 48 | + cy.get('button').eq(2).should('have.class', 'h-10'); |
| 49 | + cy.get('button').eq(3).should('have.class', 'size-9'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles disabled state', () => { |
| 53 | + cy.mount(<Button disabled>Disabled Button</Button>); |
| 54 | + |
| 55 | + cy.get('button') |
| 56 | + .should('be.disabled') |
| 57 | + .and('have.class', 'disabled:opacity-50') |
| 58 | + .and('have.class', 'disabled:pointer-events-none'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders with icon', () => { |
| 62 | + cy.mount( |
| 63 | + <Button> |
| 64 | + <svg data-testid='test-icon' /> |
| 65 | + Button with Icon |
| 66 | + </Button>, |
| 67 | + ); |
| 68 | + |
| 69 | + cy.get('button').should('have.class', 'has-[>svg]:px-3'); |
| 70 | + cy.get('[data-testid="test-icon"]').should('exist'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('applies custom className', () => { |
| 74 | + cy.mount(<Button className='custom-class'>Custom Button</Button>); |
| 75 | + |
| 76 | + cy.get('button').should('have.class', 'custom-class'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('handles click events', () => { |
| 80 | + const onClickSpy = cy.spy().as('onClickSpy'); |
| 81 | + |
| 82 | + cy.mount(<Button onClick={onClickSpy}>Click me</Button>); |
| 83 | + |
| 84 | + cy.get('button').click(); |
| 85 | + cy.get('@onClickSpy').should('have.been.calledOnce'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('renders as child component when asChild is true', () => { |
| 89 | + cy.mount( |
| 90 | + <Button asChild> |
| 91 | + <a href='#test'>Link Button</a> |
| 92 | + </Button>, |
| 93 | + ); |
| 94 | + |
| 95 | + cy.get('a') |
| 96 | + .should('have.attr', 'href', '#test') |
| 97 | + .and('have.class', 'bg-primary'); |
| 98 | + }); |
| 99 | +}); |
0 commit comments