|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { getCldImageUrl } from '$src/helpers/getCldImageUrl'; |
| 3 | +import type { ConfigOptions, ImageOptions } from '@cloudinary-util/url-loader'; |
| 4 | + |
| 5 | +describe('getCldImageUrl', () => { |
| 6 | + it('should generate a Cloudinary URL with basic image options', () => { |
| 7 | + const options: ImageOptions = { |
| 8 | + src: 'sample', |
| 9 | + width: 300, |
| 10 | + height: 200, |
| 11 | + }; |
| 12 | + |
| 13 | + const config: ConfigOptions = { |
| 14 | + cloud: { cloudName: 'testing' }, |
| 15 | + }; |
| 16 | + |
| 17 | + const url = getCldImageUrl(options, config); |
| 18 | + |
| 19 | + expect(url).toContain('https://res.cloudinary.com/testing'); |
| 20 | + expect(url).toContain('w_300'); |
| 21 | + expect(url).toContain('h_200'); |
| 22 | + expect(url).toContain('sample'); |
| 23 | + expect(url).toContain('f_auto'); |
| 24 | + expect(url).toContain('q_auto'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should apply default crop values when no crop option is provided', () => { |
| 28 | + const options: ImageOptions = { |
| 29 | + src: 'sample', |
| 30 | + width: 300, |
| 31 | + height: 200, |
| 32 | + }; |
| 33 | + |
| 34 | + const config: ConfigOptions = { |
| 35 | + cloud: { cloudName: 'testing' }, |
| 36 | + }; |
| 37 | + |
| 38 | + const url = getCldImageUrl(options, config); |
| 39 | + |
| 40 | + expect(url).toContain('c_fill'); |
| 41 | + expect(url).toContain('g_center'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should apply custom crop values when provided', () => { |
| 45 | + const options: ImageOptions = { |
| 46 | + src: 'sample', |
| 47 | + width: 300, |
| 48 | + height: 200, |
| 49 | + crop: { |
| 50 | + type: 'fit', |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + const config: ConfigOptions = { |
| 55 | + cloud: { cloudName: 'testing' }, |
| 56 | + }; |
| 57 | + |
| 58 | + const url = getCldImageUrl(options, config); |
| 59 | + |
| 60 | + expect(url).toContain('c_fit'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should throw an error when the cloud name is missing from the config', () => { |
| 64 | + const options: ImageOptions = { |
| 65 | + src: 'sample', |
| 66 | + width: 300, |
| 67 | + height: 200, |
| 68 | + }; |
| 69 | + |
| 70 | + const configOverride: ConfigOptions = { |
| 71 | + cloud: { cloudName: '' }, // Missing cloud name |
| 72 | + }; |
| 73 | + |
| 74 | + expect(() => getCldImageUrl(options, configOverride)).toThrowError( |
| 75 | + '[svelte-cloudinary] unable to find a cloud name', |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should override global config with the provided configOverride', () => { |
| 80 | + const options: ImageOptions = { |
| 81 | + src: 'sample', |
| 82 | + width: 300, |
| 83 | + height: 200, |
| 84 | + }; |
| 85 | + |
| 86 | + const configOverride: ConfigOptions = { |
| 87 | + cloud: { cloudName: 'override-cloud' }, |
| 88 | + }; |
| 89 | + |
| 90 | + const url = getCldImageUrl(options, configOverride); |
| 91 | + |
| 92 | + expect(url).toContain('https://res.cloudinary.com/override-cloud'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments