1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-11-27 03:46:54 +00:00
not-gwent-online/test/src/PubSubSpec.js

131 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-06-19 19:53:48 +00:00
var Battle = require("../../server/Battle");
var Card = require("../../server/Card");
var data = require("../../assets/data/abilities");
2015-06-18 07:21:27 +00:00
2015-06-19 19:53:48 +00:00
describe("pubsub", function(){
var battle, card1, card2;
beforeEach(function(){
battle = {};
battle.runEvent = Battle.prototype.runEvent;
battle.on = Battle.prototype.on;
battle.off = Battle.prototype.off;
battle.events = {};
battle.update = function() {};
card1 = Card("kaedweni_siege_expert");
card2 = Card("dun_banner_medic");
2015-06-18 07:21:27 +00:00
});
2015-06-19 19:53:48 +00:00
it("on: has correct arguments", function(){
2015-06-18 07:21:27 +00:00
//this.on("EachTurn", ability.onEachTurn, this, [card])
2015-06-19 19:53:48 +00:00
battle.on("EachTurn", function(card){
expect(card).toEqual(card1);
}, this, [card1]);
2015-06-18 07:21:27 +00:00
battle.runEvent("EachTurn");
2015-06-19 19:53:48 +00:00
2015-06-18 07:21:27 +00:00
})
2015-06-19 19:53:48 +00:00
it("runEvent: has correct arguments", function(){
2015-06-18 07:21:27 +00:00
//this.on("EachTurn", ability.onEachTurn, this, [card])
2015-06-19 19:53:48 +00:00
battle.on("EachTurn", function(c){
expect(c).toEqual(card1);
2015-06-18 07:21:27 +00:00
});
2015-06-19 19:53:48 +00:00
battle.runEvent("EachTurn", null, [card1]);
2015-06-18 07:21:27 +00:00
})
2015-06-19 19:53:48 +00:00
it("on + runEvent: has correct arguments", function(){
2015-06-18 07:21:27 +00:00
//this.on("EachTurn", ability.onEachTurn, this, [card])
2015-06-19 19:53:48 +00:00
battle.on("EachTurn", function(c1, c2){
expect(c1).toEqual(card1);
expect(c2).toEqual(card2);
}, null, [card1]);
battle.runEvent("EachTurn", null, [card2]);
2015-06-18 07:21:27 +00:00
})
2015-06-19 19:53:48 +00:00
it("test context", function(){
2015-06-18 07:21:27 +00:00
2015-06-19 19:53:48 +00:00
battle.on("EachTurn", function(card){
2015-06-18 07:21:27 +00:00
expect(card.id).toEqual(card1.id);
expect(this.id).toEqual(card2.id);
}, card2, [card1]);
battle.runEvent("EachTurn");
})
2015-06-19 19:53:48 +00:00
it("test context", function(){
2015-06-18 07:21:27 +00:00
2015-06-19 19:53:48 +00:00
battle.on("EachTurn", function(card){
2015-06-18 07:21:27 +00:00
expect(card.id).toEqual(card1.id);
expect(this.id).toEqual(card2.id);
}, null, [card1]);
battle.runEvent("EachTurn", card2);
})
2015-06-19 19:53:48 +00:00
it("test context", function(){
2015-06-18 07:21:27 +00:00
2015-06-19 19:53:48 +00:00
battle.on("EachTurn", function(card){
2015-06-18 07:21:27 +00:00
expect(card.id).toEqual(card1.id);
expect(this.id).toEqual(card1.id);
}, card1, [card1]);
battle.runEvent("EachTurn", card2);
})
2015-06-19 19:53:48 +00:00
it("should handle off correctly", function() {
var cb1 = function(){}, cb2 = function() {};
var obj = {
cb1: cb1,
cb2: cb2
}
spyOn(obj, "cb1");
spyOn(obj, "cb2");
var uid1 = battle.on("EachCardPlace", obj.cb1, battle, [card1]);
var uid2 = battle.on("EachCardPlace", obj.cb2, battle, [card2]);
battle.off("EachCardPlace", uid2);
battle.runEvent("EachCardPlace");
expect(obj.cb1).toHaveBeenCalled();
expect(obj.cb2).not.toHaveBeenCalled();
/*battle.off("EachCardPlace", uid1);
expect(battle.events).toEqual({});*/
})
2015-06-21 14:50:50 +00:00
it("should give binded ctx", function() {
var obj = {}, otherCtx = { key: "test"};
var card = Card("biting_frost");
var ability = card.getAbility();
obj.setWeather = function(weatherType) {
expect(weatherType).toEqual(0);
expect(this).toBe(otherCtx);
}
spyOn(obj, "setWeather").and.callThrough();
expect(ability.weather).toBeDefined();
ability.onEachTurn = obj.setWeather.bind(otherCtx, ability.weather);
ability.onEachCardPlace = obj.setWeather.bind(otherCtx, ability.weather);
if(ability.onEachTurn){
var uid = battle.on("EachTurn", ability.onEachTurn, battle, [card])
card._uidEvents["EachTurn"] = uid;
}
if(ability.onEachCardPlace){
var uid = battle.on("EachCardPlace", ability.onEachCardPlace, battle, [card]);
card._uidEvents["EachCardPlace"] = uid;
}
battle.runEvent("EachCardPlace");
battle.runEvent("EachTurn");
expect(obj.setWeather).toHaveBeenCalled();
})
2015-06-18 07:21:27 +00:00
});