Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UstymUkhman committed Jun 25, 2022
1 parent 47aa8bb commit 552b340
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![GitHub deployments](https://img.shields.io/github/deployments/UstymUkhman/threejs-boilerplate/github-pages)
![GitHub repo size](https://img.shields.io/github/repo-size/UstymUkhman/threejs-boilerplate)
![](https://img.shields.io/badge/coverage-98.5%25-green)
![](https://img.shields.io/badge/coverage-99.2%25-green)
![GitHub package.json version](https://img.shields.io/github/package-json/v/UstymUkhman/threejs-boilerplate?color=brightgreen)
![GitHub](https://img.shields.io/github/license/UstymUkhman/threejs-boilerplate?color=brightgreen)

Expand Down
69 changes: 28 additions & 41 deletions tests/utils/AssetsLoader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { LoadingManager } from 'three/src/loaders/LoadingManager';
import { CubeTexture } from 'three/src/textures/CubeTexture';
import { Texture } from 'three/src/textures/Texture';

import { RGBAFormat } from 'three/src/constants';
import { Group } from 'three/src/objects/Group';
import { Assets } from '@/utils/AssetsLoader';
import { Group } from 'three/src/objects/Group';
import { RGBAFormat } from 'three/src/constants';

import { describe, test, expect, vi } from 'vitest';
import { Texture } from 'three/src/textures/Texture';
import { CubeTexture } from 'three/src/textures/CubeTexture';
import { LoadingManager } from 'three/src/loaders/LoadingManager';

describe('AssetsLoader', () => {
const loader = new Assets.Loader();
Expand All @@ -14,9 +15,9 @@ describe('AssetsLoader', () => {
expect(loader).toBeInstanceOf(LoadingManager);
});

test('getPromiseCallbacks', done => {
test('getPromiseCallbacks', () => {
const loaderPrototype = Object.getPrototypeOf(loader);
const getPromiseCallbacks = jest.fn(loaderPrototype.getPromiseCallbacks.bind(loaderPrototype));
const getPromiseCallbacks = vi.fn(loaderPrototype.getPromiseCallbacks.bind(loader));

new Promise((resolve, reject) => {
const callbacks = getPromiseCallbacks(resolve, reject) as Assets.Callbacks;
Expand All @@ -36,65 +37,51 @@ describe('AssetsLoader', () => {
const callbacks = getPromiseCallbacks(resolve, reject) as Assets.Callbacks;
expect(callbacks.onError(error)).rejects.toStrictEqual(error);
});

done();
});

test('loadCubeTexture', done => {
loader.loadCubeTexture('skybox').then(cubeTexture => {
expect(cubeTexture).toBeInstanceOf(CubeTexture);
expect(cubeTexture.images.length).toStrictEqual(6);
});

done();
test('loadCubeTexture', async () => {
const cubeTexture = await loader.loadCubeTexture('skybox');
expect(cubeTexture).toBeInstanceOf(CubeTexture);
expect(cubeTexture.images.length).toStrictEqual(6);
});

test('loadTexture', done => {
loader.loadTexture('AK47.png').then(texture => {
expect(texture).toBeInstanceOf(Texture);
expect(texture.image).toBeInstanceOf(Image);
});

done();
test('loadTexture', async () => {
const texture = await loader.loadTexture('texture.png');
expect(texture).toBeInstanceOf(Texture);
expect(texture.image).toBeInstanceOf(Image);
});

test('loadGLTF', done => {
loader.loadGLTF('AK47.glb').then(model => {
expect(model.scene).toBeInstanceOf(Group);
expect(model).toEqual({ scene: new Group() });
});

done();
test('loadGLTF', async () => {
const model = await loader.loadGLTF('model.glb');
expect(model.scene).toBeInstanceOf(Group);
expect(model).toEqual({ scene: new Group() });
});

test('loadAudio', done => {
loader.loadAudio('scream.mp3').then(audio => {
expect(audio).toBeInstanceOf(AudioBuffer);
});

done();
test('loadAudio', async () => {
const audio = await loader.loadAudio('audio.mp3');
expect(audio).toBeInstanceOf(AudioBuffer);
});

test('onProgress', () => {
const onProgress = jest.fn(loader.onProgress);
const onProgress = vi.fn(loader.onProgress);
onProgress('', 1, 1);
expect(onProgress).toHaveReturnedWith(undefined);
});

test('onError', () => {
const onError = jest.fn(loader.onError);
const onError = vi.fn(loader.onError);
onError('');
expect(onError).toHaveReturnedWith(undefined);
});

test('onStart', () => {
const onStart = jest.fn(loader.onStart);
const onStart = vi.fn(loader.onStart);
onStart();
expect(onStart).toHaveReturnedWith(undefined);
});

test('onLoad', () => {
const onLoad = jest.fn(loader.onLoad);
const onLoad = vi.fn(loader.onLoad);
onLoad();
expect(onLoad).toHaveReturnedWith(undefined);
});
Expand Down
10 changes: 6 additions & 4 deletions tests/utils/CustomEvents.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, test, expect, vi } from 'vitest';
import { CustomEvents, Event } from '@/utils/CustomEvents';

const timestamp = Date.now();
Expand All @@ -17,7 +18,7 @@ describe('CustomEvents', () => {
const event = new Event('time');
event.data = timestamp;

const callback = jest.fn((event: Event) => {
const callback = vi.fn((event: Event) => {
expect(event).toStrictEqual(event);
expect(event.data).toStrictEqual(timestamp);
});
Expand All @@ -35,14 +36,15 @@ describe('CustomEvents', () => {
test('remove', () => {
CustomEvents.remove('time');
CustomEvents.dispatch('time', timestamp);
expect(callback).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledOnce();
});

test('dispose', () => {
CustomEvents.add('time', callback);
const callback = vi.fn();
CustomEvents.add('event', callback);
CustomEvents.dispose();

CustomEvents.dispatch('time', timestamp);
CustomEvents.dispatch('event');
expect(callback).not.toHaveBeenCalled();
});
});
12 changes: 5 additions & 7 deletions tests/utils/worker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ describe('Worker', () => {
});

test('add', () => {
const add = vi.fn(worker.add.bind(worker, 'message', () => null));
add();
expect(add).toHaveReturnedWith(undefined);
const add = worker.add('event', vi.fn);
expect(add).toStrictEqual(undefined);
});

test('get', () => {
const get = vi.fn(worker.get.bind(worker, 'message'));
get();
expect(get).toHaveReturnedWith(undefined);
const get = worker.get('event');
expect(get).toStrictEqual(undefined);
});

test('onMessage', () => {
const event = new MessageEvent('error', {
const event = new MessageEvent('message', {
data: { name: 'message', response: null }
});

Expand Down

0 comments on commit 552b340

Please sign in to comment.