mdx-images.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { describe, it, before, after } from 'node:test';
  2. import * as assert from 'node:assert/strict';
  3. import { parseHTML } from 'linkedom';
  4. import { loadFixture } from '../../../astro/test/test-utils.js';
  5. const imageTestRoutes = ['with-components', 'esm-import', 'content-collection'];
  6. describe('MDX Page', () => {
  7. let devServer;
  8. let fixture;
  9. before(async () => {
  10. fixture = await loadFixture({
  11. root: new URL('./fixtures/mdx-images/', import.meta.url),
  12. });
  13. devServer = await fixture.startDevServer();
  14. });
  15. after(async () => {
  16. await devServer.stop();
  17. });
  18. describe('Optimized images in MDX', () => {
  19. it('works', async () => {
  20. const res = await fixture.fetch('/');
  21. assert.equal(res.status, 200);
  22. const html = await res.text();
  23. const { document } = parseHTML(html);
  24. const imgs = document.getElementsByTagName('img');
  25. assert.equal(imgs.length, 4);
  26. // Image using a relative path
  27. assert.equal(imgs.item(0).src.startsWith('/_image'), true);
  28. // Image using an aliased path
  29. assert.equal(imgs.item(1).src.startsWith('/_image'), true);
  30. // Image with title
  31. assert.equal(imgs.item(2).title, 'Houston title');
  32. // Image with spaces in the path
  33. assert.equal(imgs.item(3).src.startsWith('/_image'), true);
  34. });
  35. for (const route of imageTestRoutes) {
  36. it(`supports img component - ${route}`, async () => {
  37. const res = await fixture.fetch(`/${route}`);
  38. assert.equal(res.status, 200);
  39. const html = await res.text();
  40. const { document } = parseHTML(html);
  41. const imgs = document.getElementsByTagName('img');
  42. assert.equal(imgs.length, 2);
  43. const assetsImg = imgs.item(0);
  44. assert.equal(assetsImg.src.startsWith('/_image'), true);
  45. assert.equal(assetsImg.hasAttribute('data-my-image'), true);
  46. const publicImg = imgs.item(1);
  47. assert.equal(publicImg.src, '/favicon.svg');
  48. assert.equal(publicImg.hasAttribute('data-my-image'), true);
  49. });
  50. }
  51. });
  52. });