Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial core image tests #6381

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.