avocado/packages/entity/test/spawner.js
2024-01-28 11:51:17 -06:00

99 lines
2.5 KiB
JavaScript

/* eslint-disable global-require */
import {Flecks} from '@flecks/core';
import {expect} from 'chai';
let flecks;
let Entity;
let EntityList;
beforeEach(async () => {
flecks = await Flecks.from({
flecks: {
'@avocado/behavior': require('@avocado/behavior'),
'@avocado/entity': require('@avocado/entity'),
'@avocado/graphics': require('@avocado/graphics'),
'@avocado/resource': require('@avocado/resource'),
'@avocado/traits': require('@avocado/traits'),
'@flecks/core': require('@flecks/core'),
'@flecks/react': require('@flecks/react'),
'@flecks/socket': require('@flecks/socket'),
},
});
({Entity, EntityList} = flecks.avocado.resource.Resources);
});
afterEach(() => {
flecks.destroy();
});
describe('Spawner', () => {
let entity;
let list;
beforeEach(async () => {
entity = await Entity.load({
traits: {
Spawner: {
params: {
spawns: {
testy: {
traits: {
Alive: {},
Positioned: {},
},
},
},
},
},
},
});
list = new EntityList();
list.addEntity(entity);
});
it('exists', async () => {
expect(entity.is('Spawner')).to.be.true;
});
it('can spawn from key', async () => {
const spawned = await entity.spawn('testy');
expect(spawned.is('Alive')).to.be.true;
const spawned2 = await entity.spawn('testy', {
traits: {
Alive: {
state: {
life: 50,
},
},
},
});
expect(spawned2.life).to.equal(50);
const spawned3 = await entity.spawnAt('testy', [69, 420]);
expect(spawned3.position).to.deep.equal([69, 420]);
});
it('can spawn from arbitrary JSON', async () => {
const spawned = await entity.spawnRaw({
traits: {
Mobile: {},
},
});
expect(spawned.is('Mobile')).to.be.true;
const spawned2 = await entity.spawnRawAt(
{
traits: {
Mobile: {},
},
},
[311, 200],
);
expect(spawned2.position).to.deep.equal([311, 200]);
});
it('can kill all children', async () => {
const COUNT = 15;
await Promise.all(Array(COUNT).fill(0).map(() => (
entity.spawnRaw({
traits: {
Mobile: {},
},
})
)));
expect(Object.keys(list.entities)).to.have.lengthOf(COUNT + 1);
await entity.killAllChildren();
expect(Object.keys(list.entities)).to.have.lengthOf(1);
});
});