Skip to content

Commit

Permalink
refactor(react-18-upgrade): update react-testing-library and co
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Sep 12, 2024
1 parent 5d6d8c1 commit ab60ee0
Show file tree
Hide file tree
Showing 43 changed files with 1,858 additions and 3,766 deletions.
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@
"@dhis2/cypress-plugins": "^10.0.1",
"@reportportal/agent-js-cypress": "^5.1.3",
"@reportportal/agent-js-jest": "^5.0.6",
"@testing-library/jest-dom": "5.16.5",
"@testing-library/react": "12.1.5",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "13.5.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"cypress": "^12.8.1",
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.7",
"enzyme": "^3.11.0",
"eslint-plugin-cypress": "2.12.1",
"fake-indexeddb": "4.0.1",
"jest-extended": "^4.0.2",
Expand All @@ -51,7 +49,7 @@
"dependencies": {
"@dhis2/app-runtime": "^3.10.2",
"@dhis2/multi-calendar-dates": "^1.1.0",
"@dhis2/ui": "^9.2.0",
"@dhis2/ui": "^9.11.4",
"@dhis2/ui-forms": "7.16.3",
"@tanstack/react-query": "4.24.10",
"@tanstack/react-query-devtools": "4.24.14",
Expand Down Expand Up @@ -82,7 +80,7 @@
},
"resolutions": {
"@dhis2/multi-calendar-dates": "^1.1.0",
"@dhis2/ui": "^9.2.0",
"@dhis2/ui": "^9.11.4",
"@dhis2/app-runtime": "^3.10.2"
}
}
6 changes: 1 addition & 5 deletions src/app/app-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const enableRQDevtools = process.env.REACT_APP_ENABLE_RQ_DEVTOOLS === 'true'
export function OuterComponents({
children,
// This way we can use a different router in tests when needed
router: Router,
router: Router = HashRouter,
queryClient: customQueryClient,
}) {
const queryClient = useQueryClient()
Expand Down Expand Up @@ -60,10 +60,6 @@ export function OuterComponents({
)
}

OuterComponents.defaultProps = {
router: HashRouter,
}

OuterComponents.propTypes = {
children: PropTypes.any.isRequired,
queryClient: PropTypes.instanceOf(QueryClient).isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react'
import { MemoryRouter } from 'react-router-dom'
import { render } from '../../test-utils/render.js'
import ValidationResultsSidebar from './validation-results-sidebar.js'
import userEvent from '@testing-library/user-event'

describe('ValidationResultsSidebar', () => {
const hide = jest.fn()
Expand Down Expand Up @@ -218,11 +219,10 @@ describe('ValidationResultsSidebar', () => {
getByText('There was a problem running validation')
).toBeDefined()

fireEvent.click(getByText('Run validation again'))
await userEvent.click(getByText('Run validation again'))

await findByText('Running validation...')
await findByText('2 medium priority alerts')
expect(queryByText('There was a problem running validation')).toBeNull()
expect(getByText('2 medium priority alerts')).toBeDefined()
})
it('should show empty data', async () => {
const overrideOptions = {
Expand Down
11 changes: 6 additions & 5 deletions src/context-selection/menu-select/menu-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import PropTypes from 'prop-types'
import React from 'react'
import css from './menu-select.module.css'

export default function MenuSelect({ values, selected, dataTest, onChange }) {
export default function MenuSelect({
values,
selected,
dataTest = 'menu-select',
onChange,
}) {
return (
<div className={css.menuSelect} data-test={dataTest}>
<Menu>
Expand All @@ -21,10 +26,6 @@ export default function MenuSelect({ values, selected, dataTest, onChange }) {
)
}

MenuSelect.defaultProps = {
dataTest: 'menu-select',
}

MenuSelect.propTypes = {
values: PropTypes.arrayOf(
PropTypes.shape({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react-hooks'
import { renderHook } from '@testing-library/react'
import { useOpenState } from './use-open-state.js'

describe('OrganisationUnitTree - useOpenState', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomDataProvider } from '@dhis2/app-runtime'
import { renderHook } from '@testing-library/react-hooks'
import { renderHook } from '@testing-library/react'
import React from 'react'
import { useOrgChildren } from './use-org-children.js'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ import { useExpanded } from './use-expanded/index.js'
import { useForceReload } from './use-force-reload.js'
import { useRootOrgData } from './use-root-org-data/index.js'

// OrganisationUnitTree.defaultProps = {

// }
const OrganisationUnitTree = ({
onChange,
roots,

autoExpandLoadingError,
dataTest,
dataTest = 'dhis2-uiwidgets-orgunittree',
filter = [],
highlighted = [],
initiallyExpanded = [],
selected = [],
renderNodeLabel = defaultRenderNodeLabel,

disableSelection,
forceReload,
highlighted,
isUserDataViewFallback,
initiallyExpanded,
filter,
renderNodeLabel,
selected,
singleSelection,
suppressAlphabeticalSorting,
offlineLevels,
Expand Down Expand Up @@ -266,13 +270,4 @@ OrganisationUnitTree.propTypes = {
//idsThatShouldBeReloaded: propTypes.arrayOf(orgUnitIdPropType),
}

OrganisationUnitTree.defaultProps = {
dataTest: 'dhis2-uiwidgets-orgunittree',
filter: [],
highlighted: [],
initiallyExpanded: [],
selected: [],
renderNodeLabel: defaultRenderNodeLabel,
}

export { OrganisationUnitTree }
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { CustomDataProvider } from '@dhis2/app-runtime'
import { shallow } from 'enzyme'
// import { shallow } from 'enzyme'
import React from 'react'
import { OrganisationUnitTree } from './organisation-unit-tree.js'

describe('OrganisationUnitTree', () => {
// There is no official Enzyme React 18 (or 17) adapter and the advice is to move to RTL
// more context here: https://dev.to/wojtekmaj/enzyme-is-dead-now-what-ekl
// ToDO: migrate this test to RTL and get rid of enzyme
describe.skip('OrganisationUnitTree', () => {
const origError = console.error.bind(console)
const errorMock = jest.fn()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import i18n from '@dhis2/d2-i18n'
import PropTypes from 'prop-types'
import React from 'react'

export const RootError = ({ dataTest, error }) => (
export const RootError = ({
dataTest = 'dhis2-uiwidgets-orgunittree-error',
error,
}) => (
<div data-test={dataTest}>
{i18n.t('Error: {{ ERRORMESSAGE }}', {
ERRORMESSAGE: error,
Expand All @@ -11,10 +14,6 @@ export const RootError = ({ dataTest, error }) => (
</div>
)

RootError.defaultProps = {
dataTest: 'dhis2-uiwidgets-orgunittree-error',
}

RootError.propTypes = {
error: PropTypes.string.isRequired,
dataTest: PropTypes.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { CircularLoader } from '@dhis2-ui/loader'
import PropTypes from 'prop-types'
import React from 'react'

export const RootLoading = ({ dataTest }) => (
export const RootLoading = ({
dataTest = 'dhis2-uiwidgets-orgunittree-loading',
}) => (
<div data-test={dataTest}>
<CircularLoader small />

Expand All @@ -15,10 +17,6 @@ export const RootLoading = ({ dataTest }) => (
</div>
)

RootLoading.defaultProps = {
dataTest: 'dhis2-uiwidgets-orgunittree-loading',
}

RootLoading.propTypes = {
dataTest: PropTypes.string,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react-hooks'
import { renderHook } from '@testing-library/react'
import { useForceReload } from './use-force-reload.js'

describe('OrganisationUnitTree - useForceReload', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomDataProvider } from '@dhis2/app-runtime'
import { renderHook } from '@testing-library/react-hooks'
import { renderHook, waitFor } from '@testing-library/react'
import React from 'react'
import { useRootOrgData } from './use-root-org-data.js'

Expand Down Expand Up @@ -66,12 +66,11 @@ describe('OrganisationUnitTree - useRootOrgData', () => {
})

it('should provide the org unit data', async () => {
const { result, waitForNextUpdate } = renderHook(
() => useRootOrgData(['A0000000000']),
{ wrapper }
)
const { result } = renderHook(() => useRootOrgData(['A0000000000']), {
wrapper,
})

await waitForNextUpdate()
await waitFor(() => {})

expect(result.current).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -102,12 +101,11 @@ describe('OrganisationUnitTree - useRootOrgData', () => {
</CustomDataProvider>
)

const { result, waitForNextUpdate } = renderHook(
() => useRootOrgData(['A0000000000']),
{ wrapper: errorWrapper }
)
const { result } = renderHook(() => useRootOrgData(['A0000000000']), {
wrapper: errorWrapper,
})

await waitForNextUpdate()
await waitFor(() => {})

expect(result.current).toEqual(
expect.objectContaining({
Expand All @@ -120,12 +118,9 @@ describe('OrganisationUnitTree - useRootOrgData', () => {
})

it('should send the "isUserDataViewFallback" parameter with value "undefined"', async () => {
const { waitForNextUpdate } = renderHook(
() => useRootOrgData(['A0000000000']),
{ wrapper }
)
renderHook(() => useRootOrgData(['A0000000000']), { wrapper })

await waitForNextUpdate()
await waitFor(() => {})

expect(dataProviderData.organisationUnits).toHaveBeenCalledWith(
'read',
Expand All @@ -140,12 +135,9 @@ describe('OrganisationUnitTree - useRootOrgData', () => {

it('should send the "isUserDataViewFallback" parameter with value "true"', async () => {
const options = { isUserDataViewFallback: true }
const { waitForNextUpdate } = renderHook(
() => useRootOrgData(['A0000000000'], options),
{ wrapper }
)
renderHook(() => useRootOrgData(['A0000000000'], options), { wrapper })

await waitForNextUpdate()
await waitFor(() => {})

expect(dataProviderData.organisationUnits).toHaveBeenCalledWith(
'read',
Expand Down Expand Up @@ -176,12 +168,11 @@ describe('OrganisationUnitTree - useRootOrgData', () => {
</CustomDataProvider>
)

const { result, waitForNextUpdate } = renderHook(
() => useRootOrgData(['A0000000000']),
{ wrapper: wrapperWithoutDisplayName }
)
const { result } = renderHook(() => useRootOrgData(['A0000000000']), {
wrapper: wrapperWithoutDisplayName,
})

await waitForNextUpdate()
await waitFor(() => {})

expect(result.current).toEqual(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQueries } from '@tanstack/react-query'
import { renderHook } from '@testing-library/react-hooks'
import { renderHook } from '@testing-library/react'
import useOfflineLevels from './use-offline-levels.js'
import usePrefetchedOrganisationUnits from './use-prefetched-organisation-units.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react-hooks'
import { renderHook } from '@testing-library/react'
import {
getCurrentDate,
periodTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react-hooks'
import { renderHook } from '@testing-library/react'
import usePeriods from './use-periods.js'

jest.mock('../../shared/use-user-info/use-user-info.js', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,11 @@ describe('<CategoryComboTableBody />', () => {
}
)

const disabledInputs = result.getAllByRole((role, element) => {
return role === 'textbox' && element.disabled
})
// const disabledInputs = result.getAllByRole('textbox', {
// disabled: true,
// })
const disabledInputs =
result.container.getElementsByClassName('disabled')

expect(disabledInputs.length).toBe(8)
})
Expand Down
4 changes: 2 additions & 2 deletions src/data-workspace/data-details-sidebar/audit-log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('<AuditLog />', () => {
const { getByRole, container } = render(<AuditLog item={item} />)

// Expand
userEvent.click(container.querySelector('summary'))
await userEvent.click(container.querySelector('summary'))
await waitFor(() => {
expect(getByRole('group')).toHaveAttribute('open')
})
Expand All @@ -58,7 +58,7 @@ describe('<AuditLog />', () => {
)

// Expand and wait for data to load
userEvent.click(container.querySelector('summary'))
await userEvent.click(container.querySelector('summary'))
await waitFor(() => {
expect(getByRole('group')).toHaveAttribute('open')
expect(queryByRole('progressbar')).not.toBeInTheDocument()
Expand Down
Loading

0 comments on commit ab60ee0

Please sign in to comment.