From 15b4a168df1d8443ebfbbe5f21e411a02ea6225c Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sat, 13 Feb 2021 16:15:06 +0000 Subject: [PATCH 1/3] Add Tests for util.loadCommand if user doesn't have roles or userid --- tests/commands/testingRoles.js | 14 ++++++++++++++ tests/commands/testingUsers.js | 14 ++++++++++++++ tests/json/message.json | 9 ++++++++- tests/src/client/util.test.js | 14 ++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/commands/testingRoles.js create mode 100644 tests/commands/testingUsers.js diff --git a/tests/commands/testingRoles.js b/tests/commands/testingRoles.js new file mode 100644 index 0000000..5b9da82 --- /dev/null +++ b/tests/commands/testingRoles.js @@ -0,0 +1,14 @@ +const { command } = require('../../src'); + +class test extends command { + constructor() { + super("test"); + super.roles = "Regular"; + } + + test(context) { + context.message.reply(`Testing done by ${context.config.tester}`); + } +} + +module.exports = test; \ No newline at end of file diff --git a/tests/commands/testingUsers.js b/tests/commands/testingUsers.js new file mode 100644 index 0000000..d146756 --- /dev/null +++ b/tests/commands/testingUsers.js @@ -0,0 +1,14 @@ +const { command } = require('../../src'); + +class test extends command { + constructor() { + super("test"); + super.users = "000000000000000001"; + } + + test(context) { + context.message.reply(`Testing done by ${context.config.tester}`); + } +} + +module.exports = test; \ No newline at end of file diff --git a/tests/json/message.json b/tests/json/message.json index d353f84..7b26052 100644 --- a/tests/json/message.json +++ b/tests/json/message.json @@ -3,5 +3,12 @@ "author": { "bot": false }, - "content": "d!testing param1" + "content": "d!testing param1", + "member": { + "roles": { + "cache": [ + "NonRegular" + ] + } + } } \ No newline at end of file diff --git a/tests/src/client/util.test.js b/tests/src/client/util.test.js index 51a372d..5d3b627 100644 --- a/tests/src/client/util.test.js +++ b/tests/src/client/util.test.js @@ -83,4 +83,18 @@ describe('util.loadCommand', () => { expect(res.valid).toBe(false); expect(res.message).toBe('Command folder does not exist'); }); + + test('Should throw error if user does not have required role', () => { + let res = instance.loadCommand('testingRoles', 'param1', message); + + expect(res.valid).toBe(false); + expect(res.message).toBe('You require the `Regular` role to run this command'); + }); + + test('Should throw error if user is not in users array', () => { + let res = instance.loadCommand('testingUsers', 'param1', message); + + expect(res.valid).toBe(false); + expect(res.message).toBe('You do not have permission to run this command'); + }) }); From 893c0e1675d65796ca7890493d96cf0b50647be6 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sat, 13 Feb 2021 16:56:18 +0000 Subject: [PATCH 2/3] Add Client Tests --- tests/src/client/client.test.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/src/client/client.test.js b/tests/src/client/client.test.js index 735c1e8..33b18bf 100644 --- a/tests/src/client/client.test.js +++ b/tests/src/client/client.test.js @@ -1,5 +1,6 @@ const { client } = require('../../../src'); const { readFileSync } = require('fs'); +const { expect } = require('@jest/globals'); // Mocks jest.mock('discord.js'); @@ -18,10 +19,27 @@ describe('Client Tests', () => { instance = new client(config, commandConfig); expect(instance.config).toBe(config); + expect(instance.commandConfig).toBe(commandConfig); expect(instance.events).toBeDefined(); expect(instance.util).toBeDefined(); }); + test('Configure Client (Undefined: config)', () => { + expect(() => { + instance = new client(config, commandConfig); + instance._config = undefined; + instance.start(); + }).toThrow("Config has not been set"); + }); + + test('Configure Client (Undefined: Command Config)', () => { + expect(() => { + instance = new client(config, commandConfig); + instance._commandConfig = undefined; + instance.start(); + }).toThrow("Command Config has not been set"); + }); + test('Configure Client (Incorrect parameters: token)', () => { expect(() => { delete config.token; @@ -51,7 +69,9 @@ describe('Client Tests', () => { }); test('Start Client', () => { - instance = new client(config, commandConfig); - instance.start(); + expect(() => { + instance = new client(config, commandConfig); + instance.start(); + }).not.toThrow(); }); }); \ No newline at end of file From 21711c66ade354480ed7eef8b53aab4c3b12b344 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sat, 13 Feb 2021 18:07:18 +0000 Subject: [PATCH 3/3] Event Tests --- src/client/events.js | 2 ++ tests/src/client/events.test.js | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/client/events.js b/src/client/events.js index 93ef712..029e1a8 100644 --- a/src/client/events.js +++ b/src/client/events.js @@ -31,6 +31,8 @@ class event { "message": message }; } + + return false; } // Emit when bot is logged in and ready to use diff --git a/tests/src/client/events.test.js b/tests/src/client/events.test.js index 2d67ed3..dd8cf61 100644 --- a/tests/src/client/events.test.js +++ b/tests/src/client/events.test.js @@ -1,5 +1,6 @@ const events = require('../../../src/client/events'); const { readFileSync } = require('fs'); +const { expect } = require('@jest/globals'); // Mocks jest.mock('discord.js'); @@ -86,4 +87,43 @@ describe('events.message', () => { instance.message(message) }).not.toThrow(); }); + + test('Should return if message doesnt have prefix', () => { + message.content = "Just a normal message"; + const res = instance.message(message); + + expect(res).toBe(false); + }); +}); + +describe('events.ready', () => { + let instance; + let message; + let config; + + beforeEach(() => { + instance = new events(); + message = JSON.parse(readFileSync('tests/json/message.json')); + config = JSON.parse(readFileSync('tests/json/config.json')); + + instance.config = config; + + instance.util = jest.fn(); + instance.util.loadCommand = jest.fn(() => { + return { + "valid": true, + "message": "No message was set" + } + }); + + console = jest.fn(); + console.log = jest.fn(); + }); + + test('Should log when ready', () => { + const res = instance.ready(); + + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toHaveBeenCalledWith("Ready"); + }); }); \ No newline at end of file