Skip to content

Commit a5782ec

Browse files
committed
test: optimize test performance
React Testing Library can be slow and some tests time out on CI. It's a tradeoff between testing by user interaction/accessibility and speed. This optimizes the performance in places that I consider reasonable by - Replacing `getByRole` with `getByText` - Replacing `userEvent.type` with `userEvent.paste` testing-library/dom-testing-library#552 (comment) testing-library/dom-testing-library#820 testing-library/user-event#577
1 parent 7f79b95 commit a5782ec

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

src/Apps/Order/Routes/__tests__/Shipping.jest.tsx

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ const meWithAddresses: ShippingTestQuery$rawResponse["me"] = Object.assign(
180180
)
181181

182182
const saveAndContinue = async () => {
183-
await userEvent.click(
184-
screen.getByRole("button", { name: "Save and Continue" })
185-
)
183+
await userEvent.click(screen.getByText("Save and Continue"))
186184
await flushPromiseQueue()
187185
}
188186

@@ -686,9 +684,7 @@ describe("Shipping", () => {
686684
fillAddressFormTL(validAddress)
687685
userEvent.clear(screen.getByPlaceholderText("Street address"))
688686

689-
await userEvent.click(
690-
screen.getByRole("button", { name: "Save and Continue" })
691-
)
687+
await userEvent.click(screen.getByText("Save and Continue"))
692688

693689
expect(screen.getByText("This field is required")).toBeVisible()
694690
expect(env.mock.getAllOperations()).toHaveLength(0)
@@ -732,9 +728,7 @@ describe("Shipping", () => {
732728
)
733729

734730
fillAddressFormTL(validAddress)
735-
await userEvent.click(
736-
screen.getByRole("button", { name: "Save and Continue" })
737-
)
731+
await userEvent.click(screen.getByText("Save and Continue"))
738732

739733
const mutation = env.mock.getMostRecentOperation()
740734
expect(mutation.request.node.operation.name).toEqual(
@@ -773,9 +767,7 @@ describe("Shipping", () => {
773767

774768
fillAddressFormTL(validAddress)
775769
userEvent.selectOptions(screen.getByRole("combobox"), ["TW"])
776-
await userEvent.click(
777-
screen.getByRole("button", { name: "Save and Continue" })
778-
)
770+
await userEvent.click(screen.getByText("Save and Continue"))
779771

780772
expect(env.mock.getAllOperations()).toHaveLength(0)
781773
})
@@ -806,9 +798,7 @@ describe("Shipping", () => {
806798
)
807799

808800
fillAddressFormTL(validAddress)
809-
await userEvent.click(
810-
screen.getByRole("button", { name: "Save and Continue" })
811-
)
801+
await userEvent.click(screen.getByText("Save and Continue"))
812802

813803
expect(env.mock.getAllOperations()).toHaveLength(0)
814804
})
@@ -825,9 +815,7 @@ describe("Shipping", () => {
825815

826816
fillAddressFormTL(validAddress)
827817
userEvent.selectOptions(screen.getByRole("combobox"), ["TW"])
828-
await userEvent.click(
829-
screen.getByRole("button", { name: "Save and Continue" })
830-
)
818+
await userEvent.click(screen.getByText("Save and Continue"))
831819

832820
const mutation = env.mock.getMostRecentOperation()
833821
expect(mutation.request.node.operation.name).toEqual(
@@ -970,9 +958,7 @@ describe("Shipping", () => {
970958
relayEnv
971959
)
972960

973-
await userEvent.click(
974-
screen.getByRole("button", { name: "Save and Continue" })
975-
)
961+
await userEvent.click(screen.getByText("Save and Continue"))
976962

977963
// Address verification flow is not triggered.
978964
expect(env.mock.getAllOperations()).toHaveLength(0)
@@ -1143,7 +1129,7 @@ describe("Shipping", () => {
11431129
},
11441130
})
11451131

1146-
userEvent.click(screen.getByRole("radio", { name: /Premium/ }))
1132+
userEvent.click(screen.getByText(/^Premium/))
11471133
await saveAndContinue()
11481134

11491135
expect(mockCommitMutation).toHaveBeenCalledTimes(4)
@@ -1252,10 +1238,9 @@ describe("Shipping", () => {
12521238
},
12531239
})
12541240

1255-
userEvent.click(screen.getByRole("radio", { name: /Premium/ }))
1256-
userEvent.click(
1257-
screen.getByRole("checkbox", { name: /Save shipping address/ })
1258-
)
1241+
userEvent.click(screen.getByText(/^Premium/))
1242+
userEvent.click(screen.getByText(/^Save shipping address/))
1243+
12591244
await saveAndContinue()
12601245

12611246
expect(mockCommitMutation).toHaveBeenCalledTimes(4)
@@ -1546,9 +1531,7 @@ describe("Shipping", () => {
15461531
})
15471532

15481533
await flushPromiseQueue()
1549-
expect(
1550-
screen.getByRole("button", { name: "Save and Continue" })
1551-
).toBeEnabled()
1534+
expect(screen.getByText("Save and Continue")).toBeEnabled()
15521535

15531536
await saveAndContinue()
15541537

@@ -1601,10 +1584,8 @@ describe("Shipping", () => {
16011584
})
16021585
await flushPromiseQueue()
16031586

1604-
userEvent.click(screen.getByRole("radio", { name: /^Premium/ }))
1605-
expect(
1606-
screen.getByRole("button", { name: "Save and Continue" })
1607-
).toBeEnabled()
1587+
userEvent.click(screen.getByText(/^Premium/))
1588+
expect(screen.getByText("Save and Continue")).toBeEnabled()
16081589
await saveAndContinue()
16091590

16101591
expect(mockCommitMutation).toHaveBeenCalledTimes(2)
@@ -1744,8 +1725,8 @@ describe("Shipping", () => {
17441725
/Apt, floor, suite/
17451726
)[0]
17461727
userEvent.clear(addressLine2)
1747-
userEvent.type(addressLine2, "25th fl.")
1748-
userEvent.click(screen.getByRole("button", { name: "Save" }))
1728+
userEvent.paste(addressLine2, "25th fl.")
1729+
userEvent.click(screen.getByText("Save"))
17491730

17501731
await flushPromiseQueue()
17511732

@@ -1880,8 +1861,8 @@ describe("Shipping", () => {
18801861
/Apt, floor, suite/
18811862
)[0]
18821863
userEvent.clear(addressLine2)
1883-
userEvent.type(addressLine2, "25th fl.")
1884-
userEvent.click(screen.getByRole("button", { name: "Save" }))
1864+
userEvent.paste(addressLine2, "25th fl.")
1865+
userEvent.click(screen.getByText("Save"))
18851866

18861867
await flushPromiseQueue()
18871868

src/Components/__tests__/Utils/addressForm.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ export const fillAddressFormTL = (address: Address) => {
7373
"Add phone number including country code"
7474
)[0]
7575

76-
userEvent.type(name, address.name)
76+
userEvent.paste(name, address.name)
7777
userEvent.selectOptions(country, [address.country])
78-
userEvent.type(addressLine1, address.addressLine1)
79-
userEvent.type(addressLine2, address.addressLine2)
80-
userEvent.type(city, address.city)
81-
userEvent.type(region, address.region)
82-
userEvent.type(postalCode, address.postalCode)
83-
userEvent.type(phoneNumber, address.phoneNumber!)
78+
userEvent.paste(addressLine1, address.addressLine1)
79+
userEvent.paste(addressLine2, address.addressLine2)
80+
userEvent.paste(city, address.city)
81+
userEvent.paste(region, address.region)
82+
userEvent.paste(postalCode, address.postalCode)
83+
userEvent.paste(phoneNumber, address.phoneNumber!)
8484
}

0 commit comments

Comments
 (0)