Skip to content

Commit

Permalink
Add initial core image tests (#6381)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Feb 28, 2023
1 parent 33238d4 commit 406eff6
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
108 changes: 108 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { expect, assert } from 'chai';
import { loadFixture } from './test-utils.js';
import * as cheerio from 'cheerio';
import { Writable } from 'node:stream';

describe('astro:image', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

describe('dev', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
/** @type {Array<{ type: any, level: 'error', message: string; }>} */
let logs = [];

before(async () => {
fixture = await loadFixture({
root: './fixtures/core-image/',
experimental: {
images: true,
}
});

devServer = await fixture.startDevServer({
logging: {
level: 'error',
dest: new Writable({
objectMode: true,
write(event, _, callback) {
logs.push(event);
callback();
}
})
}
});
});

after(async () => {
await devServer.stop();
});

describe('basics', () => {
let $;
before(async() => {
let res = await fixture.fetch('/');
let html = await res.text();
$ = cheerio.load(html);
});

it('Adds the <img> tag', () => {
let $img = $('#local img');
expect($img).to.have.a.lengthOf(1);
expect($img.attr('src').startsWith('/_image')).to.equal(true);
});

it('includes loading and decoding attributes', () => {
let $img = $('#local img');
expect(!!$img.attr('loading')).to.equal(true);
expect(!!$img.attr('decoding')).to.equal(true);
});

it('includes the provided alt', () => {
let $img = $('#local img');
expect($img.attr('alt')).to.equal('a penguin');
});
});

describe('remote', () => {
describe('working', () => {
let $;
before(async () => {
let res = await fixture.fetch('/');
let html = await res.text();
$ = cheerio.load(html);
});

it('includes the provided alt', async () => {
let $img = $('#remote img');
expect($img.attr('alt')).to.equal('fred');
});

it('includes loading and decoding attributes', () => {
let $img = $('#remote img');
expect(!!$img.attr('loading')).to.equal(true);
expect(!!$img.attr('decoding')).to.equal(true);
});
});

it('error if no width and height', async () => {
logs.length = 0;
let res = await fixture.fetch('/remote-error-no-dimensions');
await res.text();

expect(logs).to.have.a.lengthOf(1);
expect(logs[0].message).to.contain('For remote images, width and height are required.');
});

it('error if no height', async () => {
logs.length = 0;
let res = await fixture.fetch('/remote-error-no-height');
await res.text();

expect(logs).to.have.a.lengthOf(1);
expect(logs[0].message).to.contain('For remote images, height is required.');
});
})
});
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/core-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/core-image",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions packages/astro/test/fixtures/core-image/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
import { Image } from 'astro:assets';
import myImage from "../assets/penguin1.jpg";
---
<html>
<head>

</head>
<body>
<div id="local">
<Image src={myImage} alt="a penguin" />
</div>

<div id="remote">
<Image src="https://avatars.githubusercontent.com/u/622227?s=64" alt="fred" width="48" height="48" />
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import { Image } from 'astro:assets';
---
<html>
<head>

</head>
<body>
<Image src="https://avatars.githubusercontent.com/u/622227?s=64" alt="fred" />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import { Image } from 'astro:assets';
---
<html>
<head>

</head>
<body>
<Image src="https://avatars.githubusercontent.com/u/622227?s=64" alt="fred" width="48" />
</body>
</html>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 406eff6

Please sign in to comment.