Compare commits

...
This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.

3 commits

Author SHA1 Message Date
Vylpes
21711c66ad Event Tests 2021-02-13 18:07:18 +00:00
Vylpes
893c0e1675 Add Client Tests 2021-02-13 16:56:18 +00:00
Vylpes
15b4a168df Add Tests for util.loadCommand if user doesn't have roles or userid 2021-02-13 16:15:06 +00:00
7 changed files with 114 additions and 3 deletions

View file

@ -31,6 +31,8 @@ class event {
"message": message "message": message
}; };
} }
return false;
} }
// Emit when bot is logged in and ready to use // Emit when bot is logged in and ready to use

View file

@ -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;

View file

@ -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;

View file

@ -3,5 +3,12 @@
"author": { "author": {
"bot": false "bot": false
}, },
"content": "d!testing param1" "content": "d!testing param1",
"member": {
"roles": {
"cache": [
"NonRegular"
]
}
}
} }

View file

@ -1,5 +1,6 @@
const { client } = require('../../../src'); const { client } = require('../../../src');
const { readFileSync } = require('fs'); const { readFileSync } = require('fs');
const { expect } = require('@jest/globals');
// Mocks // Mocks
jest.mock('discord.js'); jest.mock('discord.js');
@ -18,10 +19,27 @@ describe('Client Tests', () => {
instance = new client(config, commandConfig); instance = new client(config, commandConfig);
expect(instance.config).toBe(config); expect(instance.config).toBe(config);
expect(instance.commandConfig).toBe(commandConfig);
expect(instance.events).toBeDefined(); expect(instance.events).toBeDefined();
expect(instance.util).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)', () => { test('Configure Client (Incorrect parameters: token)', () => {
expect(() => { expect(() => {
delete config.token; delete config.token;
@ -51,7 +69,9 @@ describe('Client Tests', () => {
}); });
test('Start Client', () => { test('Start Client', () => {
instance = new client(config, commandConfig); expect(() => {
instance.start(); instance = new client(config, commandConfig);
instance.start();
}).not.toThrow();
}); });
}); });

View file

@ -1,5 +1,6 @@
const events = require('../../../src/client/events'); const events = require('../../../src/client/events');
const { readFileSync } = require('fs'); const { readFileSync } = require('fs');
const { expect } = require('@jest/globals');
// Mocks // Mocks
jest.mock('discord.js'); jest.mock('discord.js');
@ -86,4 +87,43 @@ describe('events.message', () => {
instance.message(message) instance.message(message)
}).not.toThrow(); }).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");
});
}); });

View file

@ -83,4 +83,18 @@ describe('util.loadCommand', () => {
expect(res.valid).toBe(false); expect(res.valid).toBe(false);
expect(res.message).toBe('Command folder does not exist'); 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');
})
}); });