Skip to content

Commit

Permalink
Refactor eco.js to remove use of 'new Entity()'
Browse files Browse the repository at this point in the history
  • Loading branch information
lajohnston committed Mar 2, 2018
1 parent bbbe09f commit 3d5fc09
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
4 changes: 2 additions & 2 deletions dist/eco.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 15 additions & 25 deletions spec/unit/eco.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Eco from "../../src/eco";

function mockEntityPrototype() {
return jasmine.createSpyObj("Entity", ["defineComponent"]);
}

function mockEntityCollection() {
return jasmine.createSpyObj("entityCollection", [
"add",
Expand All @@ -14,18 +10,20 @@ function mockEntityCollection() {

describe("Eco", () => {
it("should return entity instances", () => {
const Entity = function() {};
const entity = "foo";
const createEntity = jasmine.createSpy().and.returnValue(entity);

const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(createEntity, entityCollection);

const instance = eco.entity();
expect(instance instanceof Entity).toBeTruthy();
expect(createEntity).toHaveBeenCalledWith(eco);
expect(instance).toBe(entity);
expect(entityCollection.add).toHaveBeenCalledWith(instance);
});

it("should call the onChange function property if a component changes", done => {
const Entity = function() {};
const eco = new Eco(Entity, mockEntityCollection());
const eco = new Eco(() => ({}), mockEntityCollection());

const entity = {};
const component = "foo";
Expand All @@ -44,46 +42,41 @@ describe("Eco", () => {
});

it("should increment the entityCollection version when a component is added", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(() => ({}), entityCollection);

const entity = {};
eco.onComponentChanged(entity, "foo", "bar", undefined);
expect(entityCollection.incVersion).toHaveBeenCalledWith(entity, "foo");
});

it("should increment the entityCollection version when a component is removed", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(() => ({}), entityCollection);

const entity = {};
eco.onComponentChanged(entity, "foo", undefined, "bar");
expect(entityCollection.incVersion).toHaveBeenCalledWith(entity, "foo");
});

it("should not increment the entityCollection version if a component value has changed", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(() => ({}), entityCollection);

eco.onComponentChanged({}, "foo", "foo", "bar");
expect(entityCollection.incVersion).not.toHaveBeenCalled();
});

it("should return an array of all its entities", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(() => ({}), entityCollection);
expect(eco.all).toBe(entityCollection.entities);
});

it("should create and return iterator instances", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const createIterator = jasmine.createSpy("createIterator");
const eco = new Eco(Entity, entityCollection, createIterator);
const eco = new Eco(() => ({}), entityCollection, createIterator);

const filter = {};
createIterator.and.returnValue(filter);
Expand All @@ -98,10 +91,9 @@ describe("Eco", () => {
});

it("should create and return filter instances with custom filter functions", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const createIterator = jasmine.createSpy("createIterator");
const eco = new Eco(Entity, entityCollection, createIterator);
const eco = new Eco(() => ({}), entityCollection, createIterator);

const filter = {};
createIterator.and.returnValue(filter);
Expand All @@ -118,19 +110,17 @@ describe("Eco", () => {
});

it("should remove entities that have been disabled", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(() => ({}), entityCollection);
const entity = eco.entity();

eco.onEntityStatusChanged(entity, false);
expect(entityCollection.remove).toHaveBeenCalledWith(entity);
});

it("should re-add entities that have been re-enabled", () => {
const Entity = function() {};
const entityCollection = mockEntityCollection();
const eco = new Eco(Entity, entityCollection);
const eco = new Eco(() => ({}), entityCollection);
const entity = eco.entity();

eco.onEntityStatusChanged(entity, false);
Expand Down
8 changes: 4 additions & 4 deletions src/eco.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/
export default class Eco {
/**
* @param {function} Entity class to use to create entities
* @param {function} createEntity function that returns an entity instance
* @param {Object} entityCollection collection to hold entities
* @param {function} createIterator function that returns an interator instance
*/
constructor(Entity, entities, createIterator) {
this.Entity = Entity;
constructor(createEntity, entities, createIterator) {
this._createEntity = createEntity;
this.entities = entities;
this.createIterator = createIterator;

Expand Down Expand Up @@ -66,7 +66,7 @@ export default class Eco {
* @returns {Entity} the entity
*/
entity() {
const entity = new this.Entity(this);
const entity = this._createEntity(this);
this.entities.add(entity);
return entity;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createContainer() {
components.forEach(name => Entity.defineComponent(name));

return new container.Eco(
Entity,
eco => new Entity(eco),
container.entityCollection(),
container.createIterator
);
Expand Down

0 comments on commit 3d5fc09

Please sign in to comment.