Skip to content

Commit

Permalink
fix tests (all caused by upgrade to testing-library/user-event)
Browse files Browse the repository at this point in the history
  • Loading branch information
timrobinson33 committed Jun 4, 2022
1 parent 2ff7af9 commit 3cf87c8
Showing 1 changed file with 51 additions and 49 deletions.
100 changes: 51 additions & 49 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ export function fireChangeEvent(field, value) {
fireEvent.change(field, { target: { value } });
}

const user = userEvent.setup()

// used to set up most tests
function fillInEverything() {
async function fillInEverything() {
render(<App />)
userEvent.click(_ok())
userEvent.selectOptions(_drug(), _diamorphine())
userEvent.selectOptions(_strength(), _15mg_2ml())
await user.click(_ok())
await await user.selectOptions(_drug(), _diamorphine())
await user.selectOptions(_strength(), _15mg_2ml())
fireChangeEvent(_prescribedDose(), "25")
userEvent.selectOptions(_statDoses(), "3")
await user.selectOptions(_statDoses(), "3")
fireChangeEvent(_statDoseStrength(), "5")
}

test('happy path', () => {
test('happy path', async () => {
render(<App />)

// disclaimer
screen.getByText("Disclaimer")
expect(_drug()).toBeFalsy()
expect(_diamorphine()).toBeFalsy()
userEvent.click(_ok())
await userEvent.click(_ok())
expect(_drug()).toBeTruthy()
expect(_diamorphine()).toBeTruthy()
expect(_reset()).toBeFalsy()
Expand All @@ -46,7 +48,7 @@ test('happy path', () => {
// select a drug
expect(_strength()).toBeFalsy()
expect(_15mg_2ml()).toBeFalsy()
userEvent.selectOptions(_drug(), _diamorphine())
await user.selectOptions(_drug(), _diamorphine())
expect(_strength()).toBeTruthy()
expect(_15mg_2ml()).toBeTruthy()
expect(_reset()).toBeFalsy()
Expand All @@ -57,7 +59,7 @@ test('happy path', () => {
expect(_statDoses()).toBeFalsy()
expect(_statDoseStrength()).toBeFalsy()
expect(_reset()).toBeFalsy()
userEvent.selectOptions(_strength(), _15mg_2ml())
await user.selectOptions(_strength(), _15mg_2ml())
expect(_prescribedDose()).toBeTruthy()
expect(_statDoses()).toBeTruthy()
expect(_statDoseStrength()).toBeTruthy()
Expand All @@ -68,15 +70,15 @@ test('happy path', () => {
expect(_calculate()).toBeFalsy()
fireChangeEvent(_prescribedDose(), "25")
expect(_calculate()).toBeTruthy()
userEvent.selectOptions(_statDoses(), "3")
await user.selectOptions(_statDoses(), "3")
expect(_calculate()).toBeFalsy()
fireChangeEvent(_statDoseStrength(), "5")
expect(_calculate()).toBeTruthy()
expect(_reset()).toBeTruthy()

// click calculate
expect(_results()).toBeFalsy()
userEvent.click(_calculate())
await user.click(_calculate())
expect(_results()).toBeTruthy()
screen.getByText("Total dose (mg): 25 + (3 x 5) = 40mg")
screen.getByText("Total dose (ml): 40 ÷ 15 x 2 = 5.33ml")
Expand All @@ -87,7 +89,7 @@ test('happy path', () => {
expect(_reset()).toBeTruthy()

// click reset
userEvent.click(_reset())
await user.click(_reset())
expect(_drug()).toBeTruthy()
expect(_diamorphine()).toBeTruthy()
expect(_strength()).toBeFalsy()
Expand All @@ -100,9 +102,9 @@ test('happy path', () => {
expect(_results()).toBeFalsy()
})

test('select a blank drug -> everything clears', () => {
fillInEverything()
userEvent.selectOptions(_drug(), "0")
test('select a blank drug -> everything clears', async () => {
await fillInEverything()
await user.selectOptions(_drug(), "0")
expect(_drug()).toBeTruthy()
expect(_diamorphine()).toBeTruthy()
expect(_strength()).toBeFalsy()
Expand All @@ -115,9 +117,9 @@ test('select a blank drug -> everything clears', () => {
expect(_results()).toBeFalsy()
})

test('select a different drug -> everything clears and strength shows new values', () => {
fillInEverything()
userEvent.selectOptions(_drug(), screen.getByText("Oxycodone"))
test('select a different drug -> everything clears and strength shows new values', async () => {
await fillInEverything()
await user.selectOptions(_drug(), screen.getByText("Oxycodone"))
expect(_drug()).toBeTruthy()
expect(_diamorphine()).toBeTruthy()
expect(_strength()).toBeTruthy()
Expand All @@ -131,9 +133,9 @@ test('select a different drug -> everything clears and strength shows new values
expect(_results()).toBeFalsy()
})

test('select blank strength -> everything below clears', () => {
fillInEverything()
userEvent.selectOptions(_strength(), "0")
test('select blank strength -> everything below clears', async () => {
await fillInEverything()
await user.selectOptions(_strength(), "0")
expect(_drug()).toBeTruthy()
expect(_diamorphine()).toBeTruthy()
expect(_strength()).toBeTruthy()
Expand All @@ -146,9 +148,9 @@ test('select blank strength -> everything below clears', () => {
expect(_results()).toBeFalsy()
})

test('select different strength -> doses reset to blank', () => {
fillInEverything()
userEvent.selectOptions(_strength(), screen.getByText("10mg/1ml"))
test('select different strength -> doses reset to blank', async () => {
await fillInEverything()
await user.selectOptions(_strength(), screen.getByText("10mg/1ml"))
expect(_prescribedDose()).toHaveValue(null)
expect(_statDoses()).toHaveValue("0")
expect(_statDoseStrength()).toHaveValue(null)
Expand All @@ -157,8 +159,8 @@ test('select different strength -> doses reset to blank', () => {
expect(_results()).toBeFalsy()
})

test('prescribed dose valid and invalid values', () => {
fillInEverything()
test('prescribed dose valid and invalid values', async () => {
await fillInEverything()
fireChangeEvent(_prescribedDose(), "9.9973")
expect(_prescribedDose()).toHaveValue(9.9973)
fireChangeEvent(_prescribedDose(), "0.0004")
Expand All @@ -171,20 +173,20 @@ test('prescribed dose valid and invalid values', () => {
expect(_prescribedDose()).toHaveValue(null)
})

test('Set stat prn to zero -> clears stat dose', () => {
fillInEverything()
userEvent.selectOptions(_statDoses(), "0")
test('Set stat prn to zero -> clears stat dose', async () => {
await fillInEverything()
await user.selectOptions(_statDoses(), "0")
expect(_statDoseStrength()).toHaveValue(null)
expect(_statDoseStrength()).toBeDisabled()

// now select it again -> control is re-enabled but value still blank
userEvent.selectOptions(_statDoses(), "3")
await user.selectOptions(_statDoses(), "3")
expect(_statDoseStrength()).not.toBeDisabled()
expect(_statDoseStrength()).toHaveValue(null)
})

test('stat dose strength valid and invalid values', () => {
fillInEverything()
test('stat dose strength valid and invalid values', async () => {
await fillInEverything()
fireChangeEvent(_statDoseStrength(), "9.9973")
expect(_statDoseStrength()).toHaveValue(9.9973)
fireChangeEvent(_statDoseStrength(), "0.0004")
Expand All @@ -197,58 +199,58 @@ test('stat dose strength valid and invalid values', () => {
expect(_statDoseStrength()).toHaveValue(null)
})

test('0 treated the same as blank in prescribed dose', () => {
fillInEverything()
userEvent.selectOptions(_statDoses(), "0")
test('0 treated the same as blank in prescribed dose', async () => {
await fillInEverything()
await user.selectOptions(_statDoses(), "0")
expect(_calculate()).toBeTruthy()
fireChangeEvent(_prescribedDose(), "0")
expect(_calculate()).toBeFalsy()
})

test('0 treated the same as blank in stat dose strength', () => {
fillInEverything()
test('0 treated the same as blank in stat dose strength', async () => {
await fillInEverything()
fireChangeEvent(_prescribedDose(), "0")
expect(_calculate()).toBeTruthy()
fireChangeEvent(_statDoseStrength(), "0")
expect(_calculate()).toBeFalsy()
})

test('calculation with no stat dose', () => {
fillInEverything()
test('calculation with no stat dose', async () => {
await fillInEverything()
fireChangeEvent(_statDoses(), "0")
userEvent.click(_calculate())
await user.click(_calculate())
screen.getByText("Total dose (mg): 25 + (0 x 0) = 25mg")
screen.getByText("Total dose (ml): 25 ÷ 15 x 2 = 3.33ml")
screen.getByText("Number of vials: 2")
screen.getByText("Waste: 5mg (= 0.67ml)")
})

test('calculation with no prescribed dose', () => {
fillInEverything()
test('calculation with no prescribed dose', async () => {
await fillInEverything()
fireChangeEvent(_prescribedDose(), "")
userEvent.click(_calculate())
await user.click(_calculate())
screen.getByText("Total dose (mg): 0 + (3 x 5) = 15mg")
screen.getByText("Total dose (ml): 15 ÷ 15 x 2 = 2ml")
screen.getByText("Number of vials: 1")
screen.getByText("Waste: 0mg (= 0ml)")
})

test('micrograms', () => {
test('micrograms', async () => {
const windowAlertMock = jest.spyOn(window, "alert")
windowAlertMock.mockImplementation(() => { })
fillInEverything()
await fillInEverything()
expect(windowAlertMock).not.toHaveBeenCalled()

// as soon as you select fentanyl, the alert is shown
userEvent.selectOptions(_drug(), screen.getByText("Fentanyl"))
await user.selectOptions(_drug(), screen.getByText("Fentanyl"))
expect(windowAlertMock).toHaveBeenCalledWith(expect.stringContaining("is measured in micrograms"))

// fill in the fields and calculate
userEvent.selectOptions(_strength(), screen.getByText("100mcg/2ml"))
await user.selectOptions(_strength(), screen.getByText("100mcg/2ml"))
fireChangeEvent(_prescribedDose(), "25")
userEvent.selectOptions(_statDoses(), "3")
await user.selectOptions(_statDoses(), "3")
fireChangeEvent(_statDoseStrength(), "5")
userEvent.click(_calculate())
await user.click(_calculate())

// everything is shown in mcg
screen.getByText("Total dose (mcg): 25 + (3 x 5) = 40mcg")
Expand Down

0 comments on commit 3cf87c8

Please sign in to comment.