silphius/app/particles/emitter.test.js

51 lines
1.2 KiB
JavaScript

import {expect, test} from 'vitest';
import Components from '@/ecs/components/index.js';
import Ecs from '@/ecs/ecs.js';
import Emitter from './emitter.js';
test('emits particles at once', async () => {
const ecs = new Ecs({
Components,
});
const emitter = new Emitter(ecs);
const stream = emitter.emit({
count: 5,
frequency: 0,
entity: {},
});
expect(await stream.scan((r) => r + 1, 0).toPromise()).to.equal(5);
});
test('emits particles over time', async () => {
const ecs = new Ecs({
Components,
});
const emitter = new Emitter(ecs);
const stream = emitter.emit({
count: 2,
frequency: 0.1,
});
const current = stream.toProperty();
expect(await new Promise((resolve) => {
current.onValue(resolve);
}))
.to.deep.include({id: 1});
expect(Array.from(ecs.$$detached))
.to.deep.equal([2]);
emitter.tick(0.06);
expect(await new Promise((resolve) => {
current.onValue(resolve);
}))
.to.deep.include({id: 1});
emitter.tick(0.06);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(await new Promise((resolve) => {
current.onValue(resolve);
}))
.to.deep.include({id: 2});
expect(Array.from(ecs.$$detached))
.to.deep.equal([]);
});