-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for getCldImageUrl (#162)
Co-authored-by: Willow (GHOST) <[email protected]>
- Loading branch information
1 parent
fc309ba
commit f797c23
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
packages/svelte-cloudinary/tests/GetCldImageUrl/GetCldImageUrl.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |