Skip to content

Commit

Permalink
refactor: migrate to waitFor
Browse files Browse the repository at this point in the history
  • Loading branch information
jannikbuschke committed Nov 28, 2021
1 parent 11269b2 commit fe90175
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 69 deletions.
18 changes: 7 additions & 11 deletions src/auto-complete/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom/extend-expect'
import React, { ReactNode } from 'react'
import React from 'react'
import { Formik } from 'formik'
import { render, fireEvent, waitForDomChange } from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import Form from '../form/form'
import AutoComplete from './index'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -32,36 +32,32 @@ test('sets input value', async () => {
await act(async () => {
uat.focus()
fireEvent.change(uat, { target: { name: 'field', value: 'world' } })
await waitForDomChange()
await waitFor(() => expect(uat).toHaveValue('world'))
})
expect(uat).toHaveValue('world')
})

test('sets key as input to key value', async () => {
const { getByRole } = render(<TestAutoComplete />)
const uat = getByRole('combobox')
await act(async () => {
fireEvent.change(uat, { target: { name: 'field', value: '1' } })
await waitForDomChange()
await waitFor(() => expect(uat).toHaveValue('1'))
})
expect(uat).toHaveValue('1')
})

// somehow the 'sets key as input to key value' and 'reset value' tests intefere.
// we skip one of them to please the pipeline
test.skip('resets value', async () => {
test('resets value', async () => {
const { getByRole, getByTestId } = render(<TestAutoComplete />)
const uat = getByRole('combobox')
await act(async () => {
fireEvent.change(uat, { target: { name: 'field', value: 'search value' } })
await waitForDomChange()
await waitFor(() => expect(uat).toHaveValue('search value'))
})
expect(uat).toHaveValue('search value')

await act(async () => {
fireEvent.click(getByTestId('reset'))
await waitForDomChange()
await waitFor(() => expect(uat).toHaveValue('hello'))
})

expect(uat).toHaveValue('hello')
})
11 changes: 4 additions & 7 deletions src/checkbox/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { Formik } from 'formik'
import { render, fireEvent, waitForDomChange } from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import Form from '../form/form'
import Checkbox from './index'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -29,19 +29,17 @@ test('should check', async () => {
const uat = getByRole('checkbox')
await act(async () => {
fireEvent.click(uat, { target: 'field', checked: true })
await waitForDomChange()
await waitFor(() => expect(uat).toBeChecked())
})
expect(uat).toBeChecked()
})

test('should uncheck', async () => {
const { getByRole } = render(<Container initiallyChecked={true} />)
const uat = getByRole('checkbox')
await act(async () => {
fireEvent.click(uat, { target: 'field', checked: false })
await waitForDomChange()
await waitFor(() => expect(uat).not.toBeChecked())
})
expect(uat).not.toBeChecked()
})

test('should validate once per click', async () => {
Expand All @@ -54,7 +52,6 @@ test('should validate once per click', async () => {
await act(async () => {
uat.focus()
fireEvent.click(uat, { target: 'field', checked: true })
await waitForDomChange()
await waitFor(() => expect(validate).toBeCalledTimes(1))
})
expect(validate).toBeCalledTimes(1)
})
8 changes: 5 additions & 3 deletions src/field/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { Formik } from 'formik'
import { render, fireEvent, waitForDomChange } from '@testing-library/react'
import { render, fireEvent } from '@testing-library/react'
import Form from '../form/form'
import Input from '../input'
import { act } from 'react-dom/test-utils'
import { waitFor } from '@testing-library/dom'

const Container = ({ fast }: { fast: boolean }) => {
return (
Expand Down Expand Up @@ -37,8 +38,9 @@ describe('should change', () => {
const uat = await findByTestId('uat')
await act(async () => {
fireEvent.change(uat, { target: { value: 'new value' } })
await waitForDomChange()
await waitFor(async () =>
expect(await findByTestId('uat')).toHaveValue('new value'),
)
})
expect(await findByTestId('uat')).toHaveValue('new value')
})
})
23 changes: 11 additions & 12 deletions src/form-item/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ test('displays validation result', async () => {
target: { name: 'test', value: 'test' },
})
fireEvent.click(getByTestId('submit'))
await waitForDomChange()
expect(queryByText('error')).toBeInTheDocument()
await waitFor(() => expect(queryByText('error')).toBeInTheDocument())
})

test('displayes initial error', async () => {
Expand Down Expand Up @@ -92,9 +91,10 @@ test('displayes error instead of initialError after touched when showInitialErro
})
fireEvent.blur(getByTestId('input'))
fireEvent.click(getByTestId('submit'))
await waitForDomChange()
expect(queryByText('error')).toBeInTheDocument()
expect(queryByText('initialError')).not.toBeInTheDocument()
await waitFor(() => {
expect(queryByText('error')).toBeInTheDocument()
expect(queryByText('initialError')).not.toBeInTheDocument()
})
})

test('displayes initialError with error after touched when showInitialErrorAfterTouched is true', async () => {
Expand Down Expand Up @@ -124,9 +124,10 @@ test('displayes initialError with error after touched when showInitialErrorAfter
})
fireEvent.blur(getByTestId('input'))
fireEvent.click(getByTestId('submit'))
await waitForDomChange()
expect(queryByText('error')).toBeInTheDocument()
expect(queryByText('initialError')).toBeInTheDocument()
await waitFor(() => {
expect(queryByText('error')).toBeInTheDocument()
expect(queryByText('initialError')).toBeInTheDocument()
})
})

test('should not display help if no display is required', async () => {
Expand Down Expand Up @@ -193,8 +194,7 @@ test('handles changes on multiselect with array field error', async () => {
)
expect(queryByText('error')).not.toBeInTheDocument()
fireEvent.click(getByTestId('submit'))
await waitForDomChange()
expect(queryByText('error')).toBeInTheDocument()
await waitFor(() => expect(queryByText('error')).toBeInTheDocument())
})

test('displays validation result on nested input', async () => {
Expand All @@ -215,8 +215,7 @@ test('displays validation result on nested input', async () => {
})
fireEvent.blur(getByTestId('input'))
fireEvent.click(getByTestId('submit'))
await waitForDomChange()
expect(queryByText('error')).toBeInTheDocument()
await waitFor(() => expect(queryByText('error')).toBeInTheDocument())
})

test('displays validation success ', async () => {
Expand Down
17 changes: 10 additions & 7 deletions src/input/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { Formik } from 'formik'
import { render, fireEvent, waitForDomChange } from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import Form from '../form/form'
import Input from './index'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -61,9 +61,10 @@ describe('should change', () => {
const uat = await findByTestId('uat')
await act(async () => {
fireEvent.change(uat, { target: { value: 'new value' } })
await waitForDomChange()
await waitFor(async () =>
expect(await findByTestId('uat')).toHaveValue('new value'),
)
})
expect(await findByTestId('uat')).toHaveValue('new value')
})
})

Expand All @@ -77,17 +78,19 @@ test('TextArea should display default value', async () => {
const uat = await findByTestId('uat')
await act(async () => {
fireEvent.change(uat, { target: { value: 'defaultvalue' } })
await waitForDomChange()
await waitFor(async () =>
expect(await findByTestId('uat')).toHaveValue('defaultvalue'),
)
})
expect(await findByTestId('uat')).toHaveValue('defaultvalue')
})

test('Password should display default value', async () => {
const { findByTestId } = render(<PasswordContainer />)
const uat = await findByTestId('uat')
await act(async () => {
fireEvent.change(uat, { target: { value: 'defaultvalue' } })
await waitForDomChange()
await waitFor(async () =>
expect(await findByTestId('uat')).toHaveValue('defaultvalue'),
)
})
expect(await findByTestId('uat')).toHaveValue('defaultvalue')
})
7 changes: 4 additions & 3 deletions src/mentions/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { Formik } from 'formik'
import { render, fireEvent, waitForDomChange } from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import Form from '../form/form'
import Mentions from './index'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -37,8 +37,9 @@ describe('should change', () => {
const uat = await findByTestId('uat')
await act(async () => {
fireEvent.change(uat, { target: { value: 'new value' } })
await waitForDomChange()
await waitFor(async () =>
expect(await findByTestId('uat')).toHaveValue('new value'),
)
})
expect(await findByTestId('uat')).toHaveValue('new value')
})
})
4 changes: 0 additions & 4 deletions src/mentions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export const Mentions = ({
onBlur(name)
$onBlur && $onBlur(e)
}}
// onBlur={(event) => {
// onBlur(event)
// $onBlur && $onBlur(event)
// }}
{...restProps}
/>
)}
Expand Down
24 changes: 10 additions & 14 deletions src/radio/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { Formik } from 'formik'
import {
render,
fireEvent,
waitForDomChange,
waitFor,
} from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import Form from '../form/form'
import Radio from './index'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -56,10 +51,11 @@ test.skip('should change', async () => {
expect(radio2).not.toBeChecked()
await act(async () => {
fireEvent.click(radio2, { checked: true })
await waitForDomChange()
await waitFor(() => {
expect(radio2).toBeChecked()
expect(radio1).not.toBeChecked()
})
})
expect(radio2).toBeChecked()
expect(radio1).not.toBeChecked()
})

test.skip('should change', async () => {
Expand All @@ -70,10 +66,11 @@ test.skip('should change', async () => {
expect(radio2).not.toBeChecked()
await act(async () => {
fireEvent.click(radio2, { checked: true })
await waitForDomChange()
await waitFor(() => {
expect(radio2).toBeChecked()
expect(radio1).not.toBeChecked()
})
})
expect(radio2).toBeChecked()
expect(radio1).not.toBeChecked()
})

test.skip('should validate once per click', async () => {
Expand All @@ -86,7 +83,6 @@ test.skip('should validate once per click', async () => {

await act(async () => {
fireEvent.click(radio2, { checked: true })
await waitForDomChange()
await waitFor(() => expect(validate).toBeCalledTimes(1))
})
expect(validate).toBeCalledTimes(1)
})
19 changes: 11 additions & 8 deletions src/table/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import { Formik } from 'formik'
import { render, fireEvent, waitForDomChange } from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import Form from '../form/form'
import Table from './index'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -63,28 +63,31 @@ test('changes row', async () => {
const uat = getByRole('textbox')
await act(async () => {
fireEvent.change(uat, { target: { value: '1' } })
await waitForDomChange()
await waitFor(() => {
expect(uat).toHaveValue('1')
expect(getByDisplayValue('1')).toBeInTheDocument()
})
})
expect(uat).toHaveValue('1')
expect(getByDisplayValue('1')).toBeInTheDocument()
})

test('deletes row', async () => {
const { getByText, getByTestId } = render(<TestTable />)
const uat = getByTestId('remove')
await act(async () => {
fireEvent.click(uat)
await waitForDomChange()
await waitFor(() => {
expect(getByText('No Data')).toBeInTheDocument()
})
})
expect(getByText('No Data')).toBeInTheDocument()
})

test('adds row', async () => {
const { getByDisplayValue, getByTestId } = render(<TestTable />)
const uat = getByTestId('add')
await act(async () => {
fireEvent.click(uat)
await waitForDomChange()
await waitFor(() =>
expect(getByDisplayValue('new item')).toBeInTheDocument(),
)
})
expect(getByDisplayValue('new item')).toBeInTheDocument()
})

0 comments on commit fe90175

Please sign in to comment.