Skip to content

Commit 92a6d58

Browse files
authored
Merge pull request #113 from osu-cascades/rc-v2.1.0
2 parents 51d377f + d4686d7 commit 92a6d58

File tree

14 files changed

+2289
-2014
lines changed

14 files changed

+2289
-2014
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node
1+
v13.7
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Help Command Execute DMs commands with prefix and descriptions it still looks the same 1`] = `
4+
"I am here to help! Well...mostly just make you chuckle at this point, let's be honest.
5+
6+
Here is a list of the commands that we've got right now:
7+
\`\`\`
8+
!one → I am number one.
9+
!two → Two is not just a number.
10+
!blueFish → Not a red fish.
11+
\`\`\`"
12+
`;

__tests__/commands/help.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import Help from '../../src/commands/help';
2+
import Commands from '../../src/library/commands';
3+
import { message as mockMessage, MockedMessage } from '../mocks/discord';
4+
5+
// TODO: These should be in a factory/mock
6+
const oneCommand = {
7+
name: 'one',
8+
description: 'I am number one.',
9+
execute: jest.fn()
10+
};
11+
12+
const twoCommand = {
13+
name: 'two',
14+
description: 'Two is not just a number.',
15+
execute: jest.fn()
16+
};
17+
18+
const blueFishCommand = {
19+
name: 'blueFish',
20+
description: 'Not a red fish.',
21+
execute: jest.fn()
22+
};
23+
24+
const commands = new Commands({
25+
one: oneCommand,
26+
two: twoCommand,
27+
blueFish: blueFishCommand
28+
});
29+
30+
let sendMock: MockedMessage;
31+
let authorSend: MockedMessage;
32+
beforeEach(() => {
33+
sendMock = jest.fn();
34+
mockMessage.reply = sendMock;
35+
authorSend = jest.fn();
36+
// @ts-ignore
37+
mockMessage.author = {
38+
send: authorSend
39+
};
40+
});
41+
42+
describe('Help Command', () => {
43+
describe('Execute', () => {
44+
beforeEach(() => {
45+
Help.execute([], mockMessage, { commands });
46+
});
47+
48+
test('Lets you know to check your DMs', () => {
49+
expect(sendMock).lastCalledWith('sliding into your DMs...');
50+
});
51+
52+
describe('DMs commands with prefix and descriptions', () => {
53+
let message: string;
54+
beforeEach(() => {
55+
message = authorSend.mock.calls[0][0];
56+
});
57+
58+
test('Snarky', () => {
59+
const snark = "I am here to help! Well...mostly just make you chuckle " +
60+
"at this point, let's be honest.";
61+
expect(message).toContain(snark);
62+
});
63+
64+
test('Command pretext header', () => {
65+
const pretext = "Here is a list of the commands that we've got right now:";
66+
expect(message).toContain(pretext);
67+
});
68+
69+
test('Code block start', () => {
70+
expect(message).toContain('```\n');
71+
});
72+
73+
test('Code block end', () => {
74+
const lines = message.split('\n');
75+
const lastLine = lines[lines.length - 1];
76+
expect(lastLine).toEqual('```');
77+
});
78+
79+
test('it still looks the same', () => {
80+
expect(message).toMatchSnapshot();
81+
});
82+
83+
describe('Commands', () => {
84+
test('one command', () => {
85+
expect(message).toContain('!one');
86+
});
87+
88+
test('one description', () => {
89+
expect(message).toContain(oneCommand.description);
90+
});
91+
92+
test('two command', () => {
93+
expect(message).toContain('!two');
94+
});
95+
96+
test('two description', () => {
97+
expect(message).toContain(twoCommand.description);
98+
});
99+
100+
test('blueFish command', () => {
101+
expect(message).toContain('!blueFish');
102+
});
103+
104+
test('BlueFish description', () => {
105+
expect(message).toContain(blueFishCommand.description);
106+
});
107+
});
108+
});
109+
});
110+
});

__tests__/commands/search.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,24 @@ describe('Search Command', () => {
4343
await Search.execute(['dingusy'], mockMessage);
4444
expect(sendMock).lastCalledWith(results.items[0].link);
4545
});
46-
test('Malformed Response', async () => {
47-
const mockedData = Promise.resolve({ data: {} });
48-
axiosMock.get.mockResolvedValueOnce(mockedData);
49-
await Search.execute(['NOPE'], mockMessage);
50-
expect(sendMock).lastCalledWith("I'm Sorry Dave, I'm afraid I can't do that...");
46+
describe('Malformed Response', () => {
47+
let consoleErrorMock: jest.SpyInstance<void, any>;
48+
beforeEach(async () => {
49+
const mockedData = Promise.resolve({ data: {} });
50+
axiosMock.get.mockResolvedValueOnce(mockedData);
51+
consoleErrorMock = jest.spyOn(console, 'error')
52+
.mockImplementation(() => undefined); // Prevent it from spewing into the test results
53+
await Search.execute(['NOPE'], mockMessage);
54+
});
55+
afterEach(() => {
56+
consoleErrorMock.mockRestore();
57+
});
58+
test('Responds with error message', async () => {
59+
expect(sendMock).lastCalledWith("I'm Sorry Dave, I'm afraid I can't do that...");
60+
});
61+
test('Console logs an error', () => {
62+
const errorMessage = "Malformed Google Search Response: {}";
63+
expect(consoleErrorMock).lastCalledWith(errorMessage);
64+
});
5165
});
5266
});

__tests__/library/commandLoader.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import glob from 'glob';
22
import CommandLoader, { ICommandClasses } from '../../src/library/commandLoader';
3-
import { COMMANDS_PATH_GLOB } from './../../src/library/commands';
43

54
describe('CommandLoader', () => {
65
let commandClasses: ICommandClasses;
7-
const files = glob.sync(COMMANDS_PATH_GLOB);
6+
const commandsPathGlob = './src/commands/*.ts';
7+
const files = glob.sync(commandsPathGlob);
88

99
beforeEach(() => {
1010
commandClasses = CommandLoader.getCommandClasses(files);
@@ -16,6 +16,7 @@ describe('CommandLoader', () => {
1616
});
1717
});
1818

19+
// More of an integration test against real commands
1920
describe('Class names match their file names', () => {
2021
test('they match their key name', () => {
2122
for (let commandName of Object.keys(commandClasses)) {

__tests__/library/commands.test.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1+
import { ICommandClasses } from '../../src/library/commandLoader';
12
import Commands from '../../src/library/commands';
3+
import ICommand from '../../src/library/iCommand';
24

3-
// TODO: I feel like this class is too basic to test,
4-
// and ultimately a wrapper for other classes that have been tested
5-
// Might just remove it some day
65
describe('Commands', () => {
7-
const commands = new Commands();
8-
test('Has a command', () => {
9-
expect(Object.keys(commands.all).length).toBeGreaterThan(0);
10-
expect(commands.names.length).toBeGreaterThan(0);
6+
let mockHelloCommand: ICommand;
7+
let mockYetAnotherCommand: ICommand;
8+
let mockCommands: ICommandClasses;
9+
let commands: Commands;
10+
11+
beforeEach(() => {
12+
// TODO: these should probably go into a factory/mock
13+
mockHelloCommand = {
14+
name: 'Hello',
15+
description: 'Hello World',
16+
execute: jest.fn()
17+
};
18+
mockYetAnotherCommand = {
19+
name: 'YAC',
20+
description: 'Yet Another Command!',
21+
execute: jest.fn()
22+
};
23+
mockCommands = {
24+
hello: mockHelloCommand,
25+
yac: mockYetAnotherCommand
26+
};
27+
commands = new Commands(mockCommands);
28+
});
29+
30+
test('.names returns command names', () => {
31+
const commandNames = ['hello', 'yac'];
32+
expect(commands.names).toEqual(commandNames);
1133
});
34+
1235
test('Can fetch a command', () => {
13-
const first = commands.names[0];
14-
expect(commands.get(first)).not.toBeUndefined();
36+
const helloCommand = commands.get('hello');
37+
expect(helloCommand).toBe(mockHelloCommand);
38+
});
39+
40+
test('Finds the longest name', () => {
41+
expect(commands.longestNameLength()).toEqual(5);
1542
});
1643
});

0 commit comments

Comments
 (0)