Skip to content

Commit 5ec828f

Browse files
authored
chore: Update jest to v26 (#1556)
Signed-off-by: Viraj Sanghvi <[email protected]>
1 parent 725994d commit 5ec828f

File tree

18 files changed

+1405
-1973
lines changed

18 files changed

+1405
-1973
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Update css loader to 5.0.0 ([1530](https://github.com/opensearch-project/oui/pull/1530))
1313
- Fix cve issue for transitive dependency @babel/runtime-corejs2 ([1531](https://github.com/opensearch-project/oui/pull/1531))
1414
- Fix cve issue for elliptic and axios ([1549](https://github.com/opensearch-project/oui/pull/1549))
15+
- Fix cve issue for jest-cli ([1556](https://github.com/opensearch-project/oui/pull/1556))
1516

1617
### 📈 Features/Enhancements
1718
- Allow TabbedContent to preserve in dom #1500 ([#1500](https://github.com/opensearch-project/oui/pull/1500))

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"@svgr/plugin-svgo": "^8.0.1",
148148
"@types/cheerio": "^0.22.31",
149149
"@types/enzyme": "3.10.12",
150-
"@types/jest": "^24.0.6",
150+
"@types/jest": "^26.0.24",
151151
"@types/lodash": "^4.14.192",
152152
"@types/node": "^10.17.5",
153153
"@types/numeral": "2.0.2",
@@ -163,7 +163,7 @@
163163
"@typescript-eslint/parser": "^5.62.0",
164164
"autoprefixer": "^9.8.6",
165165
"babel-eslint": "^10.1.0",
166-
"babel-jest": "^24.1.0",
166+
"babel-jest": "^26.6.3",
167167
"babel-loader": "^8.1.0",
168168
"babel-plugin-add-module-exports": "^1.0.2",
169169
"babel-plugin-dynamic-import-node": "^2.3.3",
@@ -204,8 +204,8 @@
204204
"html": "^1.0.0",
205205
"html-format": "^1.0.2",
206206
"html-webpack-plugin": "^4.4.1",
207-
"jest": "^24.1.0",
208-
"jest-cli": "^24.1.0",
207+
"jest": "^26.6.3",
208+
"jest-cli": "^26.6.3",
209209
"moment": "^2.29.4",
210210
"moment-timezone": "^0.5.41",
211211
"pegjs": "^0.10.0",

scripts/jest/setup/unmount_enzyme.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jest.mock('enzyme', () => {
1414
const enzyme = jest.requireActual('enzyme');
1515
return {
1616
...enzyme,
17-
mount: (component) => {
18-
const mountedComponent = enzyme.mount(component);
17+
mount: (component, ...options) => {
18+
const mountedComponent = enzyme.mount(component, ...options);
1919
mockMountedComponents.push(mountedComponent);
2020
return mountedComponent;
2121
},

src/components/accordion/accordion.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ describe('OuiAccordion', () => {
181181
});
182182

183183
describe('behavior', () => {
184+
// Note: mounting to document because activeElement requires being part of document
185+
let container: HTMLDivElement | null;
186+
187+
beforeEach(() => {
188+
container = document.createElement('div');
189+
document.body.appendChild(container);
190+
});
191+
192+
afterEach(() => {
193+
container?.parentNode?.removeChild(container);
194+
container = null;
195+
});
196+
184197
it('opens when clicked once', () => {
185198
const component = mount(<OuiAccordion id={getId()} />);
186199

@@ -214,7 +227,9 @@ describe('OuiAccordion', () => {
214227
});
215228

216229
it('moves focus to the content when expanded', () => {
217-
const component = mount<OuiAccordion>(<OuiAccordion id={getId()} />);
230+
const component = mount<OuiAccordion>(<OuiAccordion id={getId()} />, {
231+
attachTo: container,
232+
});
218233
const accordionClass = component.instance();
219234
const childWrapper = accordionClass.childWrapper;
220235

src/components/code_editor/code_editor.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,18 @@ describe('OuiCodeEditor', () => {
7777
});
7878

7979
describe('behavior', () => {
80+
let container: HTMLDivElement | null;
8081
let component: ReactWrapper;
8182

8283
beforeEach(() => {
83-
component = mount(<OuiCodeEditor />);
84+
container = document.createElement('div');
85+
document.body.appendChild(container);
86+
component = mount(<OuiCodeEditor />, { attachTo: container });
87+
});
88+
89+
afterEach(() => {
90+
container?.parentNode?.removeChild(container);
91+
container = null;
8492
});
8593

8694
describe('hint element', () => {

src/components/color_picker/color_picker.test.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,21 @@ test('popover color selector is hidden and input regains focus when the ENTER ke
232232
/>
233233
);
234234

235+
// Get the input element and spy on its focus method
236+
const inputElement = findTestSubject(
237+
colorPicker,
238+
'ouiColorPickerAnchor'
239+
).getDOMNode() as HTMLInputElement;
240+
const focusSpy = jest.spyOn(inputElement, 'focus');
241+
235242
findTestSubject(colorPicker, 'ouiColorPickerAnchor').simulate('click');
236243
findTestSubject(colorPicker, 'ouiSaturation').simulate('keydown', {
237244
key: keys.ENTER,
238245
});
239-
expect(
240-
findTestSubject(colorPicker, 'ouiColorPickerAnchor').getDOMNode()
241-
).toEqual(document.activeElement);
246+
247+
// Check if focus was called instead of checking document.activeElement
248+
expect(focusSpy).toHaveBeenCalled();
249+
242250
// Portal removal not working with Jest. The blur handler is called just before the portal would be removed.
243251
expect(onBlurHandler).toBeCalled();
244252
});

src/components/color_picker/color_stops/color_stops.test.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ const colorStopsArray = [
5353
{ stop: 35, color: '#0000FF' },
5454
];
5555

56+
// Note: providing container to optionally mount to document
57+
let container: HTMLDivElement | null;
58+
59+
beforeEach(() => {
60+
container = document.createElement('div');
61+
document.body.appendChild(container);
62+
});
63+
64+
afterEach(() => {
65+
container?.parentNode?.removeChild(container);
66+
container = null;
67+
});
68+
5669
// Note: A couple tests that would be nice, but can't be accomplished at the moment:
5770
// - Tab to bypass thumbs (tabindex="-1" not respected)
5871
// - Drag to reposition thumb (we can't get real page position info)
@@ -413,7 +426,8 @@ test('thumb focus changes', () => {
413426
min={0}
414427
max={100}
415428
{...requiredProps}
416-
/>
429+
/>,
430+
{ attachTo: container }
417431
);
418432

419433
const wrapper = findTestSubject(colorStops, 'ouiColorStops');
@@ -438,7 +452,8 @@ test('thumb direction movement', () => {
438452
min={0}
439453
max={100}
440454
{...requiredProps}
441-
/>
455+
/>,
456+
{ attachTo: container }
442457
);
443458

444459
const wrapper = findTestSubject(colorStops, 'ouiColorStops');

src/components/combo_box/combo_box.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,26 @@ describe('behavior', () => {
417417
});
418418

419419
test('focuses the input', () => {
420+
// Note: mounting to document because activeElement requires being part of document
421+
const container = document.createElement('div');
422+
document.body.appendChild(container);
423+
420424
const component = mount(
421425
<OuiComboBox
422426
options={options}
423427
selectedOptions={[options[2]]}
424428
onChange={() => {}}
425-
/>
429+
/>,
430+
{ attachTo: container }
426431
);
427432

428433
findTestSubject(component, 'comboBoxClearButton').simulate('click');
429434
expect(
430435
findTestSubject(component, 'comboBoxSearchInput').getDOMNode()
431436
).toBe(document.activeElement);
437+
438+
// Clean up
439+
document.body.removeChild(container);
432440
});
433441
});
434442

src/components/context_menu/context_menu_panel.test.tsx

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ const items = [
5353
];
5454

5555
describe('OuiContextMenuPanel', () => {
56+
// Note: mounting to document because activeElement requires being part of document
57+
let container: HTMLDivElement | null;
58+
59+
beforeEach(() => {
60+
container = document.createElement('div');
61+
document.body.appendChild(container);
62+
});
63+
64+
afterEach(() => {
65+
container?.parentNode?.removeChild(container);
66+
container = null;
67+
});
68+
5669
test('is rendered', () => {
5770
const component = render(
5871
<OuiContextMenuPanel {...requiredProps}>Hello</OuiContextMenuPanel>
@@ -187,7 +200,8 @@ describe('OuiContextMenuPanel', () => {
187200
describe('initialFocusedItemIndex', () => {
188201
it('sets focus on the item occupying that index', async () => {
189202
const component = mount(
190-
<OuiContextMenuPanel items={items} initialFocusedItemIndex={1} />
203+
<OuiContextMenuPanel items={items} initialFocusedItemIndex={1} />,
204+
{ attachTo: container }
191205
);
192206

193207
await tick(20);
@@ -199,7 +213,8 @@ describe('OuiContextMenuPanel', () => {
199213

200214
it('sets focus on the panel when set to `-1`', async () => {
201215
const component = mount(
202-
<OuiContextMenuPanel items={items} initialFocusedItemIndex={-1} />
216+
<OuiContextMenuPanel items={items} initialFocusedItemIndex={-1} />,
217+
{ attachTo: container }
203218
);
204219

205220
await tick(20);
@@ -310,23 +325,37 @@ describe('OuiContextMenuPanel', () => {
310325
</OuiContextMenuPanel>
311326
);
312327

328+
// Use the spy approach instead of checking document.activeElement
329+
const buttonElement = findTestSubject(
330+
component,
331+
'button'
332+
).getDOMNode() as HTMLButtonElement;
333+
const focusSpy = jest.spyOn(buttonElement, 'focus');
334+
313335
await tick(20);
314336

315-
expect(findTestSubject(component, 'button').getDOMNode()).toBe(
316-
document.activeElement
317-
);
337+
// Check if focus was called
338+
expect(focusSpy).toHaveBeenCalled();
318339
});
319340

320-
it('is not set on anything if hasFocus is false', () => {
341+
it('is not set on anything if hasFocus is false', async () => {
321342
const component = mount(
322343
<OuiContextMenuPanel hasFocus={false}>
323344
<button data-test-subj="button" />
324345
</OuiContextMenuPanel>
325346
);
326347

327-
expect(findTestSubject(component, 'button').getDOMNode()).not.toBe(
328-
document.activeElement
329-
);
348+
// Use the spy approach instead of checking document.activeElement
349+
const buttonElement = findTestSubject(
350+
component,
351+
'button'
352+
).getDOMNode() as HTMLButtonElement;
353+
const focusSpy = jest.spyOn(buttonElement, 'focus');
354+
355+
await tick(20);
356+
357+
// Check if focus was called
358+
expect(focusSpy).not.toHaveBeenCalled();
330359
});
331360
});
332361

@@ -344,7 +373,8 @@ describe('OuiContextMenuPanel', () => {
344373
items={items}
345374
showNextPanel={showNextPanelHandler}
346375
showPreviousPanel={showPreviousPanelHandler}
347-
/>
376+
/>,
377+
{ attachTo: container }
348378
);
349379
});
350380

0 commit comments

Comments
 (0)