import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; import 'mocha'; chai.use(chaiAsPromised); const expect = chai.expect; import {State, IStateConfig, DoCreate} from './state'; import {System} from './system'; import {Asset} from './asset'; const sleep = (time: number) => new Promise(resolve => setTimeout(resolve, time)); class FooSystem extends System { } class BarSystem extends System { } const config: IStateConfig = { systems: { foo: FooSystem, bar: BarSystem }, components: {}, ticker: setImmediate }; describe('State Systems', () => { let state: State; it('should be able to be created', () => { state = new State(config); }); it('should be able to create a game mode', () => expect(state.loadGame({ foo: {}, bar: {} })).to.be.fulfilled ); it('should have systems instantiated', () => { expect(state.systems).to.have.property('foo'); expect(state.systems).to.have.property('bar'); }); it('should be able to shut down', () => expect(state.reset()).to.be.fulfilled ); it('should have no systems', () => { expect(state.systems).to.not.have.property('foo'); expect(state.systems).to.not.have.property('bar'); }); it('should be started again with different data', () => expect(state.loadGame({ bar: {} })).to.be.fulfilled ); it('should only have one system instantiated', () => { expect(state.systems).not.to.have.property('foo'); expect(state.systems).to.have.property('bar'); }); }); describe('State Actions', () => { let state: State; it('should be initialized', () => { state = new State(config); return expect(state.loadGame({ foo: {}, bar: {} })).to.be.fulfilled; }); it('should be able to create an entity', () => { state.dispatch(DoCreate('foo1', { foo: { bar: 1 } })); return expect(new Promise(async resolve => { while(state.raw('foo1') === undefined) { await sleep(50); } resolve(state.raw('foo1')); })).to.eventually.deep.equal({ id: 'foo1', foo: { bar: 1 } }); }); it('should be able to create another entity', () => { state.dispatch(DoCreate('foo2', { foo: { bar: 1 }, fiz: { bulb: [0, 0, 0] } })); return expect(new Promise(async resolve => { while(state.raw('foo2') === undefined) { await sleep(50); } resolve(state.raw('foo2')); })).to.eventually.deep.equal({ id: 'foo2', foo: { bar: 1 }, fiz: { bulb: [0, 0, 0] } }); }); });