|
| 1 | +import { render, cleanup } from '@testing-library/svelte'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { CldOgImage } from '$src/index'; |
| 4 | + |
| 5 | +describe('CldImage', () => { |
| 6 | + afterEach(() => { |
| 7 | + cleanup(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should generate the correct URL for Cloudinary Open Graph image in the og:image meta tag', () => { |
| 11 | + render(CldOgImage, { |
| 12 | + props: { |
| 13 | + src: 'og_image', |
| 14 | + alt: 'OG Image', |
| 15 | + config: { cloud: { cloudName: 'testing-og' } }, |
| 16 | + }, |
| 17 | + }); |
| 18 | + |
| 19 | + const metaTag = document.querySelector<HTMLMetaElement>( |
| 20 | + 'meta[property="og:image"]', |
| 21 | + ); |
| 22 | + |
| 23 | + expect(metaTag).toBeInstanceOf(HTMLMetaElement); |
| 24 | + expect(metaTag?.content).toContain('https://res.cloudinary.com'); |
| 25 | + expect(metaTag?.content).toContain('og_image'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should render with a dynamically generated Cloudinary URL using a random cloud name', () => { |
| 29 | + const cloudName = crypto.randomUUID(); |
| 30 | + |
| 31 | + render(CldOgImage, { |
| 32 | + props: { |
| 33 | + src: 'og_image', |
| 34 | + alt: '', |
| 35 | + config: { cloud: { cloudName } }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + const metaTag = document.querySelector<HTMLMetaElement>( |
| 40 | + 'meta[property="og:image"]', |
| 41 | + ); |
| 42 | + |
| 43 | + expect(metaTag).toBeInstanceOf(HTMLMetaElement); |
| 44 | + expect(metaTag?.content).toContain(cloudName); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should work with global config from environment variables', () => { |
| 48 | + const cloudName = crypto.randomUUID(); |
| 49 | + vi.stubEnv('VITE_CLOUDINARY_CLOUD_NAME', cloudName); |
| 50 | + |
| 51 | + render(CldOgImage, { |
| 52 | + props: { |
| 53 | + src: 'og_image', |
| 54 | + alt: '', |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const metaTag = document.querySelector<HTMLMetaElement>( |
| 59 | + 'meta[property="og:image"]', |
| 60 | + ); |
| 61 | + |
| 62 | + expect(metaTag?.content).toContain(cloudName); |
| 63 | + }); |
| 64 | +}); |
0 commit comments