2021-01-02 22:01:57 -06:00
|
|
|
import {resource} from '@avocado/resource';
|
|
|
|
import {Trait, traits} from '@avocado/traits';
|
|
|
|
import {Latus} from '@latus/core';
|
|
|
|
import {expect} from 'chai';
|
|
|
|
|
|
|
|
const {name} = require('../package.json');
|
|
|
|
|
|
|
|
describe(name, () => {
|
|
|
|
let latus;
|
|
|
|
let Entity;
|
|
|
|
beforeEach(async () => {
|
|
|
|
latus = Latus.mock([
|
|
|
|
['@avocado/entity', `${__dirname}/../src`],
|
|
|
|
'@avocado/resource',
|
|
|
|
'@avocado/traits',
|
|
|
|
]);
|
|
|
|
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
|
|
|
({fromResourceType: {Entity}} = resource(latus));
|
|
|
|
});
|
|
|
|
it('has sane defaults', () => {
|
|
|
|
const entity = new Entity();
|
2021-01-04 06:44:32 -06:00
|
|
|
expect(entity.traits).to.deep.equal({});
|
2021-01-04 07:00:08 -06:00
|
|
|
expect(entity.traitTypes()).to.deep.equal([]);
|
2021-01-02 22:01:57 -06:00
|
|
|
});
|
|
|
|
it('can add and remove traits', async () => {
|
|
|
|
const entity = new Entity();
|
|
|
|
const TestTrait = class extends Trait {
|
|
|
|
|
|
|
|
static get type() {
|
|
|
|
return 'TestTrait';
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
traits(latus).fromType['TestTrait'] = TestTrait;
|
|
|
|
const trait = await TestTrait.load();
|
2021-01-04 07:10:48 -06:00
|
|
|
entity.addTrait(TestTrait);
|
2021-01-03 18:02:05 -06:00
|
|
|
expect(entity.is('TestTrait')).to.be.true;
|
2021-01-02 22:01:57 -06:00
|
|
|
entity.removeTrait('TestTrait');
|
2021-01-03 18:02:05 -06:00
|
|
|
expect(entity.is('TestTrait')).to.be.false;
|
2021-01-02 22:01:57 -06:00
|
|
|
});
|
|
|
|
it('can add traits asynchronously', async () => {
|
|
|
|
const DELAY = 30;
|
|
|
|
class AsyncTrait extends Trait {
|
|
|
|
|
|
|
|
static async extendJson(json) {
|
|
|
|
const extended = await super.extendJson(json);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, DELAY));
|
|
|
|
return extended;
|
|
|
|
}
|
2021-01-04 07:10:48 -06:00
|
|
|
|
2021-01-02 22:01:57 -06:00
|
|
|
}
|
|
|
|
traits(latus).fromType['Async'] = AsyncTrait;
|
|
|
|
let start = Date.now();
|
|
|
|
const entity = await Entity.load({
|
|
|
|
traits: {
|
|
|
|
Async: {},
|
|
|
|
},
|
|
|
|
});
|
2021-01-03 21:43:21 -06:00
|
|
|
expect(Date.now() - start).to.be.at.least(DELAY * 0.9);
|
2021-01-02 22:01:57 -06:00
|
|
|
});
|
2021-01-04 07:10:48 -06:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
const entity = new Entity();
|
|
|
|
entity.addTrait(AnotherTrait);
|
|
|
|
entity.addTrait(YetAnotherTrait);
|
|
|
|
expect(entity.invokeHook('testHook')).to.deep.equal({AnotherTrait: 69, YetAnotherTrait: 420});
|
|
|
|
});
|
2021-01-02 22:01:57 -06:00
|
|
|
});
|