-
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.
Writing tests and fixing default to match documentation.
- Loading branch information
Showing
3 changed files
with
40 additions
and
32 deletions.
There are no files selected for viewing
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
70 changes: 39 additions & 31 deletions
70
packages/svelte-cloudinary/tests/GetCldOgImageUrl/GetCldOgImageUrl.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 |
---|---|---|
@@ -1,47 +1,55 @@ | ||
import { render, screen, cleanup } from '@testing-library/svelte'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
import { CldOgImage } from '$src/index'; | ||
import { CldOgImage, getCldOgImageUrl } from '$src/index'; | ||
import { tick } from 'svelte'; | ||
|
||
describe('CldOgImage', () => { | ||
describe('GetCldOgImage', () => { | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it('should render a Cloudinary image with defualts', () => { | ||
const url = getCldOgImageUrl( | ||
{ | ||
src: 'sample', | ||
}, | ||
{ cloud: { cloudName: 'testing' } }, | ||
); | ||
expect(url).toContain('sample'); | ||
expect(url).toContain('h_630'); | ||
expect(url).toContain('w_1200'); | ||
expect(url).toContain('c_fill'); | ||
expect(url).toContain('g_center'); | ||
}); | ||
|
||
it('should render a Cloudinary image with the given attributes', () => { | ||
render(CldOgImage, { | ||
props: { | ||
src: 'packages/svelte-cloudinary/tests/images/turtle.jpeg', | ||
alt: 'turtle', | ||
const url = getCldOgImageUrl( | ||
{ | ||
src: 'sample', | ||
width: 300, | ||
height: 200, | ||
config: { cloud: { cloudName: 'testing' } }, | ||
crop: { type: 'auto', gravity: 'east' }, | ||
}, | ||
}); | ||
console.log(screen); | ||
expect(getMetaTagContent('og:image')).toContain( | ||
'https://res.cloudinary.com', | ||
); | ||
expect(getMetaTagContent('og:image')).toContain( | ||
'packages/svelte-cloudinary/tests/images/turtle.jpeg', | ||
{ cloud: { cloudName: 'testing' } }, | ||
); | ||
expect(getMetaTagContent('og:image:secure_url')).toContain( | ||
'packages/svelte-cloudinary/tests/images/turtle.jpeg', | ||
); | ||
expect(getMetaTagContent('og:image:secure_url')).toContain( | ||
'packages/svelte-cloudinary/tests/images/turtle.jpeg', | ||
); | ||
expect(getMetaTagContent('og:image:width')).toContain('300'); | ||
expect(getMetaTagContent('og:image:height')).toContain('200'); | ||
expect(getMetaTagContent('og:image:alt')).toContain('turtle'); | ||
expect(url).toContain('sample'); | ||
expect(url).toContain('h_200'); | ||
expect(url).toContain('w_300'); | ||
expect(url).toContain('c_auto'); | ||
expect(url).toContain('g_east'); | ||
}); | ||
}); | ||
|
||
function getMetaTagContent(name: string): string { | ||
const metaTag = document.querySelector(`meta[property="${name}"]`); | ||
if (!metaTag) { | ||
throw new Error(`Meta tag with name "${name}" not found.`); | ||
} | ||
it('should work with global config from environment variables', () => { | ||
const cloudName = crypto.randomUUID(); | ||
vi.stubEnv('VITE_CLOUDINARY_CLOUD_NAME', cloudName); | ||
|
||
return metaTag.getAttribute('content') || 'null'; | ||
} | ||
const url = getCldOgImageUrl({ | ||
src: 'sample', | ||
}); | ||
expect(url).toContain('sample'); | ||
expect(url).toContain('h_630'); | ||
expect(url).toContain('w_1200'); | ||
expect(url).toContain('c_fill'); | ||
expect(url).toContain('g_center'); | ||
}); | ||
}); |
Binary file not shown.