Skip to content

Commit 1e48467

Browse files
committed
update npm package
1 parent b8ec5b2 commit 1e48467

File tree

6 files changed

+6121
-4
lines changed

6 files changed

+6121
-4
lines changed

.github/workflows/test.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Run tests
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull:
7+
8+
jobs:
9+
test:
10+
name: Run lib tests
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: "☁️ checkout repository"
14+
uses: actions/checkout@v3
15+
16+
- name: "🔧 setup node"
17+
uses: actions/setup-node@v3
18+
with:
19+
node-version: 18
20+
cache: "npm"
21+
22+
- name: "🔍 lint code"
23+
run: npm test

index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class ApiWrapper {
1+
class DisStat {
22
constructor(apikey = "", botId = "") {
33
if (!apikey) throw new Error("No API key for DisStat provided. You can find your API key in the DisStat dashboard, it's the same for all your bots.")
44
if (!botId) throw new Error("No bot ID provided.")
@@ -32,6 +32,7 @@ class ApiWrapper {
3232
}
3333

3434
async postCommand(command = "", userId = "") {
35+
if (!command) throw new Error("No command provided.")
3536
const url = this.base_url + "/bot/" + this.botId + "/cmd"
3637
fetch(url, {
3738
method: "post",
@@ -46,10 +47,12 @@ class ApiWrapper {
4647
})
4748
}
4849
async postCmd(command = "", userId = "") {
50+
if (!command) throw new Error("No command provided.")
4951
return await this.postCommand(command, userId)
5052
}
5153

5254
async postEvent(event = "", userId = "") {
55+
if (!event) throw new Error("No event provided.")
5356
const url = this.base_url + "/bot/" + this.botId + "/event"
5457
fetch(url, {
5558
method: "post",
@@ -64,3 +67,5 @@ class ApiWrapper {
6467
})
6568
}
6669
}
70+
71+
module.exports = DisStat

index.test.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const DisStat = require("./index.js")
2+
3+
test("missing apikey", () => {
4+
expect(() => new DisStat()).toThrow()
5+
})
6+
test("missing botId", () => {
7+
expect(() => new DisStat("jest")).toThrow()
8+
})
9+
10+
test("getBot", async () => {
11+
const disstat = new DisStat("jest", "jest")
12+
const bot = await disstat.getBot("685166801394335819")
13+
expect(Object.keys(bot)).toContain("id")
14+
expect(Object.keys(bot)).toContain("name")
15+
})
16+
17+
test("postData", async () => {
18+
const disstat = new DisStat("jest", "jest")
19+
const data = await disstat.postData({}, true)
20+
expect(Object.keys(data)).toContain("message")
21+
})
22+
23+
test("postCommand", async () => {
24+
const disstat = new DisStat("jest", "jest")
25+
const data = await disstat.postCommand("jest")
26+
expect(data).toBeUndefined()
27+
})
28+
29+
// This test fails as it should, but it crashes the whole test process with it.
30+
/*test("postCmd without command", () => {
31+
const disstat = new DisStat("jest", "jest")
32+
expect(async () => {
33+
await disstat.postCmd()
34+
}).toThrow("no command provided")
35+
})*/
36+
37+
test("postEvent", async () => {
38+
const disstat = new DisStat("jest", "jest")
39+
const data = await disstat.postEvent("jest", "jest")
40+
expect(data).toBeUndefined()
41+
})

0 commit comments

Comments
 (0)