Skip to content

Commit 5605b49

Browse files
committed
fix(lol) and feat(jest)
1 parent 51a0e25 commit 5605b49

File tree

7 files changed

+119
-5
lines changed

7 files changed

+119
-5
lines changed

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Jest Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
12+
- name: Use Node.js
13+
uses: actions/setup-node@v2
14+
with:
15+
node-version: '14.x'
16+
17+
- name: Install Dependencies
18+
run: npm install
19+
20+
- name: Run Jest Tests
21+
run: npm test

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ environnement.sh
22
logs.json
33
node_modules
44
package-lock.json
5-
extensions.json
5+
extensions.json
6+
.env
7+
environnement

commands/adminlol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ module.exports = {
607607
} else if (interaction.options.getSubcommand() === "maintenance") {
608608
client.lol.trackers.forEach(ch => {
609609
client.channels.fetch(ch).then(chs => {
610-
chs.send("an unexpected error has occurred, game fetching and tracker messages will be disabled until more investigations")
610+
chs.send("an unexpected error has occurred, game fetching and tracker messages will be disabled until more investigations");
611611
});
612612
});
613613
}

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testMatch: ['<rootDir>/tests/*.test.js'],
3+
};

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
"license": "GPL v3+",
88
"dependencies": {
99
"canvas": "^2.10.1",
10+
"chartjs-node-canvas": "^4.1.6",
1011
"common-tags": "^1.8.2",
11-
"discord.js": "^13.10.3",
12+
"cookie-parser": "^1.4.6",
13+
"discord.js": "^13.15.1",
14+
"ejs": "^3.1.9",
15+
"express": "^4.18.2",
16+
"express-sse-middleware": "^3.0.3",
17+
"markov-strings": "^3.0.1",
1218
"node-fetch": "^2.6.7",
1319
"node-ical": "^0.14.1",
1420
"pdfkit": "^0.13.0",
15-
"pg": "^8.8.0"
21+
"pg": "^8.8.0",
22+
"undici": "^5.21.0"
23+
},
24+
"scripts": {
25+
"test": "jest"
1626
},
1727
"devDependencies": {
18-
"eslint": "^8.26.0"
28+
"eslint": "^8.26.0",
29+
"jest": "^29.5.0"
1930
}
2031
}

tests/lol_api.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const lolapi = require('../util/lol_api');
2+
const { describe, it, expect, beforeEach } = require('@jest/globals');
3+
4+
describe('getChampsId', () => {
5+
let client;
6+
7+
beforeEach(() => {
8+
client = {
9+
'lol': {
10+
"api_limit": false
11+
}
12+
}; // mock client object
13+
});
14+
15+
it('should return an object with champion names as keys and IDs as values', async () => {
16+
const champs = await lolapi.getChampsId('NA1', client);
17+
expect(champs).toHaveProperty('Aatrox', 'Aatrox');
18+
expect(champs).toHaveProperty('Ahri', 'Ahri');
19+
expect(champs).toHaveProperty('Zyra', 'Zyra');
20+
expect(champs).toHaveProperty('Swain', 'Swain');
21+
expect(champs).toHaveProperty('Renata Glasc', 'Renata');
22+
});
23+
24+
it('should throw an error when given an invalid region', async () => {
25+
await expect(lolapi.getChampsId('invalid', client)).rejects.toThrow();
26+
});
27+
28+
it('should throw an error when the API call fails', async () => {
29+
const error = new Error('API call failed');
30+
lolapi.apiCall = jest.fn().mockRejectedValue(error);
31+
await expect(lolapi.getChampsId('NA1', client)).rejects.toThrow(error);
32+
});
33+
34+
it('should throw an error when the API call fails', async () => {
35+
const error = new Error('API call failed');
36+
lolapi.apiCall = jest.fn().mockRejectedValue(error);
37+
await expect(lolapi.getChampsId('NA1', client)).rejects.toThrow(error);
38+
});
39+
});
40+
41+
describe('summonersByName', () => {
42+
let client;
43+
44+
beforeEach(() => {
45+
client = {
46+
'lol': {
47+
"api_limit": false
48+
}
49+
}; // mock client object
50+
});
51+
52+
it('should return a summoner object when given valid input', async () => {
53+
const summoner = await lolapi.summonersByName(process.env.RIOT_API_KEY, 'EUW1', 'KwikKill', client);
54+
console.log(summoner);
55+
expect(summoner).toHaveProperty('id');
56+
expect(summoner).toHaveProperty('accountId');
57+
expect(summoner).toHaveProperty('puuid');
58+
expect(summoner).toHaveProperty('name', 'SummonerName');
59+
expect(summoner).toHaveProperty('profileIconId');
60+
expect(summoner).toHaveProperty('revisionDate');
61+
expect(summoner).toHaveProperty('summonerLevel');
62+
});
63+
64+
it('should throw an error when given an invalid region', async () => {
65+
await expect(lolapi.summonersByName(process.env.RIOT_API_KEY, 'invalid', 'SummonerName', client)).rejects.toThrow();
66+
});
67+
68+
it('should throw an error when the API call fails', async () => {
69+
const error = new Error('API call failed');
70+
lolapi.apiCall = jest.fn().mockRejectedValue(error);
71+
await expect(lolapi.summonersByName(process.env.RIOT_API_KEY, 'EUW1', 'KwikKill', client)).rejects.toThrow(error);
72+
});
73+
});

util/lol_api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ module.exports = {
298298
return null;
299299
}
300300
} catch (error) {
301+
// catch Invalid JSON
302+
if (error.type === 'invalid-json') {
303+
throw error;
304+
}
301305
logger.error(error.name + " : " + url);
302306
console.log(error);
303307
await delay(1000);

0 commit comments

Comments
 (0)