0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/markdoc/test/image-assets.test.js
Ben Holmes cfcf2e2ffd
[Markdoc] Support automatic image optimization with experimental.assets (#6630)
* wip: scrappy implementation. It works! 🥳

* chore: add code comments on inline utils

* fix: code cleanup, run on experimental.assets

* feat: support ~/assets alias

* fix: spoof `astro:assets` when outside experimental

* test: image paths in dev and prod

* feat: support any vite alias with ctx.resolve

* fix: avoid trying to process absolute paths

* fix: raise helpful error for invalid vite paths

* refactor: revert URL support on emitAsset

* chore: lint

* refactor: expose emitESMImage from assets base

* wip: why doesn't assets exist

* scary chore: make @astrojs/markdoc truly depend on astro

* fix: import emitESMImage straight from dist

* chore: remove type def from assets package

* chore: screw it, just ts ignore

* deps: rollup types

* refactor: optimize images during parse step

* chore: remove unneeded `.flat()`

* fix: use file-based relative paths

* fix: add back helpful error

* chore: changeset

* deps: move astro back to dev dep

* fix: put emit assets behind flag

* chore: change to markdoc patch
2023-03-24 07:58:56 -04:00

76 lines
2.4 KiB
JavaScript

import { parseHTML } from 'linkedom';
import { expect } from 'chai';
import { loadFixture } from '../../../astro/test/test-utils.js';
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);
expect(document.querySelector('#public > img')?.src).to.equal('/favicon.svg');
});
it('transforms relative image paths to optimized path', async () => {
const res = await baseFixture.fetch('/');
const html = await res.text();
const { document } = parseHTML(html);
expect(document.querySelector('#relative > img')?.src).to.equal(
'/_image?href=%2Fsrc%2Fassets%2Frelative%2Foar.jpg%3ForigWidth%3D420%26origHeight%3D630%26origFormat%3Djpg&f=webp'
);
});
it('transforms aliased image paths to optimized path', async () => {
const res = await baseFixture.fetch('/');
const html = await res.text();
const { document } = parseHTML(html);
expect(document.querySelector('#alias > img')?.src).to.equal(
'/_image?href=%2Fsrc%2Fassets%2Falias%2Fcityscape.jpg%3ForigWidth%3D420%26origHeight%3D280%26origFormat%3Djpg&f=webp'
);
});
});
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);
expect(document.querySelector('#public > img')?.src).to.equal('/favicon.svg');
});
it('transforms relative image paths to optimized path', async () => {
const html = await baseFixture.readFile('/index.html');
const { document } = parseHTML(html);
expect(document.querySelector('#relative > img')?.src).to.match(/^\/_astro\/oar.*\.webp$/);
});
it('transforms aliased image paths to optimized path', async () => {
const html = await baseFixture.readFile('/index.html');
const { document } = parseHTML(html);
expect(document.querySelector('#alias > img')?.src).to.match(/^\/_astro\/cityscape.*\.webp$/);
});
});
});