0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/mdx/test/mdx-images.test.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-01-31 03:37:34 -05:00
import { describe, it, before, after } from 'node:test';
import * as assert from 'node:assert/strict';
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'];
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);
const html = await res.text();
const { document } = parseHTML(html);
const imgs = document.getElementsByTagName('img');
2024-01-31 03:37:34 -05:00
assert.equal(imgs.length, 4);
// Image using a relative path
2024-01-31 03:37:34 -05:00
assert.equal(imgs.item(0).src.startsWith('/_image'), true);
// Image using an aliased path
2024-01-31 03:37:34 -05:00
assert.equal(imgs.item(1).src.startsWith('/_image'), true);
// Image with title
2024-01-31 03:37:34 -05:00
assert.equal(imgs.item(2).title, 'Houston title');
// Image with spaces in the path
2024-01-31 03:37:34 -05:00
assert.equal(imgs.item(3).src.startsWith('/_image'), true);
});
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
const html = await res.text();
const { document } = parseHTML(html);
2023-09-13 11:29:09 -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
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
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);
});
}
});
});