Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion src/hooks/useAnimationLoop/useAnimationLoop.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderHook } from '@testing-library/react';
import { jest } from '@jest/globals';
import { renderHook, waitFor } from '@testing-library/react';
import { useAnimationLoop } from './useAnimationLoop.js';

describe('useAnimationLoop', () => {
Expand All @@ -7,4 +8,101 @@ describe('useAnimationLoop', () => {
initialProps: undefined,
});
});

it('should not execute the callback function when enabled is not passed', async () => {
const spy = jest.fn();
renderHook(
({ callback }) => {
useAnimationLoop(callback);
},
{
initialProps: {
callback: () => {
spy();
},
},
},
);

waitFor(() => {
expect(spy).toBeCalledTimes(0);
});
});

it('should execute the callback function when useAnimationLoop is enabled', async () => {
const spy = jest.fn();
renderHook(
({ callback, enabled }) => {
useAnimationLoop(callback, enabled);
},
{
initialProps: {
callback: () => {
spy();
},
enabled: true,
},
},
);

expect(spy).toBeCalledTimes(0);
waitFor(() => {
expect(spy).toBeCalled();
});
});

it('should execute another callback function when it is passed to useAnimationLoop and not execute previously passed callback function', async () => {
const spyFirstRender = jest.fn();
const spySecondRender = jest.fn();
const { rerender } = renderHook(
({ callback, enabled }) => {
useAnimationLoop(callback, enabled);
},
{
initialProps: {
callback: () => {
spyFirstRender();
},
enabled: true,
},
},
);

waitFor(() => {
expect(spyFirstRender).toBeCalled();
expect(spySecondRender).toBeCalledTimes(0);
});

rerender({ callback: spySecondRender, enabled: true });
waitFor(() => {
expect(spySecondRender).toBeCalled();
expect(spyFirstRender).toBeCalledTimes(0);
});
});

it('should execute the callback function when useAnimationLoop is enabled on the first render and should not execute the callback function when useAnimationLoop is disabled on the second render', async () => {
const spy = jest.fn();
const { rerender } = renderHook(
({ callback, enabled }) => {
useAnimationLoop(callback, enabled);
},
{
initialProps: {
callback: () => {
spy();
},
enabled: true,
},
},
);

waitFor(() => {
expect(spy).toBeCalled();
});

rerender({ callback: spy, enabled: false });
waitFor(() => {
expect(spy).toBeCalledTimes(0);
});
});
});