fix: tests

This commit is contained in:
cha0s 2021-01-22 11:35:23 -06:00
parent ac0b951ef0
commit cecea48996
5 changed files with 47 additions and 8 deletions

View File

@ -1,4 +1,5 @@
import {resource} from '@avocado/resource';
import {Trait} from '@avocado/traits';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -48,8 +49,48 @@ describe('Existent', () => {
entity.on('destroyed', resolve);
}),
]);
entity.destroy();
await entity.destroy();
expect(entity.traits).to.deep.equal({});
return promise;
});
it('can be destroyed with deference', async () => {
const DELAY = 30;
class Waiter extends Trait {
constructor() {
super();
this.duration = DELAY / 1000;
}
hooks() {
return {
destroy: () => (elapsed) => {
this.duration -= elapsed;
return this.duration <= 0;
},
};
}
}
latus.get('%traits.fromType')['Waiter'] = Waiter;
await entity.addTrait('Waiter');
const promise = Promise.all([
new Promise((resolve) => {
entity.on('destroying', resolve);
}),
new Promise((resolve) => {
entity.on('destroyed', resolve);
}),
]);
let start = Date.now();
const handle = setInterval(() => {
entity.tick(0.01);
}, 10);
await entity.destroy();
clearInterval(handle);
expect(Date.now() - start).to.be.at.least(DELAY * 0.9);
return promise;
});
});

View File

@ -90,7 +90,7 @@ describe('Spawner', () => {
});
}
expect(Object.keys(list.entities)).to.have.lengthOf(COUNT + 1);
entity.killAllChildren();
await entity.killAllChildren();
expect(Object.keys(list.entities)).to.have.lengthOf(1);
});
});

View File

@ -1,5 +1,4 @@
import {assert, expect} from 'chai';
import merge from 'deepmerge';
import {validate} from 'uuid';
import {Image} from '../src/resources/image';

View File

@ -1,5 +1,4 @@
import {assert, expect} from 'chai';
import merge from 'deepmerge';
import {validate} from 'uuid';
import Resource from '../src/resource';
@ -71,7 +70,7 @@ describe('JsonResource', () => {
});
it("can load to instance", async () => {
const resource = new JsonResourceSubclass();
resource.load({foo: 420, bar: 'hoo'});
await resource.load({foo: 420, bar: 'hoo'});
expect(resource.json.foo).to.equal(420);
expect(resource.json.bar).to.equal('hoo');
});

View File

@ -21,20 +21,20 @@ describe('Layer', () => {
});
describe('entity list interaction', () => {
let entity;
let entityList;
let layer;
beforeEach(async () => {
const {fromResourceType: {Entity, EntityList, Layer}} = resource(latus);
entity = await Entity.load({
traits: {
Existent: {},
Layered: {},
Listed: {},
},
});
entityList = new EntityList();
const entityList = new EntityList();
entityList.addEntity(entity);
layer = new Layer();
await layer.load({entityList});
layer.setEntityList(entityList);
})
it('delegates to entity list', async () => {
expect(Object.keys(layer.entities).length).to.equal(1);