From 288e429db030f4af0fe11d890b0a7cd8538a737a Mon Sep 17 00:00:00 2001 From: Dorian Miller <45341518+dorianmiller@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:48:22 -0700 Subject: [PATCH] fix: tooltip unit test (#532) * fix: tooltip unit test * fix: satisfy git build * fix: for git build * fix: manage resetPdfWorker * fix: pr feedback --------- Co-authored-by: DORIAN MILLER --- .../CIDocument/__tests__/CIDocument.spec.tsx | 8 ++++ .../Section/__tests__/Section.spec.tsx | 7 ++++ .../src/{setupTests.ts => setupTests.tsx} | 0 .../src/setupTestsUtil.tsx | 38 +++++++++++++++++++ 4 files changed, 53 insertions(+) rename packages/discovery-react-components/src/{setupTests.ts => setupTests.tsx} (100%) create mode 100644 packages/discovery-react-components/src/setupTestsUtil.tsx diff --git a/packages/discovery-react-components/src/components/CIDocument/components/CIDocument/__tests__/CIDocument.spec.tsx b/packages/discovery-react-components/src/components/CIDocument/components/CIDocument/__tests__/CIDocument.spec.tsx index 135fcd7b5..2755bbea1 100644 --- a/packages/discovery-react-components/src/components/CIDocument/components/CIDocument/__tests__/CIDocument.spec.tsx +++ b/packages/discovery-react-components/src/components/CIDocument/components/CIDocument/__tests__/CIDocument.spec.tsx @@ -16,6 +16,14 @@ import CIDocument from '../CIDocument'; import purchaseOrder from '../__fixtures__/po-index_op.json'; import invoice from '../__fixtures__/invoice-index_op.json'; import shortContract from '../__fixtures__/shortenedContract.json'; +import { defineDOMRect, removeDOMRect } from 'setupTestsUtil'; + +beforeAll(() => { + defineDOMRect(); +}); +afterAll(() => { + removeDOMRect(); +}); describe('', () => { describe('Invoice Document', () => { diff --git a/packages/discovery-react-components/src/components/CIDocument/components/Section/__tests__/Section.spec.tsx b/packages/discovery-react-components/src/components/CIDocument/components/Section/__tests__/Section.spec.tsx index cce7d0dde..ccda4bc1b 100644 --- a/packages/discovery-react-components/src/components/CIDocument/components/Section/__tests__/Section.spec.tsx +++ b/packages/discovery-react-components/src/components/CIDocument/components/Section/__tests__/Section.spec.tsx @@ -3,8 +3,15 @@ import { screen, act, render } from '@testing-library/react'; import 'utils/test/createRange.mock'; import Section from '../Section'; import sectionData from '../__fixtures__/sectionData'; +import { defineDOMRect, removeDOMRect } from 'setupTestsUtil'; describe('
', () => { + beforeEach(() => { + defineDOMRect(); + }); + afterEach(() => { + removeDOMRect(); + }); it('renders section HTML', async () => { const data = { html: '

Here I am!

', diff --git a/packages/discovery-react-components/src/setupTests.ts b/packages/discovery-react-components/src/setupTests.tsx similarity index 100% rename from packages/discovery-react-components/src/setupTests.ts rename to packages/discovery-react-components/src/setupTests.tsx diff --git a/packages/discovery-react-components/src/setupTestsUtil.tsx b/packages/discovery-react-components/src/setupTestsUtil.tsx new file mode 100644 index 000000000..df9c31c07 --- /dev/null +++ b/packages/discovery-react-components/src/setupTestsUtil.tsx @@ -0,0 +1,38 @@ +// Helper functions to acount for DOMRect +// guidance https://stackoverflow.com/questions/71521976/referenceerror-domrect-is-not-defined +// globalThis https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis +export function defineDOMRect() { + globalThis.DOMRect = class DOMRect { + x: number = 0; + y: number = 0; + bottom: number = 0; + left: number = 0; + right: number = 0; + top: number = 0; + width: number = 0; + height: number = 0; + constructor( + x: number | undefined = 0, + y: number | undefined = 0, + width: number | undefined = 0, + height: number | undefined = 0 + ) { + this.top = x; + this.bottom = y + height; + this.right = x + width; + this.left = x; + this.width = width; + this.height = height; + } + static fromRect(other: DOMRectInit): DOMRect { + return new DOMRect(other.x, other.y, other.width, other.height); + } + toJSON() { + return JSON.stringify(this); + } + }; +} + +export function removeDOMRect() { + delete globalThis.DOMRect; +}