avocado/packages/entity/test/entity.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-01-02 22:01:57 -06:00
import {Trait, traits} from '@avocado/traits';
import {Latus} from '@latus/core';
import {expect} from 'chai';
2021-01-05 11:25:37 -06:00
let latus;
let Entity;
beforeEach(async () => {
latus = Latus.mock({
'@avocado/entity': require('../src'),
'@avocado/resource': require('@avocado/resource'),
'@avocado/traits': require('@avocado/traits'),
2021-01-02 22:01:57 -06:00
});
2021-01-05 11:25:37 -06:00
await Promise.all(latus.invokeFlat('@latus/core/starting'));
2021-01-22 17:58:45 -06:00
({Entity} = latus.get('%resources'));
2021-01-05 11:25:37 -06:00
});
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 {
2021-01-02 22:01:57 -06:00
2021-01-05 11:25:37 -06:00
static get type() {
return 'TestTrait';
}
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
};
2021-01-22 17:58:45 -06:00
latus.set('%traits.TestTrait', TestTrait);
await entity.addTrait('TestTrait');
2021-01-05 11:25:37 -06:00
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 extendJson(json) {
const extended = await super.extendJson(json);
await new Promise((resolve) => setTimeout(resolve, DELAY));
return extended;
2021-01-02 22:01:57 -06:00
}
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
}
2021-01-22 17:58:45 -06:00
latus.set('%traits.Async', AsyncTrait);
2021-01-05 11:25:37 -06:00
let start = Date.now();
const entity = 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 {
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
hooks() {
return {
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
testHook: () => 69,
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
};
}
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
static get type() {
return 'AnotherTrait';
2021-01-04 07:10:48 -06:00
}
2021-01-05 11:25:37 -06:00
}
class YetAnotherTrait extends Trait {
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
hooks() {
return {
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
testHook: () => 420,
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
};
}
2021-01-04 07:10:48 -06:00
2021-01-05 11:25:37 -06:00
static get type() {
return 'YetAnotherTrait';
2021-01-04 07:10:48 -06:00
}
2021-01-05 11:25:37 -06:00
}
2021-01-22 17:58:45 -06:00
latus.set('%traits.AnotherTrait', AnotherTrait);
latus.set('%traits.YetAnotherTrait', YetAnotherTrait);
2021-01-05 11:25:37 -06:00
const entity = new Entity();
await entity.addTrait('AnotherTrait');
await entity.addTrait('YetAnotherTrait');
2021-01-05 11:25:37 -06:00
expect(entity.invokeHook('testHook')).to.deep.equal({AnotherTrait: 69, YetAnotherTrait: 420});
2021-01-02 22:01:57 -06:00
});