Skip to content

Commit 0431d2b

Browse files
Merge pull request #16221 from openshift-cherrypick-robot/cherry-pick-16184-to-release-4.20
[release-4.20] OCPBUGS-81439: Fix TypeError in OLS code import to console
2 parents 39eb908 + 1492581 commit 0431d2b

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { CodeEditorRef } from '@console/dynamic-plugin-sdk/src/extensions/console-types';
2+
3+
describe('EditYAML: getEditor function', () => {
4+
it('should handle undefined monacoRef.current without throwing TypeError', () => {
5+
// This test verifies the fix for OCPBUGS-77912
6+
// The bug occurred when monacoRef.current was undefined and the 'in' operator was used
7+
const monacoRef = { current: undefined } as React.MutableRefObject<CodeEditorRef>;
8+
9+
// This simulates the getEditor function from edit-yaml.tsx
10+
const getEditor = () =>
11+
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;
12+
13+
// Before the fix, this would throw: "TypeError: Cannot use 'in' operator to search for 'editor' in undefined"
14+
// After the fix, it should return undefined gracefully
15+
expect(() => getEditor()).not.toThrow();
16+
expect(getEditor()).toBeUndefined();
17+
});
18+
19+
it('should return undefined when monacoRef.current exists but has no editor property', () => {
20+
const monacoRef = { current: {} as CodeEditorRef } as React.MutableRefObject<CodeEditorRef>;
21+
22+
const getEditor = () =>
23+
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;
24+
25+
expect(getEditor()).toBeUndefined();
26+
});
27+
28+
it('should return the editor when monacoRef.current has an editor property', () => {
29+
const mockEditor = { getValue: jest.fn(), setValue: jest.fn() };
30+
const monacoRef = { current: { editor: mockEditor } as any } as React.MutableRefObject<
31+
CodeEditorRef
32+
>;
33+
34+
const getEditor = () =>
35+
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;
36+
37+
expect(getEditor()).toBe(mockEditor);
38+
});
39+
});

frontend/public/components/edit-yaml.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ const EditYAMLInner: React.FC<EditYAMLInnerProps> = (props) => {
221221
const { t } = useTranslation();
222222

223223
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
224-
'editor' in monacoRef?.current ? monacoRef.current.editor : undefined;
224+
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;
225225

226226
const getModel = React.useCallback(
227227
(obj) => {

0 commit comments

Comments
 (0)