image-assets.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { parseHTML } from 'linkedom';
  2. import { loadFixture } from '../../../astro/test/test-utils.js';
  3. import assert from 'node:assert/strict';
  4. import { after, before, describe, it } from 'node:test';
  5. const root = new URL('./fixtures/image-assets/', import.meta.url);
  6. describe('Markdoc - Image assets', () => {
  7. let baseFixture;
  8. before(async () => {
  9. baseFixture = await loadFixture({
  10. root,
  11. });
  12. });
  13. describe('dev', () => {
  14. let devServer;
  15. before(async () => {
  16. devServer = await baseFixture.startDevServer();
  17. });
  18. after(async () => {
  19. await devServer.stop();
  20. });
  21. it('uses public/ image paths unchanged', async () => {
  22. const res = await baseFixture.fetch('/');
  23. const html = await res.text();
  24. const { document } = parseHTML(html);
  25. assert.equal(document.querySelector('#public > img')?.src, '/favicon.svg');
  26. });
  27. it('transforms relative image paths to optimized path', async () => {
  28. const res = await baseFixture.fetch('/');
  29. const html = await res.text();
  30. const { document } = parseHTML(html);
  31. assert.match(
  32. document.querySelector('#relative > img')?.src,
  33. /\/_image\?href=.*%2Fsrc%2Fassets%2Frelative%2Foar.jpg%3ForigWidth%3D420%26origHeight%3D630%26origFormat%3Djpg&f=webp/
  34. );
  35. });
  36. it('transforms aliased image paths to optimized path', async () => {
  37. const res = await baseFixture.fetch('/');
  38. const html = await res.text();
  39. const { document } = parseHTML(html);
  40. assert.match(
  41. document.querySelector('#alias > img')?.src,
  42. /\/_image\?href=.*%2Fsrc%2Fassets%2Falias%2Fcityscape.jpg%3ForigWidth%3D420%26origHeight%3D280%26origFormat%3Djpg&f=webp/
  43. );
  44. });
  45. it('passes images inside image tags to configured image component', async () => {
  46. const res = await baseFixture.fetch('/');
  47. const html = await res.text();
  48. const { document } = parseHTML(html);
  49. assert.equal(document.querySelector('#component > img')?.className, 'custom-styles');
  50. });
  51. });
  52. describe('build', () => {
  53. before(async () => {
  54. await baseFixture.build();
  55. });
  56. it('uses public/ image paths unchanged', async () => {
  57. const html = await baseFixture.readFile('/index.html');
  58. const { document } = parseHTML(html);
  59. assert.equal(document.querySelector('#public > img')?.src, '/favicon.svg');
  60. });
  61. it('transforms relative image paths to optimized path', async () => {
  62. const html = await baseFixture.readFile('/index.html');
  63. const { document } = parseHTML(html);
  64. assert.match(document.querySelector('#relative > img')?.src, /^\/_astro\/oar.*\.webp$/);
  65. });
  66. it('transforms aliased image paths to optimized path', async () => {
  67. const html = await baseFixture.readFile('/index.html');
  68. const { document } = parseHTML(html);
  69. assert.match(document.querySelector('#alias > img')?.src, /^\/_astro\/cityscape.*\.webp$/);
  70. });
  71. it('passes images inside image tags to configured image component', async () => {
  72. const html = await baseFixture.readFile('/index.html');
  73. const { document } = parseHTML(html);
  74. assert.equal(document.querySelector('#component > img')?.className, 'custom-styles');
  75. assert.match(document.querySelector('#component > img')?.src, /^\/_astro\/oar.*\.webp$/);
  76. });
  77. });
  78. });