import React from 'react' import { render, screen } from '@testing-library/react' import { GenericButton, type ButtonFormAttributes } from './GenericButton' describe('', () => { it('renders the custom component element when passed the component prop', () => { render( (
I'm custom
{props.children}
)} />, ) expect(screen.getByText("I'm custom")).toBeInTheDocument() expect(screen.getByText('button label')).toBeInTheDocument() }) it('renders an anchor element when passed the href prop and a falsy disabled prop and a falsy working prop', () => { render() expect(screen.getByRole('link', { name: 'My link' })).toBeInTheDocument() }) it('does not render an anchor element when passed the href prop but has a truthy disabled prop or a truthy working prop', () => { const { rerender } = render( , ) expect(screen.queryByRole('link', { name: 'My link' })).not.toBeInTheDocument() expect(screen.getByRole('button', { name: 'My link' })).toBeInTheDocument() rerender( , ) expect(screen.queryByRole('link', { name: 'My link working' })).not.toBeInTheDocument() expect(screen.getByRole('button', { name: 'My link working' })).toBeInTheDocument() }) it('renders a button element when not passed a component prop or when not passed an href and a falsy disabled prop and a falsy working prop', () => { render() expect(screen.getByRole('button', { name: 'My button' })).toBeInTheDocument() }) it('passes custom props to a custom component', () => { render(
} />, ) expect(screen.getByTestId('custom-prop')).toBeInTheDocument() }) it('passes custom props to a link', () => { render() expect(screen.getByTestId('custom-prop')).toBeInTheDocument() }) it('passes custom props to a button', () => { render() expect(screen.getByTestId('custom-prop')).toBeInTheDocument() }) }) describe(' with native HTML `form` attributes', () => { const buttonFormAttributes = { form: 'linked-form-id', formAction: '/', formMethod: 'post', formEncType: 'text/plain', formTarget: '_self', formNoValidate: false, } satisfies ButtonFormAttributes it('will pass form props into the button', () => { render( , ) expect(screen.getByRole('button', { name: 'submit button' })).toHaveAttribute( 'form', buttonFormAttributes.form, ) }) }) describe(' `working` accessible states', () => { it('renders a button without aria-live by default', () => { const { getByTestId } = render( , ) const button = getByTestId('id--generic-test') // The id is passed to the element not the container so we have to get its parent const buttonContainer = button.parentElement expect(buttonContainer).not.toHaveAttribute('aria-live', '') }) it('renders a button with aria-live if working label if provided', () => { const { getByTestId } = render( , ) const button = getByTestId('id--generic-test') const buttonContainer = button.parentElement expect(buttonContainer).toHaveAttribute('aria-live', 'polite') }) it('renders a link button with aria-live if working label if provided', () => { const { getByTestId } = render( , ) const button = getByTestId('id--generic-test') const buttonContainer = button.parentElement expect(buttonContainer).toHaveAttribute('aria-live', 'polite') }) it('renders a custom button with aria-live if working label if provided', () => { const { getByTestId } = render( ( )} />, ) const button = getByTestId('id--generic-test') const buttonContainer = button.parentElement expect(buttonContainer).toHaveAttribute('aria-live', 'polite') }) })