mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
00c605ce35
* WIP: simplifying the use of `fs` vs. the vite plugin * removing a few node deps (etag and node:path) * adding ts defs for sharp * using the same mime package as astro's core App * fixing file URL support in windows * using file URLs when loading local image metadata * fixing a bug in the etag helper * Windows compat * splitting out dev & build tests * why do these suites fail in parallel? * one last windows compat case * Adding tests for treating /public images the same as remote URLs * a couple fixes for Astro's `base` config * adding base path tests for SSR * fixing a bad merge, lost the kleur dependency * adding a test suite for images + MDX * chore: add changeset * simplifying the with-mdx tests * bugfix: don't duplicate the period when using existing file extensions * let Vite cache the image loader service * adding some docs for using /public images * fixing changeset * Update packages/integrations/image/README.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update packages/integrations/image/README.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * nit: minor README syntax tweaks Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
101 lines
2.7 KiB
JavaScript
101 lines
2.7 KiB
JavaScript
import { expect } from 'chai';
|
|
import * as cheerio from 'cheerio';
|
|
import sizeOf from 'image-size';
|
|
import { fileURLToPath } from 'url';
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
describe('Images in MDX - build', function () {
|
|
let fixture;
|
|
let $;
|
|
let html;
|
|
|
|
before(async () => {
|
|
fixture = await loadFixture({ root: './fixtures/with-mdx/' });
|
|
await fixture.build();
|
|
|
|
html = await fixture.readFile('/index.html');
|
|
$ = cheerio.load(html);
|
|
});
|
|
|
|
function verifyImage(pathname, expected) {
|
|
const url = new URL('./fixtures/with-mdx/dist/' + pathname, import.meta.url);
|
|
const dist = fileURLToPath(url);
|
|
const result = sizeOf(dist);
|
|
expect(result).to.deep.equal(expected);
|
|
}
|
|
|
|
describe('Local images', () => {
|
|
it('includes <img> attributes', () => {
|
|
const image = $('#social-jpg');
|
|
|
|
expect(image.attr('src')).to.equal('/social.cece8c77_1zwatP.jpg');
|
|
expect(image.attr('width')).to.equal('506');
|
|
expect(image.attr('height')).to.equal('253');
|
|
});
|
|
|
|
it('built the optimized image', () => {
|
|
verifyImage('social.cece8c77_1zwatP.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
});
|
|
});
|
|
|
|
describe('Inline imports', () => {
|
|
it('includes <img> attributes', () => {
|
|
const image = $('#inline');
|
|
|
|
expect(image.attr('src')).to.equal('/social.cece8c77_Z2tF99.jpg');
|
|
expect(image.attr('width')).to.equal('506');
|
|
expect(image.attr('height')).to.equal('253');
|
|
});
|
|
|
|
it('built the optimized image', () => {
|
|
verifyImage('social.cece8c77_Z2tF99.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
});
|
|
});
|
|
|
|
describe('Remote images', () => {
|
|
// Hard-coding in the test! These should never change since the hash is based
|
|
// on the static `src` string
|
|
const HASH = 'Z1RBHqs';
|
|
const HASH_WITH_QUERY = 'Z17oujH';
|
|
|
|
it('includes <img> attributes', () => {
|
|
const image = $('#google');
|
|
|
|
expect(image.attr('src')).to.equal(
|
|
`/googlelogo_color_272x92dp_${HASH}.webp`
|
|
);
|
|
expect(image.attr('width')).to.equal('544');
|
|
expect(image.attr('height')).to.equal('184');
|
|
});
|
|
|
|
it('built the optimized image', () => {
|
|
verifyImage(`/googlelogo_color_272x92dp_${HASH}.webp`, {
|
|
width: 544,
|
|
height: 184,
|
|
type: 'webp',
|
|
});
|
|
});
|
|
|
|
it('removes query strings', () => {
|
|
verifyImage(`/googlelogo_color_272x92dp_${HASH_WITH_QUERY}.webp`, {
|
|
width: 544,
|
|
height: 184,
|
|
type: 'webp',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('/public images', () => {
|
|
it('includes <img> attributes', () => {
|
|
const image = $('#hero');
|
|
|
|
expect(image.attr('src')).to.equal('/hero_Z2k1JGN.webp');
|
|
expect(image.attr('width')).to.equal('768');
|
|
expect(image.attr('height')).to.equal('414');
|
|
});
|
|
|
|
it('built the optimized image', () => {
|
|
verifyImage('hero_Z2k1JGN.webp', { width: 768, height: 414, type: 'webp' });
|
|
});
|
|
});
|
|
});
|