104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
/* eslint-disable global-require */
|
|
import {Trait} from '@avocado/traits';
|
|
import {Flecks} from '@flecks/core';
|
|
import {expect} from 'chai';
|
|
|
|
let flecks;
|
|
let Entity;
|
|
beforeEach(async () => {
|
|
flecks = await Flecks.from({
|
|
flecks: {
|
|
'@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'),
|
|
},
|
|
});
|
|
({Entity} = flecks.avocado.resource.Resources);
|
|
});
|
|
afterEach(() => {
|
|
flecks.destroy();
|
|
});
|
|
it('has sane defaults', () => {
|
|
const entity = new Entity();
|
|
expect(entity.traits).to.deep.equal({});
|
|
expect(entity.traitTypes()).to.deep.equal([]);
|
|
});
|
|
it('can add and remove traits', async () => {
|
|
const entity = new Entity();
|
|
const TestTrait = class extends Trait {
|
|
|
|
static get type() {
|
|
return 'TestTrait';
|
|
}
|
|
|
|
};
|
|
flecks.avocado.traits.Traits.TestTrait = TestTrait;
|
|
await entity.addTrait('TestTrait');
|
|
expect(entity.is('TestTrait')).to.be.true;
|
|
entity.removeTrait('TestTrait');
|
|
expect(entity.is('TestTrait')).to.be.false;
|
|
});
|
|
it('can add traits asynchronously', async () => {
|
|
const DELAY = 30;
|
|
class AsyncTrait extends Trait {
|
|
|
|
static async load(...args) {
|
|
const loaded = await super.load(...args);
|
|
await new Promise((resolve) => {
|
|
setTimeout(resolve, DELAY);
|
|
});
|
|
return loaded;
|
|
}
|
|
|
|
}
|
|
flecks.avocado.traits.Traits.Async = AsyncTrait;
|
|
const start = Date.now();
|
|
await Entity.load({
|
|
traits: {
|
|
Async: {},
|
|
},
|
|
});
|
|
expect(Date.now() - start).to.be.at.least(DELAY * 0.9);
|
|
});
|
|
it('can invoke hooks', async () => {
|
|
class AnotherTrait extends Trait {
|
|
|
|
hooks() {
|
|
return {
|
|
|
|
testHook: () => 69,
|
|
|
|
};
|
|
}
|
|
|
|
static get type() {
|
|
return 'AnotherTrait';
|
|
}
|
|
|
|
}
|
|
class YetAnotherTrait extends Trait {
|
|
|
|
hooks() {
|
|
return {
|
|
|
|
testHook: () => 420,
|
|
|
|
};
|
|
}
|
|
|
|
static get type() {
|
|
return 'YetAnotherTrait';
|
|
}
|
|
|
|
}
|
|
flecks.avocado.traits.Traits.AnotherTrait = AnotherTrait;
|
|
flecks.avocado.traits.Traits.YetAnotherTrait = YetAnotherTrait;
|
|
const entity = new Entity();
|
|
await entity.addTrait('AnotherTrait');
|
|
await entity.addTrait('YetAnotherTrait');
|
|
expect(entity.invokeHook('testHook')).to.deep.equal({AnotherTrait: 69, YetAnotherTrait: 420});
|
|
});
|