Skip to content

Commit

Permalink
Add mocha tests from @caresx
Browse files Browse the repository at this point in the history
A few are outdated and should be fixed (I fixed some too!)
  • Loading branch information
coyotte508 committed Feb 2, 2017
1 parent 601ce94 commit 6ebf860
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ bower_components

#Copied from bower components by grunt concat
app/assets/stylesheets/colorpicker.less

#mocha
.tmp
2 changes: 2 additions & 0 deletions app/assets/javascript/afterload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {afterLoad as playerList} from "./playerlistui";
import {afterLoad as chat} from "./chat";
import {afterLoad as pms} from "./pms/pmlistui";
import {afterLoad as battles} from "./battles/battlelistui";
import {afterLoad as utils} from "./utils";

export default function() {
utils();
chat();
playerList();
channelList();
Expand Down
44 changes: 0 additions & 44 deletions app/assets/javascript/pokeinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,50 +250,6 @@ PokeInfo.sprite = function(poke, params) {
return path;
};

PokeInfo.battlesprite = function(poke, params) {
params = params || {};

var back = params.back || false;
var data = PokeInfo.spriteData(poke, params);

var path = pokedex.generations.options[GenInfo.lastGen.num].sprite_folder;
if ((data.ext || "gif") === "gif") {
path += "animated/";
}
if (back) {
path += "back/";
}
if (poke.shiny) {
path += "shiny/";
}
if (poke.female) {
path += "female/";
}
if ((data.ext || "gif") === "gif") {
path += ("00" + poke.num).slice(-3);
} else {
path += poke.num;
}
if (poke.forme && !data.noforme) {
path += "-" + poke.forme;
}
path += "." + (data.ext || "gif");

return path;
};

PokeInfo.spriteData = function(poke, params) {
var back = (params || {}).back || false;
var num = this.toNum(poke);

var ret = (back ? pokedex.pokes.images.back[num] : pokedex.pokes.images[num]);
if (!ret) {
ret = (back ? pokedex.pokes.images.back[num%65356] : pokedex.pokes.images[num%65356]) || {"w":96,"h":96};
ret.noforme = true;
}
return ret;
};

PokeInfo.icon = function(poke) {
poke = this.toObject(poke);
return "/images/icons/" + poke.num + (poke.forme ? "-" + poke.forme : "") + ".png";
Expand Down
25 changes: 11 additions & 14 deletions app/assets/javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export function queryField(key, defaultStr, query) {
return (match && decodeURIComponent(match[1].replace(/\+/g, " "))) || defaultStr;
}

// HTML utilities
export function escapeHtmlQuotes(str) {
return str.replace(/"/g, """).replace(/'/g, "'");
}

export function escapeHtml(str) {
return (str || "").replace(/&/g, "&")
.replace(/</g, "&lt;")
Expand Down Expand Up @@ -164,20 +159,22 @@ unenumerable(String.prototype, "startsWith", function(str) {
return this.lastIndexOf(str, 0) === 0;
});

window.isActive = true;

$(window).focus(function() {
if (typeof window !== "undefined") {
window.isActive = true;
});

$(window).blur(function() {
window.isActive = false;
});
$(window).focus(function() {
window.isActive = true;
});

$(window).blur(function() {
window.isActive = false;
});
}

/* Add a preventDefault on stopPropagation in order to avoid
on FF a click on a player element that won't be handled by our handler
because of the e.stopPropagation() that would've happened. */
$(function() {
export function afterLoad (){
if (!$.fn.contextmenu.Constructor) {
return;
}
Expand All @@ -199,4 +196,4 @@ $(window).blur(function() {
e.stopPropagation = x;
}
};
});
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"grunt-contrib-uglify": "^2.0.0",
"grunt-contrib-watch": "^1.0.0",
"load-grunt-tasks": "^3.5.2",
"webpack": "^2.2.1"
"mocha": "^3.2.0",
"mocha-webpack": "^0.7.0",
"webpack": "^2.2.1",
"webpack-node-externals": "^1.5.4"
},
"dependencies": {
"body-parser": "^1.14.0",
Expand All @@ -35,18 +38,21 @@
"css-loader": "^0.26.1",
"ejs": "^2.5.5",
"express": "^4.13.3",
"glob": "^7.1.1",
"handlebars": "^4.0.6",
"howler": "^2.0.2",
"jquery": "^3.1.1",
"md5": "^2.2.1",
"riot-observable": "^3.0.0",
"should": "^11.2.0",
"style-loader": "^0.13.1",
"typeahead": "^0.2.2",
"vex-js": "^3.0.0"
},
"scripts": {
"lint": "eslint app",
"build": "webpack --config webpack.config.js",
"test": "npm run lint"
"test": "npm run lint && npm run mocha",
"mocha": "mocha-webpack --webpack-config webpack.config-test.js \"test/*.js\""
}
}
48 changes: 48 additions & 0 deletions test/abilityinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var should = require("should");

var {AbilityInfo} = require('../app/assets/javascript/pokeinfo');
import pokedex from "../app/assets/javascript/pokedex";

describe('abilityinfo', function () {
describe('.list', function () {
it('should return the list of abilities', function () {
AbilityInfo.list().should.equal(pokedex.abilities.abilities);
});
});
describe('.name', function () {
it("should return the ability's name", function () {
AbilityInfo.name(62).should.equal('Guts');
AbilityInfo.name(105).should.equal('Super Luck');
AbilityInfo.name(141).should.equal('Moody');
AbilityInfo.name(171).should.equal('Fur Coat');
AbilityInfo.name(187).should.equal('Magician');
});
});
describe('.desc', function () {
it("should return the ability's description", function () {
AbilityInfo.desc(62).should.equal("Boosts Attack if there is a status problem.");
AbilityInfo.desc(105).should.equal("Heightens the critical-hit ratios of moves.");
AbilityInfo.desc(141).should.equal("Raises a random stat two stages and lowers another one stage after each turn.");
// Unimplemented
//AbilityInfo.desc(171).should.equal('');
//AbilityInfo.desc(187).should.equal('');
});
});
describe('.message', function () {
it("should return an empty string if the ability doesn't have a message", function () {
AbilityInfo.message(-1).should.equal('');
AbilityInfo.message(Object.keys(pokedex.abilities.abilities).length).should.equal('');
});
it("should return the ability's message", function () {
// Wonder Guard
AbilityInfo.message(71).should.equal("%s's Wonder Guard evades the attack!");
// Flash Fire
AbilityInfo.message(19).should.equal("%s's Flash Fire raised the power of its Fire-type moves!");
AbilityInfo.message(19, 1).should.equal("%s's Flash Fire made %m ineffective!");
});
it("it should return an empty string if the requested part doesn't exist", function () {
// Defiant - Defiant sharply raised %s's Attack!
AbilityInfo.message(80, 1).should.equal('');
});
});
});
13 changes: 13 additions & 0 deletions test/genderinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var should = require("should");

import {GenderInfo} from "../app/assets/javascript/pokeinfo";

describe('genderinfo', function () {
describe('.name', function () {
it("should return the gender's name", function () {
GenderInfo.name(1).should.equal('male');
GenderInfo.name(2).should.equal('female');
GenderInfo.name(3).should.equal('neutral');
});
});
});
62 changes: 62 additions & 0 deletions test/geninfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var should = require("should");

import {GenInfo} from "../app/assets/javascript/pokeinfo";
import pokedex from "../app/assets/javascript/pokedex";

describe('geninfo', function () {
var numGens = GenInfo.lastGen.num;

describe('.getGen', function () {
it('should convert non-objects', function () {
GenInfo.getGen(1).should.eql({num: 1, subnum: 0});
GenInfo.getGen(6).should.eql({num: 6, subnum: 0});
});
it('should range test generations', function () {
GenInfo.getGen(-1).should.eql({num: 7, subnum: 0});
GenInfo.getGen(numGens + 1).should.eql({num: 7, subnum: 0});
});
it('should not range test generations if correct is false', function () {
GenInfo.getGen(-1, false).should.eql({num: -1});
GenInfo.getGen(numGens + 1, false).should.eql({num: numGens + 1});
GenInfo.getGen(undefined, false).should.eql({num: undefined});
});
});
describe('.list', function () {
it('should return the list of versions', function () {
Object.assign(...GenInfo.list().map(d => ({[d]: GenInfo.version(d)}))).should.eql(pokedex.gens.versions);
GenInfo.list().length.should.equal(Object.keys(pokedex.gens.versions).length);
});
});
describe('.name', function () {
it('should return the generation name if it exists', function () {
GenInfo.name(3).should.equal("Generation 3");
GenInfo.name({num: 4}).should.equal("Generation 4");
GenInfo.name(6).should.equal("Generation 6");
should(GenInfo.name(-1)).equal(undefined);
should(GenInfo.name({num: "Generation 1"})).equal(undefined);
});
});
describe('.options', function () {
it('should return the list of generation options', function () {
GenInfo.options().should.equal(pokedex.generations.options);
Object.keys(GenInfo.options()).length.should.equal(numGens);
});
});
describe('.option', function () {
it('should return options specific to a generation if given', function () {
GenInfo.option(1).sprite_folder.should.equal('http://pokemon-online.eu/images/pokemon/red-blue/');
GenInfo.option(2).ability.should.equal(false);
GenInfo.option({num: 4}).gender.should.equal(true);
should(GenInfo.option(-1)).equal(undefined);
GenInfo.option(6).animated.should.equal(true);
GenInfo.option(7).animated.should.equal(false);
});
});
describe('.hasOption', function () {
it('should return true or false depending on if given gen has that option enabled', function () {
GenInfo.hasOption(1, 'sprite_folder').should.equal(true);
GenInfo.hasOption(2, 'ability').should.equal(false);
GenInfo.hasOption({num: 4}, 'gender').should.equal(true);
});
});
});
3 changes: 3 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--require should
--reporter spec
--ui bdd
43 changes: 43 additions & 0 deletions test/moveinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var should = require("should");

import {MoveInfo} from '../app/assets/javascript/pokeinfo';
import '../app/assets/javascript/pokedex-full';

describe('moveinfo', function () {
describe('.accuracy', function () {
it('should resolve data from older/newer gens when not found', function () {
MoveInfo.accuracy(3, 1).should.equal(85);
});
});
describe('.category', function () {
it('should resolve data from older/newer gens when not found', function () {
MoveInfo.category(44, 3).should.equal(1);
});
});
describe('.effect', function () {
it('should resolve data from older/newer gens when not found', function () {
MoveInfo.effect(34, 4).should.equal("Has a $effect_chance% chance to paralyze the target.");
});
});
describe('.pp', function () {
it('should resolve data from older/newer gens when not found', function () {
MoveInfo.pp(26, 2).should.equal(25);
});
});
describe('.type', function () {
it('should resolve data from older/newer gens when not found', function () {
// Flower Shield
MoveInfo.type(592).should.equal(17);
});
});
describe('.power', function () {
it('should resolve data from older/newer gens when not found', function () {
MoveInfo.power(143, 2).should.equal(140);
});
it("should use the given gen's data", function () {
// Crabhammer: 90 in 5th, 100 in 6th
MoveInfo.power(152, 5).should.equal(90);
MoveInfo.power(152, 6).should.equal(100);
});
});
});
72 changes: 72 additions & 0 deletions test/pokeinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var should = require("should");

import {PokeInfo} from '../app/assets/javascript/pokeinfo';
import '../app/assets/javascript/pokedex-full';

describe('pokeinfo', function () {
describe('.toNum', function () {
it('should return a number if given one', function () {
PokeInfo.toNum(40).should.equal(40);
});
it('should deal with formes', function () {
PokeInfo.toNum({num: 3, forme: 1}).should.equal((1 << 16) + 3);
});
});

describe('.toArray', function () {
it('should convert numbers to arrays', function () {
PokeInfo.toArray(493).should.eql([493, 0]);
PokeInfo.toArray(200).should.eql([200, 0]);
});
it('should convert strings to arrays', function () {
PokeInfo.toArray("493").should.eql([493, 0]);
PokeInfo.toArray("493-2").should.eql([493, 2]);
PokeInfo.toArray("262637").should.eql([493, 4]);
});
it('should fix up arrays', function () {
PokeInfo.toArray(["493", "0"]).should.eql([493, 0]);
});
});

describe('.types', function () {
it('should resolve data from older/newer gens when not found', function () {
// Pidgeot
PokeInfo.types(18, 4).should.eql([0, 2]); // Normal Flying

// Clefairy in gen 1
PokeInfo.types(35, 1).should.eql([0]); // Normal
// Clefairy in gen 6
PokeInfo.types(35).should.eql([17]); // Fairy

// Retest, to make sure internal expand didn't touch anything
// Clefairy in gen 1
PokeInfo.types(35, 1).should.eql([0]); // Normal
// Clefairy in gen 6
PokeInfo.types(35).should.eql([17]); // Fairy
});
});

describe('.stats', function () {
it("should use the given gen's data", function () {
// Butterfree
PokeInfo.stats(12, 3).should.eql([60, 45, 50, 80, 80, 70]);
PokeInfo.stats(12, 6).should.eql([60, 45, 50, 90, 80, 70]);
});
});

describe('.sprite', function () {
it('should generate sprite urls from parameters', function () {
PokeInfo.sprite({num: 1, gen: 6}).should.equal("http://pokemon-online.eu/images/pokemon/x-y/animated/001.gif");
PokeInfo.sprite({num: 1, gen: {num: 6}}).should.equal("http://pokemon-online.eu/images/pokemon/x-y/animated/001.gif");
PokeInfo.sprite({num: 1}).should.equal("http://pokemon-online.eu/images/pokemon/x-y/animated/001.gif");
PokeInfo.sprite({num: 3, forme: 1, gen: {num: 6}}).should.equal("http://pokemon-online.eu/images/pokemon/x-y/animated/003-1.gif");
PokeInfo.sprite({num: 3, forme: 1, gen: {num: 6}}, {back: true}).should.equal("http://pokemon-online.eu/images/pokemon/x-y/animated/back/003-1.gif");
});
});

describe('.trainerSprite', function () {
it('should generate sprite urls from parameters', function () {
PokeInfo.trainerSprite(200).should.equal('http://pokemon-online.eu/images/trainers/200.png');
});
});
});
Loading

0 comments on commit 6ebf860

Please sign in to comment.