silphius/app/ecs/component.test.js
2024-06-26 10:41:09 -05:00

57 lines
1.6 KiB
JavaScript

import {expect, test} from 'vitest';
import Schema from './schema.js';
import Component from './component.js';
test('creates instances', () => {
class CreatingComponent extends Component {
static schema = new Schema({
type: 'object',
properties: {foo: {defaultValue: 'bar', type: 'string'}},
});
}
const ComponentInstance = new CreatingComponent();
ComponentInstance.create(1);
expect(ComponentInstance.get(1).entity)
.to.equal(1);
});
test('does not serialize default values', () => {
class CreatingComponent extends Component {
static schema = new Schema({
type: 'object',
properties: {foo: {defaultValue: 'bar', type: 'string'}, bar: {type: 'uint8'}},
});
}
const fakeEcs = {markChange() {}};
const ComponentInstance = new CreatingComponent(fakeEcs);
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', () => {
class ReusingComponent extends Component {
static schema = new Schema({
type: 'object',
properties: {foo: {type: 'string'}},
});
}
const ComponentInstance = new ReusingComponent();
ComponentInstance.create(1);
const instance = ComponentInstance.get(1);
ComponentInstance.destroy(1);
expect(ComponentInstance.get(1))
.to.be.undefined;
expect(() => {
ComponentInstance.destroy(1);
})
.to.throw();
ComponentInstance.create(1);
expect(ComponentInstance.get(1))
.to.equal(instance);
});