Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automated testing. #2726

Open
wants to merge 1 commit into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions js/foeproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ const FoEproxy = (function () {
try {
callback(data);
} catch (e) {
console.error(e);
console.error(e, 'data:', data);
}
}
}
Expand Down Expand Up @@ -341,7 +341,7 @@ const FoEproxy = (function () {
try {
callback(data);
} catch (e) {
console.error(e);
console.error(e, 'data:', data);
}
}

Expand All @@ -354,7 +354,7 @@ const FoEproxy = (function () {
proxyWsAction(data.requestClass, data.requestMethod, data);
}
} catch (e) {
console.error(e);
console.error(e, 'evt:', evt);
}
}

Expand Down Expand Up @@ -389,7 +389,7 @@ const FoEproxy = (function () {
try {
callback(data, postData);
} catch (e) {
console.error(e);
console.error(e, 'data:', data, 'postData:', postData);
}
}
}
Expand Down Expand Up @@ -422,7 +422,7 @@ const FoEproxy = (function () {
try {
callback(postData);
} catch (e) {
console.error(e);
console.error(e, 'postData:', postData);
}
}
}
Expand Down Expand Up @@ -477,7 +477,7 @@ const FoEproxy = (function () {
try {
callback(this, requestData);
} catch (e) {
console.error(e);
console.error(e, 'requestData:', requestData);
}
}

Expand All @@ -498,7 +498,7 @@ const FoEproxy = (function () {
try {
callback(this, postData);
} catch (e) {
console.error(e);
console.error(e, 'postData:', postData);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function inject (loadBeta = false, extUrl = chrome.runtime.getURL(''), betaDate=
}

// is there a translation?
if (Languages.PossibleLanguages[lng] === undefined) {
if (typeof Languages === 'object' && Languages.PossibleLanguages[lng] === undefined) {
lng = 'en';
}

Expand Down
37 changes: 29 additions & 8 deletions js/web/_main/js/_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,15 +1463,36 @@ let MainParser = {
setTimeout(() => resolve({ ok: false, error: "response timeout for: " + JSON.stringify(data) }), 1000)
});

if (typeof response !== 'object' || typeof response.ok !== 'boolean') {
throw new Error('invalid response from Extension-API call');
}
try {
if (typeof response !== 'object' || typeof response.ok !== 'boolean') {
throw new Error('invalid response from Extension-API call');
}

if (response.ok === true) {
return response.data;
}
else {
throw new Error('EXT-API error: ' + response.error);
if (response.ok === true) {
return response.data;
}
else {
throw new Error('EXT-API error: ' + response.error);
}
} catch (err) {
if (typeof jsApiReporter !== 'undefined' && !jsApiReporter.finished) {
// in test environment: degrade thrown error to a logged error, so running tests are not affected
console.groupCollapsed('Error in Extension communication during test (' + typeof response + ':' + response + ') - ', err);
try {
console.log('data=\'' + JSON.stringify(data) + '\', response=\'' + JSON.stringify(response) + '\'');
} finally {
console.groupEnd();
}
return [];
} else {
console.groupCollapsed('Error in Extension communication (' + typeof response + ':' + response + ')');
try {
console.log('data=\'' + JSON.stringify(data) + '\', response=\'' + JSON.stringify(response) + '\'');
} finally {
console.groupEnd();
}
throw err;
}
}
},

Expand Down
44 changes: 44 additions & 0 deletions js/web/eventhandler/test/eventhandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
describe(tt.getModuleSuiteName(), function() {
beforeEach(function() {
// todo: the spec below touches main's players dictionary, reset eventhandler and main modules
});

it('friend with no guild is listed without error', async function() {
// reported in https://discord.com/channels/577475401144598539/950390739437760523/1189018590351982692
EventHandler.CurrentPlayerGroup = 'Friends';
EventHandler.ShowHideColumns = { "GuildName": true, "Era": false, "Points": false };
EventHandler.MaxVisitCount = 7;
const player = {
'player_id': 1234567890,
'name': 'Friend with no guild',
'is_friend': true,
'avatar': 'portrait_id_1',
};
MainParser.UpdatePlayerDictCore(player);
PlayerDictFriendsUpdated = true;
const olddb = EventHandler.db; // just to be sure, should be null without initialization via startup response / after reset
EventHandler.db = {
'Events': {
where: (s) => ({
equals: (pid) => ({
toArray: () => []
})
})
}
};
const div = $('<div><table id="moppelhelperTable"></table></div>');
await $('body').append(div).promise();

await EventHandler.CalcMoppelHelperTable();

const trs = $('#moppelhelperTable tr');
expect(trs.length).toBe(2);
const tr1 = trs[1]; // first non-header row
expect($(':nth-child(3) a', tr1)[0].text).toBe(player.name + ' ');
expect($(':nth-child(4)', tr1)[0].innerText).toBe(''); // empty guild name (displayed)
expect($(':nth-child(4)', tr1)[0].getAttribute('data-text')).toBe(''); // empty guild name (data for sorter / exporter)

await div.remove().promise();
EventHandler.db = olddb;
});
});
53 changes: 53 additions & 0 deletions js/web/guildfights/js/guildfights.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,59 @@ let GuildFights = {
TabsContent: [],


/**
* Reset the module state for testing purposes
* @returns {Promise<void>}
*/
_reset: async function() {
// ensure we are in the test environment
expect(tt).toBeDefined();

// todo: remove UI
// todo: remove proxy handlers
// todo: delete databases

localStorage.removeItem('GuildFights.NewAction');
localStorage.removeItem('GuildFights.NewActionTimestamp');
localStorage.removeItem('GuildFightsPlayerBoxSettings');
localStorage.removeItem('GildPlayersDetailViewCords');
localStorage.removeItem('LiveFightSettings');

this.Alerts = [];
this.GlobalRankingTimeout = null;
this.PrevAction = null;
this.PrevActionTimestamp = null;
this.NewAction = null;
this.NewActionTimestamp = null;
this.MapData = null;
this.Neighbours = [];
this.PlayersPortraits = null;
this.Colors = null;
this.SortedColors = null;
this.ProvinceNames = null;
this.InjectionLoaded = false;
this.PlayerBoxContent = [];
this.CurrentGBGRound = null;
this.GBGRound = null;
this.GBGAllRounds = null;
this.GBGHistoryView = false;
this.LogDatePicker = null;
this.curDateFilter = null;
this.curDateEndFilter = null;
this.curDetailViewFilter = null;
this.PlayerBoxSettings = {
showRoundSelector: 1,
showLogButton: 1,
showProgressFilter: 1,
showOnlyActivePlayers: 0,
};
this.showGuildColumn = 0;
this.showAdjacentSectors = 0;
this.showOwnSectors = 0;
this.Tabs = [];
this.TabsContent = [];
},

/**
*
* @returns {Promise<void>}
Expand Down
57 changes: 57 additions & 0 deletions js/web/guildfights/test/guildfights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
describe(tt.getModuleSuiteName(), function() {
beforeEach(async function() {
await GuildFights._reset();
});

afterAll(async function() {
await GuildFights._reset();
ExtGuildID = 0;
});

it('own locked isolated province is shown in NextUp window', function() {
// reported in https://discord.com/channels/577475401144598539/577482701930627072/1188257574429147216
localStorage.setItem('LiveFightSettings', '{"showGuildColumn":1,"showAdjacentSectors":1,"showOwnSectors":1}');
ExtGuildID = 123;
GuildFights.MapData = {map: {}};
GuildFights.MapData.map.provinces = [{
"victoryPoints": 114,
"victoryPointsBonus": 0,
"ownerId": 12345,
"lockedUntil": 1234567890,
"conquestProgress": [],
"totalBuildingSlots": 3,
"usedBuildingSlots": 0,
"gainAttritionChance": 100,
"__class__": "GuildBattlegroundProvince",
"id": 0,
"title": "A1:M_foo",
"name": "Mati Tudokk FOO",
"owner": "ouR cLan",
"neighbor": []
}];
GuildFights.MapData.battlegroundParticipants = [{
"participantId": 12345,
"clan": {
"id": 123,
"name": "ouR cLan",
"membersNum": 69,
"flag": "flag_1",
"__class__": "BaseClan"
},
"colour": "own_guild_colour",
"victoryPoints": 1,
"signals": [],
"__class__": "GuildBattlegroundParticipant"
}];
expect(GuildFights.MapData.battlegroundParticipants[0].participantId).toBe(GuildFights.MapData.map.provinces[0].ownerId);
expect(GuildFights.MapData.battlegroundParticipants[0].clan.id).toBe(ExtGuildID);
expect(GuildFights.MapData.battlegroundParticipants[0].clan.name).toBe(GuildFights.MapData.map.provinces[0].owner);
GuildFights.Colors = [{"id":"own_guild_colour","province":"#e7e7e7","shadow":"#4b4a4a","base":"#5d5d5d","mainColour":"#bebdbc","highlight":"#dfdfdf","ownGuildColour":true,"__class__":"GuildBattlegroundColour"}];
GuildFights.PrepareColors();

const result = GuildFights.BuildNextUpTab();

expect(result.length).toBeGreaterThan(7);
expect(result).toContain('<td class="prov-name" title="Owner: ouR cLan"><span class="province-color" style="background-color:#bebdbc""></span> <b>A1:M_foo</b></td>');
});
});
Loading