Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 10, 2024
1 parent fbeaa3e commit 3bdc485
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
run: npm ci || npm install
- name: build
run: npm run build --if-present
- name: AVA
run: npx ava
- name: Vitest
run: npx vitest
Build:
runs-on: ubuntu-latest
steps:
Expand Down
123 changes: 123 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {test, assert} from 'vitest';
import {loadImage, loadImages} from './index.js';

declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/naming-convention -- Must extend it
interface HTMLImageElement {
_loadMetadata(): void;
_load(): void;
}
}

// @ts-expect-error Just a fake class for testing
globalThis.Image = class Image {
#src = '';
#complete = false;
#naturalWidth = 0;
#naturalHeight = 0;

_loadMetadata() {
this.#naturalWidth = 16;
this.#naturalHeight = 16;
}

_load() {
this.#complete = true;
}

get src() {
return this.#src;
}

set src(value) {
this.#src = value;
}

get complete() {
return this.#complete;
}

set complete(value) {
this.#complete = value;
}

get naturalWidth() {
return this.#naturalWidth;
}

set naturalWidth(value) {
this.#naturalWidth = value;
}

get naturalHeight() {
return this.#naturalHeight;
}

set naturalHeight(value) {
this.#naturalHeight = value;
}

decode() {
return new Promise<void>(resolve => {
setInterval(() => {
if (this.#complete) {
resolve();
}
}, 5);
});
}
};

const source = 'https://avatars.githubusercontent.com/u/1402241?s=96&v=4';
const source2 = 'https://avatars.githubusercontent.com/u/1402241';

test('loadImage', async () => {
const info = loadImage(source);

assert.equal(info.image.src, source);
assert.isFalse(info.image.complete);

info.image._loadMetadata();
await info.metadata();

assert.isFalse(info.image.complete);
assert.equal(info.image.naturalWidth, 16);
assert.equal(info.image.naturalHeight, 16);

info.image._load();
assert.equal(await info.loaded(), info.image);

assert.isTrue(info.image.complete);
});

test('loadImages', async () => {
const images = loadImages([source, source2]);

console.log(images);

assert.equal(images.results[0].image.src, source);
assert.equal(images.results[1].image.src, source2);

for (const info of images.results) {
assert.isFalse(info.image.complete);
info.image._loadMetadata();
}

await images.metadata();

for (const info of images.results) {
assert.isFalse(info.image.complete);
assert.equal(info.image.naturalWidth, 16);
assert.equal(info.image.naturalHeight, 16);
}

for (const info of images.results) {
info.image._load();
}

await images.loaded();

for (const info of images.results) {
assert.isTrue(info.image.complete);
}
});

0 comments on commit 3bdc485

Please sign in to comment.