|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 The Diffusion Studio Authors |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla |
| 5 | + * Public License, v. 2.0 that can be found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, expect, it, vi } from 'vitest'; |
| 9 | +import { documentToSvgImageUrl, fontToBas64Url } from './html.utils'; |
| 10 | +import { setFetchMockReturnValue } from '../../vitest.mocks'; |
| 11 | + |
| 12 | +describe('documentToSvgImageUrl', () => { |
| 13 | + it('should return empty SVG if document is not provided', () => { |
| 14 | + const result = documentToSvgImageUrl(); |
| 15 | + expect(result).toBe("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E"); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should return empty SVG if document has no body', () => { |
| 19 | + const mockDocument = document.implementation.createDocument('', '', null); |
| 20 | + const result = documentToSvgImageUrl(mockDocument); |
| 21 | + expect(result).toBe("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E"); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return valid SVG when document has body and style', () => { |
| 25 | + const mockDocument = document.implementation.createHTMLDocument('Test Document'); |
| 26 | + const body = mockDocument.body; |
| 27 | + const style = mockDocument.createElement('style'); |
| 28 | + style.textContent = 'body { background: red; }'; |
| 29 | + mockDocument.head.appendChild(style); |
| 30 | + body.innerHTML = '<div>Hello World</div>'; |
| 31 | + |
| 32 | + const result = documentToSvgImageUrl(mockDocument); |
| 33 | + |
| 34 | + // Check if result starts with valid data URI and contains parts of the body and style |
| 35 | + expect(result).toContain('data:image/svg+xml;base64,'); |
| 36 | + const decodedSvg = atob(result.split(',')[1]); |
| 37 | + expect(decodedSvg).toContain('Hello World'); |
| 38 | + expect(decodedSvg).toContain('body { background: red; }'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return valid SVG when document has body but no style', () => { |
| 42 | + const mockDocument = document.implementation.createHTMLDocument('Test Document'); |
| 43 | + const body = mockDocument.body; |
| 44 | + body.innerHTML = '<div>Hello World</div>'; |
| 45 | + |
| 46 | + const result = documentToSvgImageUrl(mockDocument); |
| 47 | + |
| 48 | + // Check if result starts with valid data URI and contains parts of the body |
| 49 | + expect(result).toContain('data:image/svg+xml;base64,'); |
| 50 | + const decodedSvg = atob(result.split(',')[1]); |
| 51 | + expect(decodedSvg).toContain('Hello World'); |
| 52 | + expect(decodedSvg).not.toContain('<style>'); // There should be no style element |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle documents with nested elements', () => { |
| 56 | + const mockDocument = document.implementation.createHTMLDocument('Test Document'); |
| 57 | + const body = mockDocument.body; |
| 58 | + body.innerHTML = '<div><p>Nested</p></div>'; |
| 59 | + |
| 60 | + const result = documentToSvgImageUrl(mockDocument); |
| 61 | + |
| 62 | + // Check if result starts with valid data URI and contains nested elements |
| 63 | + expect(result).toContain('data:image/svg+xml;base64,'); |
| 64 | + const decodedSvg = atob(result.split(',')[1]); |
| 65 | + expect(decodedSvg).toContain('<div><p>Nested</p></div>'); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('fontToBas64Url', () => { |
| 70 | + it('should return base64 font URL with format woff2 by default', async () => { |
| 71 | + // Mock the response from fetch to return a Blob |
| 72 | + const resetFetch = setFetchMockReturnValue({ |
| 73 | + ok: true, |
| 74 | + blob: async () => new Blob(['font-data'], { type: 'font/woff2' }), |
| 75 | + }); |
| 76 | + |
| 77 | + // Mock FileReader and its behavior |
| 78 | + vi.stubGlobal('FileReader', class { |
| 79 | + readAsDataURL = vi.fn(); |
| 80 | + set onloadend(fn: () => void) { |
| 81 | + fn(); |
| 82 | + } |
| 83 | + result = 'data:font/woff2;base64,Zm9udC1kYXRh'; |
| 84 | + }); |
| 85 | + |
| 86 | + const result = await fontToBas64Url('https://example.com/font.woff2'); |
| 87 | + |
| 88 | + expect(result).toBe(`url(data:font/woff2;base64,Zm9udC1kYXRh) format('woff2')`); |
| 89 | + |
| 90 | + resetFetch() |
| 91 | + vi.unstubAllGlobals(); // Cleanup |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return base64 font URL with format ttf', async () => { |
| 95 | + // Mock the response from fetch to return a Blob |
| 96 | + const resetFetch = setFetchMockReturnValue({ |
| 97 | + ok: true, |
| 98 | + blob: async () => new Blob(['font-data'], { type: 'font/ttf' }), |
| 99 | + }); |
| 100 | + |
| 101 | + // Mock FileReader and its behavior |
| 102 | + vi.stubGlobal('FileReader', class { |
| 103 | + readAsDataURL = vi.fn(); |
| 104 | + set onloadend(fn: () => void) { |
| 105 | + fn(); |
| 106 | + } |
| 107 | + result = 'data:font/ttf;base64,Zm9udC1kYXRh'; |
| 108 | + }); |
| 109 | + |
| 110 | + const result = await fontToBas64Url('https://example.com/font.ttf'); |
| 111 | + |
| 112 | + expect(result).toBe(`url(data:font/ttf;base64,Zm9udC1kYXRh) format('truetype')`); |
| 113 | + |
| 114 | + resetFetch() |
| 115 | + vi.unstubAllGlobals(); // Cleanup |
| 116 | + }); |
| 117 | +}); |
0 commit comments