2022-08-30 16:09:44 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 544, height: 184, type: 'webp' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/hero_\w{4,10}.webp/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-07 12:22:11 -05:00
|
|
|
{
|
|
|
|
title: 'Background color',
|
|
|
|
id: '#bg-color',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
|
2022-09-07 12:22:11 -05:00
|
|
|
size: { width: 544, height: 184, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
].forEach(({ title, id, regex, size }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.match(regex);
|
|
|
|
expect(image.attr('width')).to.equal(size.width.toString());
|
|
|
|
expect(image.attr('height')).to.equal(size.height.toString());
|
|
|
|
|
|
|
|
verifyImage(image.attr('src'), size);
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|