|
| 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 | +}); |
0 commit comments