astro/packages/integrations/image/test/sharp.test.js
Tony Sullivan 00c605ce35
Image integration refactor and cleanup (#4482)
* 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>
2022-08-30 21:09:44 +00:00

60 lines
2 KiB
JavaScript

import { expect } from 'chai';
import sharp from '../dist/loaders/sharp.js';
describe('Sharp service', () => {
describe('serializeTransform', () => {
const src = '/assets/image.png';
[
['only requires src', { src }],
['quality', { src, quality: 80 }],
['format', { src, format: 'jpeg' }],
['width', { src, width: 1280 }],
['height', { src, height: 414 }],
['width & height', { src, height: 400, width: 200 }],
['aspect ratio string', { src, aspectRatio: '16:9' }],
['aspect ratio float', { src, aspectRatio: 1.7 }],
].forEach(([description, props]) => {
it(description, async () => {
const { searchParams } = await sharp.serializeTransform(props);
function verifyProp(expected, search) {
if (expected) {
expect(searchParams.get(search)).to.equal(expected.toString());
} else {
expect(searchParams.has(search)).to.be.false;
}
}
verifyProp(props.quality, 'q');
verifyProp(props.format, 'f');
verifyProp(props.width, 'w');
verifyProp(props.height, 'h');
verifyProp(props.aspectRatio, 'ar');
});
});
});
describe('parseTransform', async () => {
const src = '/assets/image.png';
const href = encodeURIComponent(src);
[
['only requires src', `href=${href}`, { src }],
['quality', `q=80&href=${href}`, { src, quality: 80 }],
['format', `f=jpeg&href=${href}`, { src, format: 'jpeg' }],
['width', `w=1280&href=${href}`, { src, width: 1280 }],
['height', `h=414&href=${href}`, { src, height: 414 }],
['width & height', `w=200&h=400&href=${href}`, { src, height: 400, width: 200 }],
['aspect ratio string', `ar=16:9&href=${href}`, { src, aspectRatio: '16:9' }],
['aspect ratio float', `ar=1.7&href=${href}`, { src, aspectRatio: 1.7 }],
].forEach(([description, params, expected]) => {
it(description, async () => {
const searchParams = new URLSearchParams(params);
const props = sharp.parseTransform(searchParams);
expect(props).to.deep.equal(expected);
});
});
});
});