🤡(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:
33
packages/react/src/tests/AnimateMock.ts
Normal file
33
packages/react/src/tests/AnimateMock.ts
Normal 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();
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user