feat: test toegevoegd base64
This commit is contained in:
parent
7d78cd1440
commit
f62baae90f
5 changed files with 78 additions and 0 deletions
1
frontend/public/base64Sample.txt
Normal file
1
frontend/public/base64Sample.txt
Normal file
File diff suppressed because one or more lines are too long
52
frontend/public/test-base64-image.html
Normal file
52
frontend/public/test-base64-image.html
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Base64 Image Test</title>
|
||||||
|
<script>
|
||||||
|
function convertBase64ToImageSrc(base64String) {
|
||||||
|
return base64String.startsWith("data:image")
|
||||||
|
? base64String
|
||||||
|
: `data:image/png;base64,${base64String}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadImage() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('base64Sample.txt');
|
||||||
|
const base64String = await response.text();
|
||||||
|
|
||||||
|
if (!base64String.trim()) {
|
||||||
|
console.error("❌ No Base64 data found in the file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("📸 Base64 Data (first 50 chars):", base64String.substring(0, 50) + "...");
|
||||||
|
|
||||||
|
const imgElement = document.getElementById('imagePreview');
|
||||||
|
imgElement.src = convertBase64ToImageSrc(base64String);
|
||||||
|
imgElement.alt = "Base64 Test Image";
|
||||||
|
|
||||||
|
// Handle load event
|
||||||
|
imgElement.onload = () => {
|
||||||
|
console.log("✅ Image loaded successfully!");
|
||||||
|
};
|
||||||
|
|
||||||
|
imgElement.onerror = () => {
|
||||||
|
console.error("❌ Image failed to load!");
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Error fetching Base64 image:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.onload = loadImage;
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Base64 Image Test</h1>
|
||||||
|
<p>This page will fetch and display a Base64 image from <code>tests/base64.txt</code>.</p>
|
||||||
|
<img id="imagePreview" src="" alt="Loading image..." width="300">
|
||||||
|
</body>
|
||||||
|
</html>
|
1
frontend/tests/base64/base64Sample.txt
Normal file
1
frontend/tests/base64/base64Sample.txt
Normal file
File diff suppressed because one or more lines are too long
24
frontend/tests/base64/base64ToImage.test.ts
Normal file
24
frontend/tests/base64/base64ToImage.test.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { describe, it, expect, beforeAll } from 'vitest';
|
||||||
|
import { convertBase64ToImageSrc } from '../../src/utils/base64ToImage.js';
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
let sampleBase64: string;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
// Load base64 sample from text file
|
||||||
|
const filePath = path.resolve(__dirname, 'base64Sample.txt');
|
||||||
|
sampleBase64 = fs.readFileSync(filePath, 'utf8').trim();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('convertBase64ToImageSrc', () => {
|
||||||
|
it('should return the same string if it is already a valid data URL', () => {
|
||||||
|
const base64Image = `data:image/png;base64,${sampleBase64}`;
|
||||||
|
expect(convertBase64ToImageSrc(base64Image)).toBe(base64Image);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should correctly format a raw Base64 string as a PNG image URL', () => {
|
||||||
|
expect(convertBase64ToImageSrc(sampleBase64)).toBe(`data:image/png;base64,${sampleBase64}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue