diff --git a/app/ecs/ecs.test.js b/app/ecs/ecs.test.js index d6e6ce0..864b27a 100644 --- a/app/ecs/ecs.test.js +++ b/app/ecs/ecs.test.js @@ -3,25 +3,21 @@ import {expect, test} from 'vitest'; import Component from './component.js'; import Ecs from './ecs.js'; import System from './system.js'; +import {wrapComponents} from './test-helper.js'; -function wrapProperties(name, properties) { - return class WrappedComponent extends Component { - static componentName = name; - static properties = properties; - }; -} +const Components = wrapComponents([ + ['Empty', {}], + ['Momentum', {x: {type: 'int32'}, y: {type: 'int32'}, z: {type: 'int32'}}], + ['Name', {name: {type: 'string'}}], + ['Position', {x: {type: 'int32', defaultValue: 32}, y: {type: 'int32'}, z: {type: 'int32'}}], +]); -const Empty = wrapProperties('Empty', {}); - -const Name = wrapProperties('Name', { - name: {type: 'string'}, -}); - -const Position = wrapProperties('Position', { - x: {type: 'int32', defaultValue: 32}, - y: {type: 'int32'}, - z: {type: 'int32'}, -}); +const { + Empty, + Momentum, + Position, + Name, +} = Components; function asyncTimesTwo(x) { return new Promise((resolve) => { @@ -131,11 +127,6 @@ test('inserts components into entities', async () => { }); test('ticks systems', async () => { - const Momentum = wrapProperties('Momentum', { - x: {type: 'int32'}, - y: {type: 'int32'}, - z: {type: 'int32'}, - }); const ecs = new Ecs({ Components: {Momentum, Position}, Systems: { diff --git a/app/ecs/query.test.js b/app/ecs/query.test.js index a18e590..d5f6a75 100644 --- a/app/ecs/query.test.js +++ b/app/ecs/query.test.js @@ -1,23 +1,14 @@ import {expect, test} from 'vitest'; -import Ecs from './ecs'; -import Component from './component.js'; +import Ecs from './ecs.js'; +import {wrapComponents} from './test-helper.js'; import Query from './query.js'; -const Components = [ +const Components = wrapComponents([ ['A', {a: {type: 'int32', defaultValue: 64}}], ['B', {b: {type: 'int32', defaultValue: 32}}], ['C', {c: {type: 'int32'}}], -] - .reduce((Components, [componentName, properties]) => { - return { - ...Components, - [componentName]: class extends Component { - static componentName = componentName; - static properties = properties; - }, - }; - }, {}) +]); const ecsTest = test.extend({ ecs: async ({}, use) => { diff --git a/app/ecs/test-helper.js b/app/ecs/test-helper.js new file mode 100644 index 0000000..8a41f91 --- /dev/null +++ b/app/ecs/test-helper.js @@ -0,0 +1,14 @@ +import Component from './component.js'; + +export function wrapComponents(Components) { + return Components + .reduce((Components, [componentName, properties]) => { + return { + ...Components, + [componentName]: class extends Component { + static componentName = componentName; + static properties = properties; + }, + }; + }, {}) +} \ No newline at end of file