This commit is contained in:
Vylpes 2021-01-16 19:06:35 +00:00
parent 7840bc9be8
commit 80e9daa8dc
24 changed files with 5435 additions and 111 deletions

14
tests/commands/testing.js Normal file
View file

@ -0,0 +1,14 @@
const { command } = require('../../src');
class test extends command {
constructor() {
super("test");
super.configs = "tester";
}
test(context) {
context.message.reply(`Testing done by ${context.config.tester}`);
}
}
module.exports = test;

0
tests/events/.gitkeep Normal file
View file

View file

@ -0,0 +1,5 @@
{
"testing": {
"tester": "General Kenobi"
}
}

6
tests/json/config.json Normal file
View file

@ -0,0 +1,6 @@
{
"token": "TOKEN",
"prefix": "d!",
"commands": "tests/commands",
"events": "tests/events"
}

7
tests/json/message.json Normal file
View file

@ -0,0 +1,7 @@
{
"guild": "000000000000000000",
"author": {
"bot": false
},
"content": "d!testing param1"
}

View file

@ -0,0 +1,57 @@
const { client } = require('../../../src');
const { readFileSync } = require('fs');
// Mocks
jest.mock('discord.js');
describe('Client Tests', () => {
let instance;
let config;
let commandConfig;
beforeEach(() => {
config = JSON.parse(readFileSync('tests/json/config.json'));
commandConfig = JSON.parse(readFileSync('tests/json/commandConfig.json'));
});
test('Configure Client (Correct paramaters)', () => {
instance = new client(config, commandConfig);
expect(instance.config).toBe(config);
expect(instance.events).toBeDefined();
expect(instance.util).toBeDefined();
});
test('Configure Client (Incorrect parameters: token)', () => {
expect(() => {
delete config.token;
instance = new client(config, commandConfig);
}).toThrow();
});
test('Configure Client (Incorrect parameters: prefix)', () => {
expect(() => {
delete config.prefix;
instance = new client(config, commandConfig);
}).toThrow();
});
test('Configure Client (Incorrect parameters: commands)', () => {
expect(() => {
delete config.commands;
instance = new client(config, commandConfig);
}).toThrow();
});
test('Configure Client (Incorrect parameters: events)', () => {
expect(() => {
delete config.events;
instance = new client(config, commandConfig);
}).toThrow();
});
test('Start Client', () => {
instance = new client(config, commandConfig);
instance.start();
});
});

View file

@ -0,0 +1,89 @@
const events = require('../../../src/client/events');
const { readFileSync } = require('fs');
// Mocks
jest.mock('discord.js');
describe('events.message', () => {
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"
}
});
});
test('If no message should return', () => {
const res = instance.message();
expect(res).toBe(false);
});
test('If no guild should return', () => {
delete message.guild;
const res = instance.message(message);
expect(res).toBe(false);
});
test('If author is a bot should return', () => {
message.author.bot = true;
const res = instance.message(message);
expect(res).toBe(false);
});
test('Should loadCommand', () => {
const res = instance.message(message);
expect(instance.util.loadCommand).toHaveBeenCalledTimes(1);
expect(instance.util.loadCommand).toHaveBeenCalledWith('testing', ['param1'], message);
});
test('Should return correct values', () => {
const res = instance.message(message);
expect(res.prefix).toBe('d!');
expect(res.name).toBe('testing');
expect(res.args[0]).toBe('param1');
expect(res.message).toBe(message);
});
test('Should throw if response is invalid', () => {
instance.util.loadCommand = jest.fn(() => {
return {
"valid": false,
"message": "Invalid"
}
});
expect(() => {
instance.message(message)
}).toThrow('Invalid');
});
test('Should not throw if file does not exist', () => {
instance.util.loadCommand = jest.fn(() => {
return {
"valid": false,
"message": "File does not exist"
}
});
expect(() => {
instance.message(message)
}).not.toThrow();
});
});

View file

@ -0,0 +1,77 @@
const util = require('../../../src/client/util');
const { readFileSync, read } = require('fs');
// Mocks
jest.mock('discord.js');
const fs = jest.createMockFromModule('fs');
fs.stat = jest.fn((path, cb) => {
cb(null);
});
describe('util.constructor', () => {
let instance;
let client;
beforeEach(() => {
client = jest.fn();
instance = new util(client);
});
test('Should set client', () => {
expect(instance._client).toBeDefined();
});
});
describe('util.loadCommand', () => {
let instance;
let message;
let client;
beforeEach(() => {
client = jest.fn();
client.config = JSON.parse(readFileSync('tests/json/config.json'));
client.commandConfig = JSON.parse(readFileSync('tests/json/commandConfig.json'));
instance = new util(client);
message = JSON.parse(readFileSync('tests/json/message.json'));
message.reply = jest.fn();
});
test('Should load command correctly', () => {
let res = instance.loadCommand('testing', 'param1', message);
expect(res.valid).toBe(true);
expect(res.message).toBe("loaded command 'testing' with arguments 'param1'");
});
test('Should load command correctly (no arguments)', () => {
let res = instance.loadCommand('testing', '', message);
expect(res.valid).toBe(true);
expect(res.message).toBe("loaded command 'testing' with arguments ''");
});
test('Should be invalid if it tries to load an undefined command', () => {
let res = instance.loadCommand('testingz', '', message);
expect(res.valid).toBe(false);
expect(res.message).toBe('File does not exist');
});
test('Should be invalid if incorrect configs', () => {
delete client.commandConfig.testing;
let res = instance.loadCommand('testing', 'param1', message);
expect(res.valid).toBe(false);
expect(res.message).toBe("test requires tester in it's configuration");
});
test('Should be invalid if incorrect configs (single config)', () => {
delete client.commandConfig.testing.tester;
let res = instance.loadCommand('testing', 'param1', message);
expect(res.valid).toBe(false);
expect(res.message).toBe("test requires tester in it's configuration");
});
});

View file

@ -0,0 +1,72 @@
const { generateResponse, validateConfig } = require('../../../src/client/validation');
const { readFileSync } = require('fs');
describe('Validation: generateResponse', () => {
test('Returns the corect response', () => {
const isValidWithMessage = generateResponse(true, "Test Message");
const isValidNoMessage = generateResponse(true);
const notValidWithMessage = generateResponse(false, "Test Message");
const notValidNoMessage = generateResponse(false);
expect(isValidWithMessage.valid).toBe(true);
expect(isValidWithMessage.message).toBe('Test Message');
expect(isValidNoMessage.valid).toBe(true);
expect(isValidNoMessage.message).toBe('No message was given');
expect(notValidWithMessage.valid).toBe(false);
expect(notValidWithMessage.message).toBe('Test Message');
expect(notValidNoMessage.valid).toBe(false);
expect(notValidNoMessage.message).toBe('No message was given');
});
});
describe('Validation: validateConfig', () => {
let config;
let expected;
beforeEach(() => {
config = JSON.parse(readFileSync('tests/json/config.json'));
expected = JSON.parse(readFileSync('src/json/expectedConfig.json'));
});
test('Validates expected config as valid', () => {
const res = validateConfig(config, expected);
expect(res.valid).toBe(true);
expect(res.message).toBe('No message was given');
});
test('Validates unexpected config as invalid (token)', () => {
delete config.token;
const res = validateConfig(config, expected);
expect(res.valid).toBe(false);
expect(res.message).toBe("'token' is not defined");
});
test('Validates unexpected config as invalid (prefix)', () => {
delete config.prefix;
const res = validateConfig(config, expected);
expect(res.valid).toBe(false);
expect(res.message).toBe("'prefix' is not defined");
});
test('Validates unexpected config as invalid (commands)', () => {
delete config.commands;
const res = validateConfig(config, expected);
expect(res.valid).toBe(false);
expect(res.message).toBe("'commands' is not defined");
});
test('Validates unexpected config as invalid (events)', () => {
delete config.events;
const res = validateConfig(config, expected);
expect(res.valid).toBe(false);
expect(res.message).toBe("'events' is not defined");
});
});

View file

@ -0,0 +1,190 @@
const command = require('../../../src/type/command');
describe('Command: constructor', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Command run is set correctly', () => {
expect(instance.run).toBe("test");
});
test('Command roles is set correctly', () => {
expect(typeof instance._roles).toBe('object');
expect(instance._roles.length).toBe(0);
});
test('Command configs is set correctly', () => {
expect(typeof instance._configs).toBe('object');
expect(instance._configs.length).toBe(0);
});
test('Command users is set correctly', () => {
expect(typeof instance._users).toBe('object');
expect(instance._users.length).toBe(0);
});
});
describe('Command: description', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Setting description', () => {
instance.description = "desc";
expect(instance._description).toBe("desc");
});
test('Getting description', () => {
instance.description = "desc";
expect(instance.description).toBe("desc");
});
});
describe('Command: category', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Setting category', () => {
instance.category = "cat";
expect(instance._category).toBe("cat");
});
test('Getting category', () => {
instance.category = "cat";
expect(instance.category).toBe("cat");
});
});
describe('Command: usage', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Setting usage', () => {
instance.usage = "use";
expect(instance._usage).toBe("use");
});
test('Getting usage', () => {
instance.usage = "use";
expect(instance.usage).toBe("use");
});
});
describe('Command: roles', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Setting roles (1 role)', () => {
instance.roles = "role0";
expect(instance._roles.length).toBe(1);
expect(instance._roles[0]).toBe("role0");
});
test('Getting roles (1 role)', () => {
instance.roles = "role0";
expect(instance.roles.length).toBe(1);
expect(instance.roles[0]).toBe("role0");
});
test('Setting roles (2 roles)', () => {
instance.roles = "role0";
instance.roles = "role1";
expect(instance._roles.length).toBe(2);
expect(instance._roles[0]).toBe("role0");
expect(instance._roles[1]).toBe("role1");
});
test('Getting roles (2 roles)', () => {
instance.roles = "role0";
instance.roles = "role1";
expect(instance.roles.length).toBe(2);
expect(instance.roles[0]).toBe("role0");
expect(instance.roles[1]).toBe("role1");
});
});
describe('Command: configs', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Setting configs (1 config)', () => {
instance.configs = "config0";
expect(instance._configs.length).toBe(1);
expect(instance._configs[0]).toBe("config0");
});
test('Getting configs (1 config)', () => {
instance.configs = "config0";
expect(instance.configs.length).toBe(1);
expect(instance.configs[0]).toBe("config0");
});
test('Setting configs (2 configs)', () => {
instance.configs = "config0";
instance.configs = "config1";
expect(instance._configs.length).toBe(2);
expect(instance._configs[0]).toBe("config0");
expect(instance._configs[1]).toBe("config1");
});
test('Getting configs (2 configs)', () => {
instance.configs = "config0";
instance.configs = "config1";
expect(instance.configs.length).toBe(2);
expect(instance.configs[0]).toBe("config0");
expect(instance.configs[1]).toBe("config1");
});
});
describe('Command: users', () => {
let instance;
beforeEach(() => {
instance = new command("test");
});
test('Setting users (1 user)', () => {
instance.users = "user0";
expect(instance._users.length).toBe(1);
expect(instance._users[0]).toBe("user0");
});
test('Getting users (1 user)', () => {
instance.users = "user0";
expect(instance.users.length).toBe(1);
expect(instance.users[0]).toBe("user0");
});
test('Setting users (2 user)', () => {
instance.users = "user0";
instance.users = "user1";
expect(instance._users.length).toBe(2);
expect(instance._users[0]).toBe("user0");
expect(instance._users[1]).toBe("user1");
});
test('Getting users (2 users)', () => {
instance.users = "user0";
instance.users = "user1";
expect(instance.users.length).toBe(2);
expect(instance.users[0]).toBe("user0");
expect(instance.users[1]).toBe("user1");
});
});

View file

@ -0,0 +1,13 @@
const event = require('../../../src/type/event');
describe('Event: constructor', () => {
let instance;
beforeEach(() => {
instance = new event("test");
});
test('Event run is set correctly', () => {
expect(instance.run).toBe("test");
});
});