From 0b67af3485aca07308f08fa50e5026b1e4cbb515 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Mon, 21 Mar 2022 16:38:42 +0100 Subject: [PATCH] Write tests for hooks Signed-off-by: Emma Indal Co-authored-by: Camila Belo --- plugins/techdocs-addons/src/hooks.test.ts | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/techdocs-addons/src/hooks.test.ts diff --git a/plugins/techdocs-addons/src/hooks.test.ts b/plugins/techdocs-addons/src/hooks.test.ts new file mode 100644 index 0000000000..c85d3a3d11 --- /dev/null +++ b/plugins/techdocs-addons/src/hooks.test.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2022 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 { useShadowRoot, useShadowRootElements } from './hooks'; +import { renderHook } from '@testing-library/react-hooks'; + +const mockShadowRoot = () => { + const div = document.createElement('div'); + const shadowRoot = div.attachShadow({ mode: 'open' }); + shadowRoot.innerHTML = '

Shadow DOM Mock

'; + return shadowRoot; +}; + +const shadowRoot = mockShadowRoot(); + +jest.mock('./context', () => { + return { + useTechDocsReaderPage: () => ({ shadowRoot }), + }; +}); + +describe('hooks', () => { + describe('useShadowRoot', () => { + it('should return shadow root', async () => { + const { result } = renderHook(() => useShadowRoot()); + + expect(result.current?.innerHTML).toBe(shadowRoot.innerHTML); + }); + }); + + describe('useShadowRootElements', () => { + it('should return shadow root elements based on selector', () => { + const { result } = renderHook(() => useShadowRootElements(['h1'])); + + expect(result.current).toHaveLength(1); + }); + }); +});