silphius/app/ecs/component.test.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-06-26 10:33:31 -05:00
import {expect, test} from 'vitest';
import Component from './component.js';
2024-08-25 04:14:24 -05:00
const fakeEcs = {markChange() {}};
test('creates instances', async () => {
2024-06-26 10:33:31 -05:00
class CreatingComponent extends Component {
2024-06-26 21:08:09 -05:00
static properties = {
foo: {defaultValue: 'bar', type: 'string'},
};
2024-06-26 10:33:31 -05:00
}
2024-08-25 04:14:24 -05:00
const ComponentInstance = new CreatingComponent(fakeEcs);
await ComponentInstance.create(1);
2024-06-26 10:33:31 -05:00
expect(ComponentInstance.get(1).entity)
.to.equal(1);
});
2024-08-25 04:14:24 -05:00
test('does not serialize default values', async () => {
2024-06-26 10:33:31 -05:00
class CreatingComponent extends Component {
2024-06-26 21:08:09 -05:00
static properties = {
foo: {defaultValue: 'bar', type: 'string'}, bar: {type: 'uint8'},
};
2024-06-26 10:33:31 -05:00
}
const ComponentInstance = new CreatingComponent(fakeEcs);
2024-08-25 04:14:24 -05:00
await ComponentInstance.create(1)
2024-06-26 10:33:31 -05:00
expect(ComponentInstance.get(1).toJSON())
.to.deep.equal({});
ComponentInstance.get(1).bar = 1;
expect(ComponentInstance.get(1).toJSON())
.to.deep.equal({bar: 1});
});
2024-08-25 04:14:24 -05:00
test('reuses instances', async () => {
2024-06-26 10:33:31 -05:00
class ReusingComponent extends Component {
2024-06-26 21:08:09 -05:00
static properties = {
foo: {type: 'string'},
};
2024-06-26 10:33:31 -05:00
}
2024-08-25 04:14:24 -05:00
const ComponentInstance = new ReusingComponent(fakeEcs);
await ComponentInstance.create(1);
2024-06-26 10:33:31 -05:00
const instance = ComponentInstance.get(1);
ComponentInstance.destroy(1);
expect(ComponentInstance.get(1))
.to.be.undefined;
expect(() => {
ComponentInstance.destroy(1);
})
.to.throw();
2024-08-25 04:14:24 -05:00
await ComponentInstance.create(1);
2024-06-26 10:33:31 -05:00
expect(ComponentInstance.get(1))
.to.equal(instance);
});