1
0
Fork 0
spacetac/src/ui/TestGame.ts

93 lines
2.7 KiB
TypeScript
Raw Normal View History

/// <reference path="battle/BattleView.ts"/>
2017-01-30 00:40:33 +00:00
/// <reference path="map/UniverseMapView.ts"/>
2015-01-08 00:00:00 +00:00
2017-02-09 00:00:35 +00:00
module TS.SpaceTac.UI.Specs {
2017-02-21 22:38:31 +00:00
/**
* Class to hold references to test objects (used as singleton in "describe" blocks)
*
* Attributes should only be accessed from inside corresponding "it" blocks (they are initialized by the setup).
*/
export class TestGame {
ui: MainUI;
battleview: BattleView;
mapview: UniverseMapView;
}
/**
* Setup a headless test UI, with a single view started.
*/
export function setupSingleView(buildView: (testgame: TestGame) => [Phaser.State, any[]]) {
let testgame = new TestGame();
beforeEach(function (done) {
spyOn(console, "log").and.stub();
spyOn(console, "warn").and.stub();
2017-02-21 22:38:31 +00:00
testgame.ui = new MainUI(true);
2015-01-08 00:00:00 +00:00
2017-02-21 22:38:31 +00:00
if (testgame.ui.load) {
spyOn(testgame.ui.load, 'image').and.stub();
spyOn(testgame.ui.load, 'audio').and.stub();
2017-02-16 22:59:41 +00:00
}
2017-02-10 00:08:28 +00:00
2017-02-21 22:38:31 +00:00
let [state, stateargs] = buildView(testgame);
2015-01-08 00:00:00 +00:00
2017-02-21 22:38:31 +00:00
let orig_create = bound(state, "create");
spyOn(state, "create").and.callFake(() => {
orig_create();
2015-01-08 00:00:00 +00:00
done();
2017-02-21 22:38:31 +00:00
});
testgame.ui.state.add("test", state);
testgame.ui.state.start("test", true, false, ...stateargs);
2015-01-08 00:00:00 +00:00
});
2017-02-21 22:38:31 +00:00
afterEach(function () {
let ui = testgame.ui;
window.requestAnimationFrame(() => ui.destroy());
testgame.ui = null;
testgame.battleview = null;
testgame.mapview = null;
});
return testgame;
2015-01-08 00:00:00 +00:00
}
2017-02-21 22:38:31 +00:00
/**
* Test setup of an empty BaseView
*/
export function setupEmptyView(): TestGame {
return setupSingleView(testgame => [new BaseView(), []]);
}
2017-01-30 00:40:33 +00:00
2017-02-21 22:38:31 +00:00
/**
* Test setup of a battleview bound to a battle, to be called inside a "describe" block.
*/
export function setupBattleview(): TestGame {
return setupSingleView(testgame => {
testgame.battleview = new BattleView();
let battle = Battle.newQuickRandom();
let player = battle.playing_ship.getPlayer();
return [testgame.battleview, [player, battle]];
});
}
/**
* Test setup of a mapview bound to a universe, to be called inside a "describe" block.
*/
export function setupMapview(): TestGame {
return setupSingleView(testgame => {
testgame.mapview = new UniverseMapView();
let mapview = new UniverseMapView();
let session = new GameSession();
session.startNewGame();
return [testgame.mapview, [session.universe, session.player]];
});
2017-01-30 00:40:33 +00:00
}
2015-01-08 00:00:00 +00:00
}