Skip to content

🎥 Make videos programmatically with React

add tests for pixel-format.ts

Calvin Falter60fb30cbb53d
diff --git a/packages/core/src/config/pixel-format.ts b/packages/core/src/config/pixel-format.ts
index d256608918..017457e1c9 100644
--- a/packages/core/src/config/pixel-format.ts
+++ b/packages/core/src/config/pixel-format.ts
@@ -31,12 +31,15 @@ export const validateSelectedPixelFormatAndCodecCombination = (
pixelFormat: PixelFormat,
codec: Codec
) => {
+ if (!validOptions.includes(pixelFormat)) {
+ throw new TypeError(`Value ${pixelFormat} is not valid as a pixel format.`);
+ }
if (pixelFormat !== 'yuva420p') {
return;
}
if (codec !== 'vp8' && codec !== 'vp9') {
throw new TypeError(
- "Pixel format was set to 'yuva420p' but codec is not 'vp8' or 'vp8'. To render videos with alpha channel, you must choose a codec that supports it."
+ "Pixel format was set to 'yuva420p' but codec is not 'vp8' or 'vp9'. To render videos with alpha channel, you must choose a codec that supports it."
);
}
};
diff --git a/packages/core/src/test/pixel-format.test.ts b/packages/core/src/test/pixel-format.test.ts
new file mode 100644
index 0000000000..6e1fb918f3
--- /dev/null
+++ b/packages/core/src/test/pixel-format.test.ts
@@ -0,0 +1,79 @@
+import {Codec} from '../config';
+import {
+ getPixelFormat,
+ PixelFormat,
+ setPixelFormat,
+ validateSelectedPixelFormatAndCodecCombination,
+} from '../config/pixel-format';
+import {expectToThrow} from './expect-to-throw';
+
+test('pixel-format tests setPixelFormat', () => {
+ // input format
+ const valuesA: PixelFormat[] = [
+ 'yuv420p',
+ 'yuva420p',
+ 'yuv422p',
+ 'yuv444p',
+ 'yuv420p10le',
+ 'yuv422p10le',
+ 'yuv444p10le',
+ ];
+ valuesA.forEach((entry) => {
+ setPixelFormat(entry);
+ expect(getPixelFormat()).toEqual(entry);
+ });
+
+ // input format
+ const valuesB = ['abc', '', 3, undefined];
+ valuesB.forEach((entry) =>
+ expectToThrow(
+ // @ts-expect-error
+ () => setPixelFormat(entry),
+ new RegExp(`Value ${entry} is not valid as a pixel format.`)
+ )
+ );
+});
+
+test('pixel-format tests validateSelectedPixelFormatAndCodecCombination', () => {
+ const formats: PixelFormat[] = [
+ 'yuv420p',
+ 'yuv422p',
+ 'yuv444p',
+ 'yuv420p10le',
+ 'yuv422p10le',
+ 'yuv444p10le',
+ ];
+
+ const codecs: Codec[] = ['h264', 'h265', 'vp8', 'vp9'];
+
+ formats.forEach((f) =>
+ codecs.forEach((c) =>
+ expect(() =>
+ validateSelectedPixelFormatAndCodecCombination(f, c)
+ ).not.toThrow()
+ )
+ );
+
+ const invalidCodecs: Codec[] = ['h264', 'h265'];
+ invalidCodecs.forEach((entry) =>
+ expectToThrow(
+ () => validateSelectedPixelFormatAndCodecCombination('yuva420p', entry),
+ /Pixel format was set to 'yuva420p' but codec is not 'vp8' or 'vp9'. To render videos with alpha channel, you must choose a codec that supports it./
+ )
+ );
+ const validCodecs: Codec[] = ['vp8', 'vp9'];
+ validCodecs.forEach((c) =>
+ expect(() =>
+ validateSelectedPixelFormatAndCodecCombination('yuva420p', c)
+ ).not.toThrow()
+ );
+
+ const invalidFormats = ['abc', '', 3];
+ invalidFormats.forEach((entry) =>
+ expectToThrow(
+ // @ts-expect-error
+ () => validateSelectedPixelFormatAndCodecCombination(entry, 'h264'),
+ new RegExp(`Value ${entry} is not valid as a pixel format.`)
+ )
+ );
+});