From f84401f9fa5cda900835f7674bcc67d993c9fa08 Mon Sep 17 00:00:00 2001 From: Mark Dunphy Date: Tue, 8 Apr 2025 12:43:08 -0400 Subject: [PATCH] add useEntityContextMenu test Signed-off-by: Mark Dunphy --- .../src/hooks/useEntityContextMenu.test.tsx | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 plugins/catalog-react/src/hooks/useEntityContextMenu.test.tsx diff --git a/plugins/catalog-react/src/hooks/useEntityContextMenu.test.tsx b/plugins/catalog-react/src/hooks/useEntityContextMenu.test.tsx new file mode 100644 index 0000000000..e2b45a0fc9 --- /dev/null +++ b/plugins/catalog-react/src/hooks/useEntityContextMenu.test.tsx @@ -0,0 +1,59 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import React from 'react'; +import { useEntityContextMenu } from './useEntityContextMenu'; +import { renderHook } from '@testing-library/react'; +import { + createVersionedContext, + createVersionedValueMap, +} from '@backstage/version-bridge'; + +const Context = createVersionedContext<{ + 1: { onMenuClose: () => void }; +}>('entity-context-menu-context'); + +const Provider = ({ + children, + onMenuClose, +}: { + children: React.ReactNode; + onMenuClose: () => void; +}) => ( + + {children} + +); + +describe('useEntityContextMenu', () => { + it('should throw error when used outside of provider', () => { + expect(() => { + renderHook(() => useEntityContextMenu()); + }).toThrow( + 'useEntityContextMenu must be used within an EntityContextMenuProvider', + ); + }); + + it('should return the context value', () => { + const mockOnMenuClose = jest.fn(); + const { result } = renderHook(() => useEntityContextMenu(), { + wrapper: ({ children }) => ( + {children} + ), + }); + + expect(result.current.onMenuClose).toBe(mockOnMenuClose); + }); +});