From f797c23bd3754abc64f2191bb0df17fefea670e3 Mon Sep 17 00:00:00 2001 From: Himath Samarakoon <114925949+moonlander101@users.noreply.github.com> Date: Fri, 25 Oct 2024 07:14:55 +0530 Subject: [PATCH] test: add tests for getCldImageUrl (#162) Co-authored-by: Willow (GHOST) --- .../GetCldImageUrl/GetCldImageUrl.test.ts | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 packages/svelte-cloudinary/tests/GetCldImageUrl/GetCldImageUrl.test.ts diff --git a/packages/svelte-cloudinary/tests/GetCldImageUrl/GetCldImageUrl.test.ts b/packages/svelte-cloudinary/tests/GetCldImageUrl/GetCldImageUrl.test.ts new file mode 100644 index 00000000..53715436 --- /dev/null +++ b/packages/svelte-cloudinary/tests/GetCldImageUrl/GetCldImageUrl.test.ts @@ -0,0 +1,94 @@ +import { describe, it, expect } from 'vitest'; +import { getCldImageUrl } from '$src/helpers/getCldImageUrl'; +import type { ConfigOptions, ImageOptions } from '@cloudinary-util/url-loader'; + +describe('getCldImageUrl', () => { + it('should generate a Cloudinary URL with basic image options', () => { + const options: ImageOptions = { + src: 'sample', + width: 300, + height: 200, + }; + + const config: ConfigOptions = { + cloud: { cloudName: 'testing' }, + }; + + const url = getCldImageUrl(options, config); + + expect(url).toContain('https://res.cloudinary.com/testing'); + expect(url).toContain('w_300'); + expect(url).toContain('h_200'); + expect(url).toContain('sample'); + expect(url).toContain('f_auto'); + expect(url).toContain('q_auto'); + }); + + it('should apply default crop values when no crop option is provided', () => { + const options: ImageOptions = { + src: 'sample', + width: 300, + height: 200, + }; + + const config: ConfigOptions = { + cloud: { cloudName: 'testing' }, + }; + + const url = getCldImageUrl(options, config); + + expect(url).toContain('c_fill'); + expect(url).toContain('g_center'); + }); + + it('should apply custom crop values when provided', () => { + const options: ImageOptions = { + src: 'sample', + width: 300, + height: 200, + crop: { + type: 'fit', + }, + }; + + const config: ConfigOptions = { + cloud: { cloudName: 'testing' }, + }; + + const url = getCldImageUrl(options, config); + + expect(url).toContain('c_fit'); + }); + + it('should throw an error when the cloud name is missing from the config', () => { + const options: ImageOptions = { + src: 'sample', + width: 300, + height: 200, + }; + + const configOverride: ConfigOptions = { + cloud: { cloudName: '' }, // Missing cloud name + }; + + expect(() => getCldImageUrl(options, configOverride)).toThrowError( + '[svelte-cloudinary] unable to find a cloud name', + ); + }); + + it('should override global config with the provided configOverride', () => { + const options: ImageOptions = { + src: 'sample', + width: 300, + height: 200, + }; + + const configOverride: ConfigOptions = { + cloud: { cloudName: 'override-cloud' }, + }; + + const url = getCldImageUrl(options, configOverride); + + expect(url).toContain('https://res.cloudinary.com/override-cloud'); + }); +});