Skip to content

🎥 Make videos programmatically with React

add tests for hooks

Calvin Faltercd51b813c66a
diff --git a/packages/core/src/test/use-media-tag-volume.test.tsx b/packages/core/src/test/use-media-tag-volume.test.tsx
new file mode 100644
index 0000000000..bd247113bf
--- /dev/null
+++ b/packages/core/src/test/use-media-tag-volume.test.tsx
@@ -0,0 +1,48 @@
+import {renderHook} from '@testing-library/react-hooks';
+import React, {RefObject} from 'react';
+import {useMediaTagVolume} from '../use-media-tag-volume';
+
+describe('setState test', () => {
+ const setState = jest.fn();
+ const useStateSpy = jest.spyOn(React, 'useState');
+ beforeEach(() => {
+ //@ts-expect-error
+ useStateSpy.mockImplementation((init) => [init, setState]);
+ });
+ afterEach(() => {
+ useStateSpy.mockRestore();
+ });
+
+ test('has the volume been set', async () => {
+ const addEventListener = jest.fn();
+ const removeEventListener = jest.fn();
+ let audioRef = ({
+ current: {volume: 0.5, addEventListener, removeEventListener},
+ } as unknown) as RefObject<HTMLAudioElement>;
+
+ const {rerender} = renderHook(({mediaRef}) => useMediaTagVolume(mediaRef), {
+ initialProps: {mediaRef: audioRef},
+ });
+
+ expect(setState).toHaveBeenCalledWith(0.5);
+ audioRef = {
+ current: {...audioRef.current, volume: 0.75},
+ } as RefObject<HTMLAudioElement>;
+ rerender({mediaRef: audioRef});
+ expect(setState).toHaveBeenCalledWith(0.75);
+ });
+});
+
+test('has the event listener been added', () => {
+ const addEventListener = jest.fn();
+ const removeEventListener = jest.fn();
+ const audioRef = ({
+ current: {volume: 0.5, addEventListener, removeEventListener},
+ } as unknown) as RefObject<HTMLAudioElement>;
+
+ renderHook(({mediaRef}) => useMediaTagVolume(mediaRef), {
+ initialProps: {mediaRef: audioRef},
+ });
+
+ expect(addEventListener).toHaveBeenCalledTimes(1);
+});
diff --git a/packages/core/src/test/use-sync-volume-with-media-tag.test.ts b/packages/core/src/test/use-sync-volume-with-media-tag.test.ts
new file mode 100644
index 0000000000..ed50612223
--- /dev/null
+++ b/packages/core/src/test/use-sync-volume-with-media-tag.test.ts
@@ -0,0 +1,49 @@
+import {renderHook} from '@testing-library/react-hooks';
+import {RefObject} from 'react';
+import {useSyncVolumeWithMediaTag} from '../use-sync-volume-with-media-tag';
+import {VolumeProp} from '../volume-prop';
+
+test('has the volume been adapted', async () => {
+ const addEventListener = jest.fn();
+ const removeEventListener = jest.fn();
+ const audioRef = ({
+ current: {volume: 0.5, addEventListener, removeEventListener},
+ } as unknown) as RefObject<HTMLAudioElement>;
+ const volume = 0.6 as VolumeProp;
+ const volumePropFrame = 1;
+ const actualVolume = 0.4;
+
+ const initialProps = {volumePropFrame, actualVolume, volume, mediaRef: audioRef};
+
+ const {rerender} = renderHook(
+ (hookProps) => useSyncVolumeWithMediaTag(hookProps),
+ {
+ initialProps,
+ }
+ );
+ expect(audioRef.current?.volume).toEqual(initialProps.volume)
+ initialProps.volume = 0.5
+ rerender(initialProps)
+ expect(audioRef.current?.volume).toEqual(initialProps.volume)
+});
+
+test('volume should not be adapted', async () => {
+ const addEventListener = jest.fn();
+ const removeEventListener = jest.fn();
+ const audioRef = ({
+ current: {volume: 0.4, addEventListener, removeEventListener},
+ } as unknown) as RefObject<HTMLAudioElement>;
+ const volume = 0.4 as VolumeProp;
+ const volumePropFrame = 1;
+ const actualVolume = 0.4;
+
+ const initialProps = {volumePropFrame, actualVolume, volume, mediaRef: audioRef};
+
+ renderHook(
+ (hookProps) => useSyncVolumeWithMediaTag(hookProps),
+ {
+ initialProps,
+ }
+ );
+ expect(audioRef.current?.volume).toEqual(initialProps.volume)
+});
\ No newline at end of file