2023-03-24 06:58:56 -05:00
|
|
|
import { parseHTML } from 'linkedom';
|
|
|
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
2024-01-31 05:37:39 -05:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { after, before, describe, it } from 'node:test';
|
2023-03-24 06:58:56 -05:00
|
|
|
|
|
|
|
const root = new URL('./fixtures/image-assets/', import.meta.url);
|
|
|
|
|
|
|
|
describe('Markdoc - Image assets', () => {
|
|
|
|
let baseFixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
baseFixture = await loadFixture({
|
|
|
|
root,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dev', () => {
|
|
|
|
let devServer;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
devServer = await baseFixture.startDevServer();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses public/ image paths unchanged', async () => {
|
|
|
|
const res = await baseFixture.fetch('/');
|
|
|
|
const html = await res.text();
|
|
|
|
const { document } = parseHTML(html);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(document.querySelector('#public > img')?.src, '/favicon.svg');
|
2023-03-24 06:58:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms relative image paths to optimized path', async () => {
|
|
|
|
const res = await baseFixture.fetch('/');
|
|
|
|
const html = await res.text();
|
|
|
|
const { document } = parseHTML(html);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.match(
|
|
|
|
document.querySelector('#relative > img')?.src,
|
2023-05-29 03:37:40 -05:00
|
|
|
/\/_image\?href=.*%2Fsrc%2Fassets%2Frelative%2Foar.jpg%3ForigWidth%3D420%26origHeight%3D630%26origFormat%3Djpg&f=webp/
|
2023-03-24 06:58:56 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms aliased image paths to optimized path', async () => {
|
|
|
|
const res = await baseFixture.fetch('/');
|
|
|
|
const html = await res.text();
|
|
|
|
const { document } = parseHTML(html);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.match(
|
|
|
|
document.querySelector('#alias > img')?.src,
|
2023-05-29 03:37:40 -05:00
|
|
|
/\/_image\?href=.*%2Fsrc%2Fassets%2Falias%2Fcityscape.jpg%3ForigWidth%3D420%26origHeight%3D280%26origFormat%3Djpg&f=webp/
|
2023-03-24 06:58:56 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('build', () => {
|
|
|
|
before(async () => {
|
|
|
|
await baseFixture.build();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses public/ image paths unchanged', async () => {
|
|
|
|
const html = await baseFixture.readFile('/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(document.querySelector('#public > img')?.src, '/favicon.svg');
|
2023-03-24 06:58:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms relative image paths to optimized path', async () => {
|
|
|
|
const html = await baseFixture.readFile('/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.match(document.querySelector('#relative > img')?.src, /^\/_astro\/oar.*\.webp$/);
|
2023-03-24 06:58:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms aliased image paths to optimized path', async () => {
|
|
|
|
const html = await baseFixture.readFile('/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.match(document.querySelector('#alias > img')?.src, /^\/_astro\/cityscape.*\.webp$/);
|
2023-03-24 06:58:56 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|