From 9e0f7c5ef4971e94105c54fba48a9d3d74eebb63 Mon Sep 17 00:00:00 2001 From: Nathan Vasse Date: Fri, 5 Jan 2024 11:53:25 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=A1(react)=20add=20Animate=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The jest-dom by default does not mock the Animate API, as the Toast use it we need to mock it. --- packages/react/src/tests/AnimateMock.ts | 33 +++++++++++++++++++++++++ packages/react/src/tests/Setup.ts | 1 + 2 files changed, 34 insertions(+) create mode 100644 packages/react/src/tests/AnimateMock.ts diff --git a/packages/react/src/tests/AnimateMock.ts b/packages/react/src/tests/AnimateMock.ts new file mode 100644 index 0000000..1a8b816 --- /dev/null +++ b/packages/react/src/tests/AnimateMock.ts @@ -0,0 +1,33 @@ +/** + * Inspired by https://stackoverflow.com/a/74725347. + */ +const animationMock = () => { + let originalAnimateFunction: typeof HTMLDivElement.prototype.animate; + let originalGetAnimationsFunction: typeof HTMLDivElement.prototype.getAnimations; + + // Mock native animate function + beforeAll(() => { + originalAnimateFunction = HTMLDivElement.prototype.animate; + originalGetAnimationsFunction = HTMLDivElement.prototype.getAnimations; + + const obj = { + onfinish: () => {}, + }; + + HTMLDivElement.prototype.animate = () => { + Promise.resolve().then(async () => { + obj.onfinish(); + }); + + return obj as unknown as Animation; + }; + HTMLDivElement.prototype.getAnimations = () => []; + }); + + afterAll(() => { + HTMLDivElement.prototype.animate = originalAnimateFunction; + HTMLDivElement.prototype.getAnimations = originalGetAnimationsFunction; + }); +}; + +animationMock(); diff --git a/packages/react/src/tests/Setup.ts b/packages/react/src/tests/Setup.ts index 24b86c8..6f5f036 100644 --- a/packages/react/src/tests/Setup.ts +++ b/packages/react/src/tests/Setup.ts @@ -1,6 +1,7 @@ import "@testing-library/jest-dom/vitest"; import createFetchMock from "vitest-fetch-mock"; import { vi } from "vitest"; +import "./AnimateMock"; const fetchMocker = createFetchMock(vi);