Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
test: refactor to remove querySelector calls
Browse files Browse the repository at this point in the history
  • Loading branch information
codyarose committed Jan 14, 2021
1 parent 758daba commit 4860ebb
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 80 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/medications/ViewMedication.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ describe('View Medication', () => {
})

it('should not display the canceled date if the medication is not canceled', async () => {
const { container } = setup({} as Medication, [Permissions.ViewMedication])
setup({} as Medication, [Permissions.ViewMedication])

await waitFor(() => {
expect(container.querySelector('.canceled-on')).not.toBeInTheDocument()
})
expect(
screen.queryByRole('heading', { name: /medications\.medication\.canceledOn/i }),
).not.toBeInTheDocument()
})

it('should display the notes in the notes text field', async () => {
Expand Down
21 changes: 8 additions & 13 deletions src/__tests__/medications/search/MedicationRequestTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ describe('Medication Request Table', () => {
}

it('should render a table with the correct columns', async () => {
const { container } = setup()
setup()

await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
expect(await screen.findByRole('table')).toBeInTheDocument()

const columns = container.querySelectorAll('th')
const columns = screen.getAllByRole('columnheader')

expect(columns[0]).toHaveTextContent(/medications.medication.medication/i)
expect(columns[1]).toHaveTextContent(/medications.medication.priority/i)
Expand All @@ -54,23 +52,20 @@ describe('Medication Request Table', () => {
status: expectedSearchRequest.status,
} as Medication,
]
const { container } = setup(expectedSearchRequest, expectedMedicationRequests)
setup(expectedSearchRequest, expectedMedicationRequests)

await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
expect(await screen.findByRole('table')).toBeInTheDocument()
expect(screen.getByText(expectedSearchRequest.text)).toBeInTheDocument()
expect(screen.getByText(expectedSearchRequest.status)).toBeInTheDocument()
})

it('should navigate to the medication when the view button is clicked', async () => {
const expectedSearchRequest: MedicationSearchRequest = { text: 'someText', status: 'draft' }
const expectedMedicationRequests: Medication[] = [{ id: 'someId' } as Medication]
const { container, history } = setup(expectedSearchRequest, expectedMedicationRequests)
const { history } = setup(expectedSearchRequest, expectedMedicationRequests)

expect(await screen.findByRole('table')).toBeInTheDocument()

await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
userEvent.click(screen.getByRole('button', { name: /actions.view/i }))

expect(history.location.pathname).toBe(`/medications/${expectedMedicationRequests[0].id}`)
Expand Down
13 changes: 5 additions & 8 deletions src/__tests__/medications/search/ViewMedications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ describe('View Medications', () => {

describe('table', () => {
it('should render a table with data with the default search', async () => {
const { container } = await setup({} as Medication, [Permissions.ViewMedications])
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
setup({} as Medication, [Permissions.ViewMedications])

expect(await screen.findByRole('table')).toBeInTheDocument()
expect(screen.getByLabelText(/medications\.search/i)).toHaveDisplayValue('')
})
})
Expand All @@ -108,15 +107,13 @@ describe('View Medications', () => {
status: expectedSearchRequest.status,
} as Medication,
]
const { container } = await setup(
setup(
{ medication: expectedSearchRequest.text } as Medication,
[],
expectedMedicationRequests,
)
userEvent.type(screen.getByLabelText(/medications\.search/i), expectedSearchRequest.text)
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
expect(await screen.findByRole('table')).toBeInTheDocument()

expect(screen.getByText(expectedSearchRequest.text)).toBeInTheDocument()
})
Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/patients/care-goals/CareGoalTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ const setup = (

describe('Care Goals Tab', () => {
it('should not render add care goal button if user does not have permissions', async () => {
const { container } = setup('/patients/123/care-goals', [])

await waitForElementToBeRemoved(container.querySelector('.css-0'))
setup('/patients/123/care-goals', [])

expect(await screen.findByRole('table')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /patient.careGoal.new/i })).not.toBeInTheDocument()
})

Expand Down
18 changes: 9 additions & 9 deletions src/__tests__/patients/care-goals/CareGoalTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ describe('Care Goal Table', () => {
}

it('should render a table', async () => {
const { container } = await setup()
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
const columns = container.querySelectorAll('th')
setup()

expect(await screen.findByRole('table')).toBeInTheDocument()

const columns = screen.getAllByRole('columnheader')
expect(columns[0]).toHaveTextContent(/patient.careGoal.description/i)
expect(columns[1]).toHaveTextContent(/patient.careGoal.startDate/i)
expect(columns[2]).toHaveTextContent(/patient.careGoal.dueDate/i)
Expand All @@ -67,10 +67,10 @@ describe('Care Goal Table', () => {
})

it('should navigate to the care goal view when the view details button is clicked', async () => {
const { container, history } = await setup()
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
const { history } = await setup()

expect(await screen.findByRole('table')).toBeInTheDocument()

userEvent.click(screen.getByRole('button', { name: /actions.view/i }))
expect(history.location.pathname).toEqual(`/patients/${patient.id}/care-goals/${careGoal.id}`)
})
Expand Down
9 changes: 4 additions & 5 deletions src/__tests__/patients/care-goals/ViewCareGoals.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, waitFor } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import React from 'react'
import { Route, Router } from 'react-router-dom'
Expand Down Expand Up @@ -33,9 +33,8 @@ describe('View Care Goals', () => {
}

it('should render a care goals table', async () => {
const { container } = await setup()
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
setup()

expect(await screen.findByRole('table')).toBeInTheDocument()
})
})
6 changes: 2 additions & 4 deletions src/__tests__/patients/care-plans/CarePlanTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ describe('Care Plan Tab', () => {
})

it('should render the care plans table when on /patient/:id/care-plans', async () => {
const { container } = await setup('/patients/123/care-plans', [Permissions.ReadCarePlan])
setup('/patients/123/care-plans', [Permissions.ReadCarePlan])

await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
expect(await screen.findByRole('table')).toBeInTheDocument()
})

it('should render the care plan view when on /patient/:id/care-plans/:carePlanId', async () => {
Expand Down
7 changes: 2 additions & 5 deletions src/__tests__/patients/care-plans/CarePlanTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ describe('Care Plan Table', () => {
}

it('should render a table', async () => {
const { container } = await setup()
setup()

await screen.findByRole('table')

const columns = container.querySelectorAll('th') as any
const columns = screen.getAllByRole('columnheader')

expect(columns[0]).toHaveTextContent(/patient\.carePlan\.title/i)

expect(columns[1]).toHaveTextContent(/patient\.carePlan\.startDate/i)

expect(columns[2]).toHaveTextContent(/patient\.carePlan\.endDate/i)

expect(columns[3]).toHaveTextContent(/patient\.carePlan\.status/i)

await waitFor(() => {
Expand Down
8 changes: 3 additions & 5 deletions src/__tests__/patients/care-plans/ViewCarePlan.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, waitFor } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import React from 'react'
import { Route, Router } from 'react-router-dom'
Expand Down Expand Up @@ -35,10 +35,8 @@ describe('View Care Plan', () => {
}

it('should render the care plan title', async () => {
const { container } = await setup()
setup()

await waitFor(() => {
expect(container.querySelectorAll('div').length).toBe(4)
})
expect(await screen.findByRole('heading', { name: carePlan.title })).toBeInTheDocument()
})
})
8 changes: 3 additions & 5 deletions src/__tests__/patients/care-plans/ViewCarePlans.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, waitFor } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import React from 'react'
import { Route, Router } from 'react-router-dom'
Expand Down Expand Up @@ -37,9 +37,7 @@ describe('View Care Plans', () => {
}

it('should render a care plans table with the patient id', async () => {
const { container } = await setup()
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
setup()
expect(await screen.findByRole('table')).toBeInTheDocument()
})
})
8 changes: 3 additions & 5 deletions src/__tests__/patients/labs/Labs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, waitFor } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import React from 'react'
import { Provider } from 'react-redux'
Expand Down Expand Up @@ -67,10 +67,8 @@ describe('Labs', () => {

describe('patient labs list', () => {
it('should render patient labs', async () => {
const { container } = await setup()
await waitFor(() => {
expect(container.querySelector('table')).toBeInTheDocument()
})
setup()
expect(await screen.findByRole('table')).toBeInTheDocument()
})
})
})
6 changes: 3 additions & 3 deletions src/__tests__/patients/notes/NewNoteModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { screen, render, waitFor } from '@testing-library/react'
import { screen, render, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'

Expand Down Expand Up @@ -44,14 +44,14 @@ describe('New Note Modal', () => {

expect(cancelButton).toHaveClass('btn-danger')
expect(successButton).toHaveClass('btn-success')
expect(successButton.querySelector('svg')).toHaveAttribute('data-icon', 'plus')
expect(within(successButton).getByRole('img')).toHaveAttribute('data-icon', 'plus')
})

it('should render a notes rich text editor', () => {
setup()

expect(screen.getByRole('textbox')).toBeInTheDocument()
expect(screen.getByText('patient.note').querySelector('svg')).toHaveAttribute(
expect(within(screen.getByText('patient.note')).getByRole('img')).toHaveAttribute(
'data-icon',
'asterisk',
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ describe('Related Persons Tab', () => {
})

it('should not render a New Related Person button if the user does not have write privileges for a patient', async () => {
const { container } = setup({ permissions: [Permissions.ReadPatients] })

await waitForElementToBeRemoved(container.querySelector(`[class^='css']`))
setup({ permissions: [Permissions.ReadPatients] })

expect(await screen.findByRole('alert')).toBeInTheDocument()
expect(
screen.queryByRole('button', { name: /patient\.relatedPersons\.add/i }),
).not.toBeInTheDocument()
Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/patients/search/ViewPatientsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ describe('View Patients Table', () => {
})

it('should display no patients exist if total patient count is 0', async () => {
const { container } = setup({ queryString: '' }, [])
await waitForElementToBeRemoved(container.querySelector('.css-0'))
expect(screen.getByRole('heading', { name: /patients.noPatients/i })).toBeInTheDocument()
setup({ queryString: '' }, [])
expect(await screen.findByRole('heading', { name: /patients.noPatients/i })).toBeInTheDocument()
})

it('should render a table', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/patients/visits/AddVisitModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Add Visit Modal', () => {
it('should render a modal and within a form', () => {
setup()

expect(screen.getByRole('dialog').querySelector('form')).toBeInTheDocument()
expect(within(screen.getByRole('dialog')).getByLabelText('visit form')).toBeInTheDocument()
})

it('should call the on close function when the cancel button is clicked', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ describe('View Appointment', () => {
})

it('button toolbar empty if has only ReadAppointments permission', async () => {
const { container } = setup()
setup()

await waitFor(() => {
expect(container.querySelector(`[class^='css-']`)).not.toBeInTheDocument()
})
expect(
await screen.findByPlaceholderText(/scheduling\.appointment\.patient/i),
).toBeInTheDocument()

expect(screen.queryAllByRole('button')).toHaveLength(0)
})
Expand Down

0 comments on commit 4860ebb

Please sign in to comment.