summaryrefslogtreecommitdiff
path: root/packages/integrations/image/test/image-ssr-dev.test.js
diff options
context:
space:
mode:
authorGravatar Tony Sullivan <tony.f.sullivan@outlook.com> 2022-08-30 21:09:44 +0000
committerGravatar GitHub <noreply@github.com> 2022-08-30 21:09:44 +0000
commit00c605ce350be83a07c5855f7b99ee41eee1ee38 (patch)
tree127906c86a66bf962f3aa97e0e4a2539a52d4966 /packages/integrations/image/test/image-ssr-dev.test.js
parent7429664566f05ecebf6d57906f950627e62e690c (diff)
downloadastro-00c605ce350be83a07c5855f7b99ee41eee1ee38.tar.gz
astro-00c605ce350be83a07c5855f7b99ee41eee1ee38.tar.zst
astro-00c605ce350be83a07c5855f7b99ee41eee1ee38.zip
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>
Diffstat (limited to 'packages/integrations/image/test/image-ssr-dev.test.js')
-rw-r--r--packages/integrations/image/test/image-ssr-dev.test.js273
1 files changed, 273 insertions, 0 deletions
diff --git a/packages/integrations/image/test/image-ssr-dev.test.js b/packages/integrations/image/test/image-ssr-dev.test.js
new file mode 100644
index 000000000..62a92a53c
--- /dev/null
+++ b/packages/integrations/image/test/image-ssr-dev.test.js
@@ -0,0 +1,273 @@
+import { expect } from 'chai';
+import * as cheerio from 'cheerio';
+import { loadFixture } from './test-utils.js';
+import testAdapter from '../../../astro/test/test-adapter.js';
+
+describe('SSR images - dev', function () {
+ let fixture;
+ let devServer;
+ let $;
+
+ before(async () => {
+ fixture = await loadFixture({
+ root: './fixtures/basic-image/',
+ adapter: testAdapter(),
+ output: 'server',
+ });
+
+ devServer = await fixture.startDevServer();
+ const html = await fixture.fetch('/').then((res) => res.text());
+ $ = cheerio.load(html);
+ });
+
+ after(async () => {
+ await devServer.stop();
+ });
+
+ describe('Local images', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#social-jpg');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/@astroimage/assets/social.jpg');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('jpg');
+ expect(searchParams.get('w')).to.equal('506');
+ expect(searchParams.get('h')).to.equal('253');
+ });
+
+ it('returns the optimized image', async () => {
+ const image = $('#social-jpg');
+
+ const res = await fixture.fetch(image.attr('src'));
+
+ expect(res.status).to.equal(200);
+ expect(res.headers.get('Content-Type')).to.equal('image/jpeg');
+
+ // TODO: verify image file? It looks like sizeOf doesn't support ArrayBuffers
+ });
+ });
+
+ describe('Inline imports', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#inline');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/@astroimage/assets/social.jpg');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('jpg');
+ expect(searchParams.get('w')).to.equal('506');
+ expect(searchParams.get('h')).to.equal('253');
+ });
+ });
+
+ describe('Remote images', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#google');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/_image');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('webp');
+ expect(searchParams.get('w')).to.equal('544');
+ expect(searchParams.get('h')).to.equal('184');
+ expect(searchParams.get('href')).to.equal(
+ 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
+ );
+ });
+
+ it('keeps remote image query params', () => {
+ const image = $('#query');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/_image');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('webp');
+ expect(searchParams.get('w')).to.equal('544');
+ expect(searchParams.get('h')).to.equal('184');
+ expect(searchParams.get('href')).to.equal(
+ 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png?token=abc'
+ );
+ });
+ });
+
+ describe('/public images', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#hero');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/_image');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('webp');
+ expect(searchParams.get('w')).to.equal('768');
+ expect(searchParams.get('h')).to.equal('414');
+ expect(searchParams.get('href')).to.equal('/hero.jpg');
+ });
+
+ it('returns the optimized image', async () => {
+ const image = $('#hero');
+
+ const res = await fixture.fetch(image.attr('src'));
+
+ expect(res.status).to.equal(200);
+ expect(res.headers.get('Content-Type')).to.equal('image/webp');
+
+ // TODO: verify image file? It looks like sizeOf doesn't support ArrayBuffers
+ });
+ });
+});
+
+describe('SSR images with subpath - dev', function () {
+ let fixture;
+ let devServer;
+ let $;
+
+ before(async () => {
+ fixture = await loadFixture({
+ root: './fixtures/basic-image/',
+ adapter: testAdapter(),
+ output: 'server',
+ base: '/docs'
+ });
+
+ devServer = await fixture.startDevServer();
+ const html = await fixture.fetch('/docs/').then((res) => res.text());
+ $ = cheerio.load(html);
+ });
+
+ after(async () => {
+ await devServer.stop();
+ });
+
+ describe('Local images', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#social-jpg');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/@astroimage/assets/social.jpg');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('jpg');
+ expect(searchParams.get('w')).to.equal('506');
+ expect(searchParams.get('h')).to.equal('253');
+ });
+
+ it('returns the optimized image', async () => {
+ const image = $('#social-jpg');
+
+ const res = await fixture.fetch(image.attr('src'));
+
+ expect(res.status).to.equal(200);
+ expect(res.headers.get('Content-Type')).to.equal('image/jpeg');
+
+ // TODO: verify image file? It looks like sizeOf doesn't support ArrayBuffers
+ });
+ });
+
+ describe('Inline imports', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#inline');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/@astroimage/assets/social.jpg');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('jpg');
+ expect(searchParams.get('w')).to.equal('506');
+ expect(searchParams.get('h')).to.equal('253');
+ });
+ });
+
+ describe('Remote images', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#google');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/_image');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('webp');
+ expect(searchParams.get('w')).to.equal('544');
+ expect(searchParams.get('h')).to.equal('184');
+ expect(searchParams.get('href')).to.equal(
+ 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
+ );
+ });
+
+ it('keeps remote image query params', () => {
+ const image = $('#query');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/_image');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('webp');
+ expect(searchParams.get('w')).to.equal('544');
+ expect(searchParams.get('h')).to.equal('184');
+ expect(searchParams.get('href')).to.equal(
+ 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png?token=abc'
+ );
+ });
+ });
+
+ describe('/public images', () => {
+ it('includes src, width, and height attributes', () => {
+ const image = $('#hero');
+
+ const src = image.attr('src');
+ const [route, params] = src.split('?');
+
+ expect(route).to.equal('/_image');
+
+ const searchParams = new URLSearchParams(params);
+
+ expect(searchParams.get('f')).to.equal('webp');
+ expect(searchParams.get('w')).to.equal('768');
+ expect(searchParams.get('h')).to.equal('414');
+ expect(searchParams.get('href')).to.equal('/hero.jpg');
+ });
+
+ it('returns the optimized image', async () => {
+ const image = $('#hero');
+
+ const res = await fixture.fetch(image.attr('src'));
+
+ expect(res.status).to.equal(200);
+ expect(res.headers.get('Content-Type')).to.equal('image/webp');
+
+ // TODO: verify image file? It looks like sizeOf doesn't support ArrayBuffers
+ });
+ });
+});