Skip to content

Commit

Permalink
test: add tests for getCldImageUrl (#162)
Browse files Browse the repository at this point in the history
Co-authored-by: Willow (GHOST) <[email protected]>
  • Loading branch information
moonlander101 and ghostdevv authored Oct 25, 2024
1 parent fc309ba commit f797c23
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit f797c23

Please sign in to comment.