🤡(react) add Animate API

The jest-dom by default does not mock the Animate API, as the Toast
use it we need to mock it.
This commit is contained in:
Nathan Vasse
2024-01-05 11:53:25 +01:00
committed by NathanVss
parent 83a533d245
commit 9e0f7c5ef4
2 changed files with 34 additions and 0 deletions

View File

@@ -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();

View File

@@ -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);