Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump jest to v26 and update focus tests #1102

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 🛡 Security

- [CVE-2023-28155] Bump `jest` from `24.9.0` to `26.6.2` ([#1102](https://github.com/opensearch-project/oui/pull/1102))

### 📈 Features/Enhancements

- Update ouiTextSubduedColor in `next` dark theme ([#973](https://github.com/opensearch-project/oui/pull/973))
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@
"html": "^1.0.0",
"html-format": "^1.0.2",
"html-webpack-plugin": "^4.4.1",
"jest": "^24.1.0",
"jest-cli": "^24.1.0",
BSFishy marked this conversation as resolved.
Show resolved Hide resolved
"jest": "^26.0.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.41",
"pegjs": "^0.10.0",
Expand Down
4 changes: 3 additions & 1 deletion src/components/accordion/accordion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ describe('OuiAccordion', () => {
// click button
component.find('button').simulate('click');

expect(childWrapper).toBe(document.activeElement);
setTimeout(() => {
expect(childWrapper).toBe(document.activeElement);
}, 0);
Comment on lines +227 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the setTimeout fix the issues? I remember seeing changes about spying on the focus event, why don't we do that? It seems less flaky to me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the exact reason this changed between jsdom versions, but previously you could check for the activeElement immediately, whereas with the update you need to await an update cycle for focus changes to be applied. (There are probably other possible ways of doing this that may be better, such as jest.useFakeTimers(); and jest.advanceTimersByTime(0);). But I found that the setTimeout method worked consistently in all cases, including some which were previously skipped, with minimal changes to existing test structure.

Before I got this working, I was spying on the focus event, but that's actually worse, because it only tells you that the event was triggered, but mostly we want to test where the focus actually ends up, which is better tested via document.activeElement.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How confident are we that we are not introducing flakiness?

Have you by chance experimented with enzymejs/enzyme#2337 (comment)?

});
});
});
9 changes: 7 additions & 2 deletions src/components/code_editor/code_editor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,13 @@ describe('OuiCodeEditor', () => {
stopPropagation: () => {},
key: keys.ESCAPE,
});
const hint = findTestSubject(component, 'codeEditorHint').getDOMNode();
expect(hint).toBe(document.activeElement);
setTimeout(() => {
const hint = findTestSubject(
component,
'codeEditorHint'
).getDOMNode();
expect(hint).toBe(document.activeElement);
BSFishy marked this conversation as resolved.
Show resolved Hide resolved
}, 0);
});
});
});
Expand Down
9 changes: 6 additions & 3 deletions src/components/color_picker/color_picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ test('popover color selector is hidden and input regains focus when the ENTER ke
findTestSubject(colorPicker, 'ouiSaturation').simulate('keydown', {
key: keys.ENTER,
});
expect(
findTestSubject(colorPicker, 'ouiColorPickerAnchor').getDOMNode()
).toEqual(document.activeElement);

setTimeout(() => {
expect(
findTestSubject(colorPicker, 'ouiColorPickerAnchor').getDOMNode()
).toEqual(document.activeElement);
}, 0);
// Portal removal not working with Jest. The blur handler is called just before the portal would be removed.
expect(onBlurHandler).toBeCalled();
});
Expand Down
16 changes: 13 additions & 3 deletions src/components/color_picker/color_stops/color_stops.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,17 @@ test('thumb focus changes', () => {
wrapper.simulate('keydown', {
key: keys.ARROW_DOWN,
});
expect(thumbs.first().getDOMNode()).toEqual(document.activeElement);
setTimeout(() => {
expect(thumbs.first().getDOMNode()).toEqual(document.activeElement);
}, 0);

thumbs.first().simulate('keydown', {
key: keys.ARROW_DOWN,
});
expect(thumbs.at(1).getDOMNode()).toEqual(document.activeElement);

setTimeout(() => {
expect(thumbs.at(1).getDOMNode()).toEqual(document.activeElement);
}, 0);
});

test('thumb direction movement', () => {
Expand All @@ -447,7 +453,11 @@ test('thumb direction movement', () => {
wrapper.simulate('keydown', {
key: keys.ARROW_DOWN,
});
expect(thumbs.first().getDOMNode()).toEqual(document.activeElement);

setTimeout(() => {
expect(thumbs.first().getDOMNode()).toEqual(document.activeElement);
}, 0);

thumbs.first().simulate('keydown', {
key: keys.ARROW_RIGHT,
});
Expand Down
8 changes: 5 additions & 3 deletions src/components/combo_box/combo_box.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,11 @@ describe('behavior', () => {
);

findTestSubject(component, 'comboBoxClearButton').simulate('click');
expect(
findTestSubject(component, 'comboBoxSearchInput').getDOMNode()
).toBe(document.activeElement);
setTimeout(() => {
expect(
findTestSubject(component, 'comboBoxSearchInput').getDOMNode()
).toBe(document.activeElement);
}, 0);
});
});

Expand Down
Loading
Loading