chore: Fixed the intermittent failing browser tests#3410
Conversation
bf315d3 to
0bb123a
Compare
| const datePickerDay = result.getByTestId("input-day").element() as HTMLInputElement; | ||
| const datePickerYear = result.getByTestId("input-year").element() as HTMLInputElement; | ||
|
|
||
| expect(datePickerDay.disabled).toBe(true); | ||
| expect(datePickerYear.disabled).toBe(true); |
There was a problem hiding this comment.
How to move the locators out of the waitFor and use the locators to perform the test on. The month test is also included.
const result = render(<Component />);
const datePickerDayLoc = result.getByTestId("input-day");
const datePickerMonthLoc = result.getByTestId("input-month").getByTestId("input");
const datePickerYearLoc = result.getByTestId("input-year");
await vi.waitFor(() => {
expect(datePickerDayLoc).toBeDisabled();
expect(datePickerMonthLoc).toBeDisabled();
expect(datePickerYearLoc).toBeDisabled();
});
There was a problem hiding this comment.
@chrisolsen The actual fix for this it seems was for the month input, getting "input-month", and then getting "input" based off of that.
|
|
||
| const menuAction = result.getByTestId(`menu-action-${i}`); | ||
| expect(menuAction).toBeDefined(); | ||
| const menuAction = result.getByTestId(`menu-action-${i}`); |
There was a problem hiding this comment.
This locator can also be moved out of the loop with
const result = render(<Component />);
const menuButton = result.getByTestId("menu-button");
const menuActions = result.getByTestId(/menu-action-*/);
for (let i = 1; i <= 3; i++) {
await menuButton.click();
await menuActions.nth(i-1).click();
await vi.waitFor(() => {
expect(onAction).toHaveBeenNthCalledWith(i, {
action: `action${i}`,
});
expect(onAction).toHaveBeenCalledTimes(i);
});
}
expect(onAction).toHaveBeenCalledTimes(3);
There was a problem hiding this comment.
Didn't know about the nth() function, thanks.
| await menuAction.click(); | ||
| await menuAction.click(); | ||
|
|
||
| // Verify the correct action was triggered |
There was a problem hiding this comment.
Is there a reason the comments were removed? They do make things easier to follow
There was a problem hiding this comment.
I removed a lot of things, just assumed this wasn't necessary anymore either. I've re-added this specific comment.
| expect(actionIcon).toBeDefined(); | ||
| await vi.waitFor(() => { | ||
| expect(actionIcon.elements().length).toBe(1); | ||
| }); |
There was a problem hiding this comment.
This isn't needed, as the element() method in the next block will confirm that
the actionIcon exists.
There was a problem hiding this comment.
That's fair. I was just trying to keep it doing as many of the same things as it was previously doing as I could.
| const firstAction = result.getByTestId("first-action"); | ||
| const secondAction = result.getByTestId("second-action"); | ||
|
|
||
| // Click first action |
There was a problem hiding this comment.
These comments also make things much easier to understand why things are being done the way they are.
There was a problem hiding this comment.
I assumed firstAction.click() kind of implied "Click first action".
There was a problem hiding this comment.
The comments were initially added for clarity and removing them makes things hard to follow, so I think they all need to be added back
There was a problem hiding this comment.
@chrisolsen All comments have been restored, some remain gone though because their functionality is now gone
| @@ -40,62 +40,51 @@ describe("Radio", () => { | |||
| }; | |||
There was a problem hiding this comment.
After looking a little more into some of the Locator methods I think we could improve this even more.
it("should enable and disable radio group programmatically", async () => {
const Component = () => {
const [isDisabled, setIsDisabled] = useState(true);
const [selectedValue, setSelectedValue] = useState("");
return (
<div>
<GoabRadioGroup
name="fruits"
disabled={isDisabled}
value={selectedValue}
onChange={(e) => {
setSelectedValue(e.value);
}}
>
<GoabRadioItem name="fruits" value="apple" label="Apple" />
<GoabRadioItem name="fruits" value="banana" label="Banana" />
</GoabRadioGroup>
<GoabButton
testId="toggle-button"
onClick={() => {
setIsDisabled((value) => !value);
}}
>
{isDisabled ? "Enable" : "Disable"}
</GoabButton>
<div data-testid="selected-value">{selectedValue}</div>
</div>
);
};
const result = render(<Component />);
const radioItemsLoc = result.getByTestId(/^radio-option-.*/);
const selectedValue = result.getByTestId("selected-value");
// check that the radio items are all disabled
await vi.waitFor(() => {
const els = radioItemsLoc.all();
expect(els.length).toBe(2);
for (const item of els) {
expect(item).toBeDisabled();
}
});
// validate the selected value is blank
await vi.waitFor(() => {
expect(selectedValue.element().textContent).toBe("");
});
// enable the radio group
const toggleButton = result.getByTestId("toggle-button");
await toggleButton.click();
// validate they are now enabled
await vi.waitFor(() => {
for (const item of radioItemsLoc.all()) {
expect(item).toBeEnabled();
}
});
// click the first radio item
const appleRadioItem = radioItemsLoc.nth(0);
appleRadioItem.element().click();
// indicator of the selected value is changed
await vi.waitFor(() => {
expect(selectedValue.element().textContent).toBe("apple");
});
// revert to disabled state
await toggleButton.click();
// verify they are disabled
await vi.waitFor(() => {
for (const item of radioItemsLoc.all()) {
expect(item).toBeDisabled();
}
});
// click on a disabled item
const bananaRadioItem = radioItemsLoc.nth(1);
bananaRadioItem.element().click();
// old value is retained
await vi.waitFor(() => {
expect(selectedValue.element().textContent).toBe("apple");
});
There was a problem hiding this comment.
I put this in place. I made a small change to your code, because the click() used on the radio items near the end didn't work in your version.
|
|
||
| await userEvent.type(textareaEL, "s"); | ||
| await vi.waitFor(() => { | ||
| expect(textarea.elements().length).toBe(1); |
There was a problem hiding this comment.
This could be shortened to textarea.length
|
|
||
| const textareaEL = textarea.element() as HTMLTextAreaElement; | ||
| textareaEL.focus(); | ||
| await userEvent.type(textareaEL, "s"); |
There was a problem hiding this comment.
No need to focus if typing, so could be shortened to
await userEvent.type(textarea.element(), "s");
| const inputEL = input.element() as HTMLInputElement; | ||
| inputEL.focus(); |
There was a problem hiding this comment.
Could be shortened to input.element().focus()
| await vi.waitFor(() => { | ||
| const inputEL = input.element() as HTMLInputElement; | ||
| expect(inputEL).toBeTruthy(); | ||
| inputEL.focus(); | ||
| expect(input.elements().length).toBe(1); | ||
| }); |
There was a problem hiding this comment.
Since we are focusing on the first element below, this isn't really necessary. Since it is a selector with a testid, it should only be 0 or 1. But if you want to keep it, it can be shortened to expect(input.length).toBe(1);
0bb123a to
ccc521d
Compare
vanessatran-ddi
left a comment
There was a problem hiding this comment.
I tested on my local, it passed all the test after I clean nx and dist and re-build.
ccc521d to
c0ca42f
Compare
|
🎉 This PR is included in version 1.41.0-dev.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 4.11.0-dev.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 6.11.0-dev.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 1.11.0-dev.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 1.41.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 1.11.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 6.11.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 4.11.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 1.41.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 1.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 6.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 4.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 2.0.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 2.0.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 7.0.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 5.0.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 2.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 7.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
🎉 This PR is included in version 5.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Before (the change)
We had multiple intermittent failures in various browser tests.
Components affected:
After (the change)
Hopefully no more intermittent failures. Though I did notice some failures in
datagrid.browser.spec.tsx, but I never modified that file as it's quite different from the rest of our browser tests.Make sure that you've checked the boxes below before you submit the PR
Steps needed to test
Explanation of changes made:
datepicker.browser.spec.tsxrenders with propsto use an exact date as a value instead of basing it off today's date (which cares about timezone)browser eventto detect the element only, and then click it outside of the waitFor, this should avoid a race conditiondisabledtest, I couldn't get that to work reliable, if you have a better way let me know. Also changed it to check the actual elements, this seemed to work better.textarea.browser.spec.tsxradio.browser.spec.tsxmenu-button.browser.spec.tsxtemporary-notification-ctrl.browser.spec.tsxAlso updated the
vitestpackage to 4.0.13 (actually 4.0.18 I think), so it's the same as the other vitest packages (@vitest/browserfor example). We were getting version mismatch errors, and that could potentially cause other issues with tests.