Skip to content

Commit

Permalink
Add tests for generic components. Use queryAll instead of getAll to m…
Browse files Browse the repository at this point in the history
…aintain consitency with 0 element queries.
  • Loading branch information
wnederhof committed Mar 13, 2024
1 parent 1123dbd commit d8f1a13
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Actions } from '@components/Actions'
import { render, screen } from '@testing-library/react'

describe('Actions', () => {
it('renders the provided action', () => {
render(
<Actions>
<Actions.Action href="https://www.example.com">Do it</Actions.Action>
</Actions>
)
expect(screen.queryAllByText('Do it')).toHaveLength(1)
expect(screen.getByRole('link')).toHaveAttribute(
'href',
'https://www.example.com'
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Details } from '@components/Details'
import { render, screen } from '@testing-library/react'

describe('Details', () => {
it('renders the provided details', () => {
render(
<Details>
<Details.Field label="Field">Value</Details.Field>
</Details>
)
expect(screen.queryAllByText('Field')).toHaveLength(1)
expect(screen.queryAllByText('Value')).toHaveLength(1)
})
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { Error } from '@components/Error'
import { render } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { CombinedError } from 'urql'

describe('Error', () => {
it('renders without errors', () => {
render(<Error value="some error" />)
it('renders the provided error message if the value is a string', () => {
render(<Error value="Error message" />)
expect(screen.queryAllByText('Error message')).toHaveLength(1)
})

it('renders the provided error message if the value is a combined error', () => {
const combinedError = new CombinedError({
graphQLErrors: ['Error message'],
})
render(<Error value={combinedError} />)
expect(screen.queryAllByText('[GraphQL] Error message')).toHaveLength(1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Form } from '@components/Form'
import { render, screen, within } from '@testing-library/react'

describe('Form', () => {
it('renders form with fields', () => {
render(
<Form>
<Form.Field label="Field">
<Form.Text />
</Form.Field>
</Form>
)
expect(screen.queryAllByText('Field')).toHaveLength(1)
expect(screen.queryAllByRole('textbox')).toHaveLength(1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { List } from '@components/List'
import { render, screen, within } from '@testing-library/react'

describe('List', () => {
it('renders the provided list', () => {
render(
<List
fields={[{ name: 'field', label: 'Field' }]}
data={[{ field: 'Value' }]}
actions={() => ({})}
/>
)
expect(screen.queryAllByText('Field')).toHaveLength(1)
expect(screen.queryAllByText('Value')).toHaveLength(1)
})

it('does not render actions if not provided', () => {
render(
<List
fields={[{ name: 'field', label: 'Field' }]}
data={[{ field: 'Value' }]}
actions={() => ({})}
/>
)
expect(screen.queryAllByText('Show')).toHaveLength(0)
expect(screen.queryAllByText('Edit')).toHaveLength(0)
expect(screen.queryAllByText('Delete')).toHaveLength(0)
})

it('renders the provided actions', () => {
let clicked = false
render(
<List
fields={[{ name: 'field', label: 'Field' }]}
data={[{ field: 'Value' }]}
actions={() => ({
showHref: 'https://www.example1.com',
editHref: 'https://www.example2.com',
onDeleteClick: () => {
clicked = true
},
})}
/>
)
expect(screen.queryAllByText('Show')).toHaveLength(1)
expect(screen.queryAllByText('Edit')).toHaveLength(1)
expect(screen.queryAllByText('Delete')).toHaveLength(1)

expect(screen.getByText('Show')).toHaveAttribute(
'href',
'https://www.example1.com'
)

expect(screen.getByText('Edit')).toHaveAttribute(
'href',
'https://www.example2.com'
)

screen.getByText('Delete').click()

expect(clicked).toBeTruthy()
})
})

0 comments on commit d8f1a13

Please sign in to comment.