2022-07-08 16:37:55 -05:00
|
|
|
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 pictures - build', function () {
|
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/basic-picture/',
|
|
|
|
adapter: testAdapter(),
|
2022-07-24 23:18:02 -05:00
|
|
|
output: 'server',
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
});
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/assets\/social.\w{8}.jpg/ },
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Social image',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/assets\/social.\w{8}.jpg/ },
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Inline social image',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'png',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Google logo',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'jpg', w: '768', h: '414', href: '/hero.jpg' },
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Hero image',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, async () => {
|
|
|
|
const app = await fixture.loadTestAdapterApp();
|
|
|
|
|
|
|
|
const request = new Request('http://example.com/');
|
|
|
|
const response = await app.render(request);
|
|
|
|
const html = await response.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
const sources = $(`${id} source`);
|
2022-07-08 16:40:22 -05:00
|
|
|
|
2022-07-08 16:37:55 -05:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
const image = $(`${id} img`);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
expect(route).to.equal(url);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
} else {
|
|
|
|
expect(searchParams.get(key)).to.match(value);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('SSR pictures with subpath - build', function () {
|
2022-07-08 16:37:55 -05:00
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/basic-picture/',
|
|
|
|
adapter: testAdapter(),
|
2022-07-24 23:18:02 -05:00
|
|
|
output: 'server',
|
2022-08-30 16:12:45 -05:00
|
|
|
base: '/docs',
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
await fixture.build();
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/docs\/assets\/social.\w{8}.jpg/ },
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Social image',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/docs\/assets\/social.\w{8}.jpg/ },
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Inline social image',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'png',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Google logo',
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'jpg', w: '768', h: '414', href: '/hero.jpg' },
|
2022-09-01 10:01:07 -05:00
|
|
|
alt: 'Hero image',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, async () => {
|
|
|
|
const app = await fixture.loadTestAdapterApp();
|
|
|
|
|
|
|
|
const request = new Request('http://example.com/');
|
|
|
|
const response = await app.render(request);
|
|
|
|
const html = await response.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
const sources = $(`${id} source`);
|
2022-07-08 16:40:22 -05:00
|
|
|
|
2022-07-08 16:37:55 -05:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
const image = $(`${id} img`);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
expect(route).to.equal(url);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
} else {
|
|
|
|
expect(searchParams.get(key)).to.match(value);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|