54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
import {expect, test} from 'vitest';
|
|
|
|
import Component from './component.js';
|
|
|
|
const fakeEcs = {markChange() {}};
|
|
|
|
test('creates instances', async () => {
|
|
class CreatingComponent extends Component {
|
|
static properties = {
|
|
foo: {defaultValue: 'bar', type: 'string'},
|
|
};
|
|
}
|
|
const ComponentInstance = new CreatingComponent(fakeEcs);
|
|
await ComponentInstance.create(1);
|
|
expect(ComponentInstance.get(1).entity)
|
|
.to.equal(1);
|
|
});
|
|
|
|
test('does not serialize default values', async () => {
|
|
class CreatingComponent extends Component {
|
|
static properties = {
|
|
foo: {defaultValue: 'bar', type: 'string'}, bar: {type: 'uint8'},
|
|
};
|
|
}
|
|
const ComponentInstance = new CreatingComponent(fakeEcs);
|
|
await ComponentInstance.create(1)
|
|
expect(ComponentInstance.get(1).toJSON())
|
|
.to.deep.equal({});
|
|
ComponentInstance.get(1).bar = 1;
|
|
expect(ComponentInstance.get(1).toJSON())
|
|
.to.deep.equal({bar: 1});
|
|
});
|
|
|
|
test('reuses instances', async () => {
|
|
class ReusingComponent extends Component {
|
|
static properties = {
|
|
foo: {type: 'string'},
|
|
};
|
|
}
|
|
const ComponentInstance = new ReusingComponent(fakeEcs);
|
|
await ComponentInstance.create(1);
|
|
const instance = ComponentInstance.get(1);
|
|
ComponentInstance.destroy(1);
|
|
expect(ComponentInstance.get(1))
|
|
.to.be.undefined;
|
|
expect(() => {
|
|
ComponentInstance.destroy(1);
|
|
})
|
|
.to.throw();
|
|
await ComponentInstance.create(1);
|
|
expect(ComponentInstance.get(1))
|
|
.to.equal(instance);
|
|
});
|