2024-01-31 03:37:34 -05:00
|
|
|
import * as assert from 'node:assert/strict';
|
2024-02-21 09:08:19 -05:00
|
|
|
import { after, before, describe, it } from 'node:test';
|
2023-04-13 04:54:40 -05:00
|
|
|
import { parseHTML } from 'linkedom';
|
|
|
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
|
|
|
2023-09-13 11:29:09 -05:00
|
|
|
const imageTestRoutes = ['with-components', 'esm-import', 'content-collection'];
|
2023-09-13 11:27:03 -05:00
|
|
|
|
2023-04-13 04:54:40 -05:00
|
|
|
describe('MDX Page', () => {
|
|
|
|
let devServer;
|
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: new URL('./fixtures/mdx-images/', import.meta.url),
|
|
|
|
});
|
|
|
|
devServer = await fixture.startDevServer();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Optimized images in MDX', () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const res = await fixture.fetch('/');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(res.status, 200);
|
2023-04-13 04:54:40 -05:00
|
|
|
|
|
|
|
const html = await res.text();
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
|
|
|
const imgs = document.getElementsByTagName('img');
|
2024-04-16 09:13:52 -05:00
|
|
|
assert.equal(imgs.length, 6);
|
2023-04-13 04:54:40 -05:00
|
|
|
// Image using a relative path
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(imgs.item(0).src.startsWith('/_image'), true);
|
2023-04-13 04:54:40 -05:00
|
|
|
// Image using an aliased path
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(imgs.item(1).src.startsWith('/_image'), true);
|
2023-04-13 04:54:40 -05:00
|
|
|
// Image with title
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(imgs.item(2).title, 'Houston title');
|
2023-04-13 04:54:40 -05:00
|
|
|
// Image with spaces in the path
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(imgs.item(3).src.startsWith('/_image'), true);
|
2024-04-16 09:13:52 -05:00
|
|
|
// Image using a relative path with no slashes
|
|
|
|
assert.equal(imgs.item(4).src.startsWith('/_image'), true);
|
|
|
|
// Image using a relative path with nested directory
|
|
|
|
assert.equal(imgs.item(5).src.startsWith('/_image'), true);
|
2023-04-13 04:54:40 -05:00
|
|
|
});
|
2023-09-13 11:27:03 -05:00
|
|
|
|
|
|
|
for (const route of imageTestRoutes) {
|
|
|
|
it(`supports img component - ${route}`, async () => {
|
|
|
|
const res = await fixture.fetch(`/${route}`);
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(res.status, 200);
|
2023-09-13 11:29:09 -05:00
|
|
|
|
2023-09-13 11:27:03 -05:00
|
|
|
const html = await res.text();
|
|
|
|
const { document } = parseHTML(html);
|
2023-09-13 11:29:09 -05:00
|
|
|
|
2023-09-13 11:27:03 -05:00
|
|
|
const imgs = document.getElementsByTagName('img');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(imgs.length, 2);
|
2023-09-13 11:29:09 -05:00
|
|
|
|
2023-09-13 11:27:03 -05:00
|
|
|
const assetsImg = imgs.item(0);
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(assetsImg.src.startsWith('/_image'), true);
|
|
|
|
assert.equal(assetsImg.hasAttribute('data-my-image'), true);
|
2023-09-13 11:29:09 -05:00
|
|
|
|
2023-09-13 11:27:03 -05:00
|
|
|
const publicImg = imgs.item(1);
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(publicImg.src, '/favicon.svg');
|
|
|
|
assert.equal(publicImg.hasAttribute('data-my-image'), true);
|
2023-09-13 11:27:03 -05:00
|
|
|
});
|
|
|
|
}
|
2023-04-13 04:54:40 -05:00
|
|
|
});
|
|
|
|
});
|