-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial core image tests (#6381)
- Loading branch information
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}); | ||
}) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
packages/astro/test/fixtures/core-image/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/core-image/src/pages/remote-error-no-dimensions.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/core-image/src/pages/remote-error-no-height.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.