Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions frontend/public/components/__tests__/edit-yaml.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { renderHook } from '@testing-library/react';
import { useRef } from 'react';
import type { CodeEditorRef } from '../../module/k8s';

describe('EditYAML: getEditor function', () => {
it('should handle undefined monacoRef.current without throwing TypeError', () => {
// This test verifies the fix for OCPBUGS-77912
// The bug occurred when monacoRef.current was undefined and the 'in' operator was used
const { result } = renderHook(() => useRef<CodeEditorRef>());
const monacoRef = result.current;

// This simulates the getEditor function from edit-yaml.tsx
const getEditor = () =>
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

// Before the fix, this would throw: "TypeError: Cannot use 'in' operator to search for 'editor' in undefined"
// After the fix, it should return undefined gracefully
expect(() => getEditor()).not.toThrow();
expect(getEditor()).toBeUndefined();
});

it('should return undefined when monacoRef.current exists but has no editor property', () => {
const { result } = renderHook(() => useRef<CodeEditorRef>());
const monacoRef = result.current;

// Set monacoRef.current to an object without an 'editor' property
monacoRef.current = {} as CodeEditorRef;

const getEditor = () =>
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

expect(getEditor()).toBeUndefined();
});

it('should return the editor when monacoRef.current has an editor property', () => {
const { result } = renderHook(() => useRef<CodeEditorRef>());
const monacoRef = result.current;

const mockEditor = { getValue: jest.fn(), setValue: jest.fn() };
// Set monacoRef.current to an object with an 'editor' property
monacoRef.current = { editor: mockEditor } as any;

const getEditor = () =>
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

expect(getEditor()).toBe(mockEditor);
});
});
2 changes: 1 addition & 1 deletion frontend/public/components/edit-yaml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const EditYAMLInner: FC<EditYAMLInnerProps> = (props) => {
const { t } = useTranslation();

const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
'editor' in monacoRef?.current ? monacoRef.current.editor : undefined;
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

const getModel = useCallback(
(obj) => {
Expand Down