From 85fe1059267216c33bdea6f18387f205cf76694a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20N=C3=A4sman?= Date: Mon, 25 Apr 2022 16:08:44 +0200 Subject: [PATCH] add test for ReportIssue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Camila Belo Co-authored-by: Eric Peterson Signed-off-by: Anders Näsman --- .../package.json | 1 + .../src/ReportIssue/ReportIssue.test.tsx | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 plugins/techdocs-module-addons-contrib/src/ReportIssue/ReportIssue.test.tsx diff --git a/plugins/techdocs-module-addons-contrib/package.json b/plugins/techdocs-module-addons-contrib/package.json index 531e68ad7a..35b6df715b 100644 --- a/plugins/techdocs-module-addons-contrib/package.json +++ b/plugins/techdocs-module-addons-contrib/package.json @@ -38,6 +38,7 @@ "@backstage/core-plugin-api": "^1.0.1", "@backstage/integration": "^1.2.0-next.0", "@backstage/integration-react": "^1.1.0-next.0", + "@backstage/plugin-techdocs-addons-test-utils": "^0.0.0", "@backstage/plugin-techdocs-react": "^0.1.1-next.0", "@backstage/theme": "^0.2.15", "@material-ui/core": "^4.9.13", diff --git a/plugins/techdocs-module-addons-contrib/src/ReportIssue/ReportIssue.test.tsx b/plugins/techdocs-module-addons-contrib/src/ReportIssue/ReportIssue.test.tsx new file mode 100644 index 0000000000..3a05f1358e --- /dev/null +++ b/plugins/techdocs-module-addons-contrib/src/ReportIssue/ReportIssue.test.tsx @@ -0,0 +1,111 @@ +/* + * 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 { TechDocsAddonBuilder } from '@backstage/plugin-techdocs-addons-test-utils'; + +import React from 'react'; +import { fireEvent, waitFor } from '@testing-library/react'; + +import { scmIntegrationsApiRef } from '@backstage/integration-react'; +import { ReportIssue } from '..'; + +const byUrl = jest.fn().mockReturnValue({ type: 'github' }); + +const fireSelectionChangeEvent = (window: Window) => { + const selectionChangeEvent = window.document.createEvent('Event'); + selectionChangeEvent.initEvent('selectionchange', true, true); + window.document.addEventListener('selectionchange', () => {}, false); + fireEvent(window.document, selectionChangeEvent); +}; + +describe('ReportIssue', () => { + const selection = { + type: 'Range', + rangeCount: 1, + isCollapsed: true, + getRangeAt: () => ({ + startContainer: 'this is a sentence', + endContainer: 'this is a sentence', + startOffset: 1, + endOffset: 3, + getBoundingClientRect: () => ({ + right: 100, + top: 100, + width: 100, + height: 100, + }), + }), + toString: () => 'his ', + containsNode: () => true, + } as unknown as Selection; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('renders without exploding', async () => { + const { shadowRoot, getByText } = + await TechDocsAddonBuilder.buildAddonsInTechDocs([ + , + ]) + .withDom( + + + +
+
+
+ + + , + ) + .withApis([[scmIntegrationsApiRef, { byUrl }]]) + .renderWithEffects(); + + (shadowRoot as ShadowRoot & Pick).getSelection = + () => selection; + + await waitFor(() => { + expect(getByText('Edit page')).toBeInTheDocument(); + }); + + fireSelectionChangeEvent(window); + + await waitFor(() => { + expect(getByText('Open new Github issue')).toBeInTheDocument(); + }); + }); +});