29
loading...
This website collects cookies to deliver better user experience
You should write tests. Tests are important.
npm install --save-dev @testing-library/react
button.spec.tsx
file we will import the React library and the Testing Library as well as the button compositions that we want to test.import React from 'react'
import { render, screen } from '@testing-library/react'
import {
PrimaryButton,
SecondaryButton,
SecondaryButtonDisabled,
PrimaryButtonDisabled,
WhiteButton,
WhiteButtonDisabled,
ButtonAsLink
} from './button.composition'
it.todo('should render a button with the class of primary')
it.todo('should render a disabled button with the class of primary')
it.todo('should render a button with the class of secondary')
it.todo('should render a disabled button with the class of secondary')
it.todo('should render a disabled button with the class of white')
it.todo('should render a disabled button with the class of secondary')
it.todo(
'should render a button as a Link, checks for href attribute and primary class'
)
it('should render a button with the class of primary', () => {
render(<PrimaryButton />)
})
screen
method followed by the getByRole
function passing in the role of blah
. We do this because we want to see what roles are available to us. It won't find a role of blah
but it will tell us the role of button
is available. This was an obvious one but sometimes you might not know what role is available to you so doing this can really help.it('should render a button with the class of primary', () => {
render(<PrimaryButton />)
const primaryButton = screen.getByRole('blah'})
})
blah
to button
. The second argument we pass in is the text that we want to test for. By passing it in as a regex instead of a string we add the i
after the word we want to test for and then we don't have to worry about capital letters.it('should render a button with the class of primary', () => {
render(<PrimaryButton />)
const primaryButton = screen.getByRole('button', { name: /primary/i })
})
primary
. We can do this by using the expect
function and passing in the button we want to test and then the class we want to test for using the toHaveClass
function.it('should render a button with the class of primary', () => {
render(<PrimaryButton />)
const primaryButton = screen.getByRole('button', { name: /primary/i })
expect(primaryButton).toHaveClass('primary')
})
bit test componentId --watch
toBeDisabled
function.it('should render a disabled button with the class of primary', () => {
render(<PrimaryButtonDisabled />)
const primaryButtonDisabled = screen.getByRole('button', {
name: /primary/i
})
expect(primaryButtonDisabled).toHaveClass('primary')
expect(primaryButtonDisabled).toBeDisabled()
})
link
which will render the button as a Link in other words as an <a>
element. We can test this by checking to see if it has the role of link
as well as if it has the href
attribute as a link with no href
won't really do much.it('should render a button as a Link, checks for href attribute and primary class', () => {
render(<ButtonAsLink />)
const buttonAsLink = screen.getByRole('link', { name: /link/i })
expect(buttonAsLink).toHaveClass('primary')
expect(buttonAsLink).toHaveAttribute('href', '/')
})