Skip to content

Commit 98b3986

Browse files
denis-onderljosberinn
authored andcommitted
Tweaked test
1 parent 2b1a71e commit 98b3986

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2-
preset: 'ts-jest'
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
34
};

src/commands/points/index.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { userNotFound } from '../../utils/errors';
1+
import { dbConnect } from '../..';
22

33
import pointsHandler from '.';
44

55
test('should throw an error if no user has been found', async () => {
6-
const sendMock = jest.fn();
7-
const replyMock = jest.fn();
6+
// Connect to database
7+
dbConnect();
88

99
const msg: any = {
1010
author: { id: '1', tag: 'test#1234' },
11-
channel: { send: sendMock },
12-
reply: replyMock,
11+
channel: { send: jest.fn() },
12+
delete: jest.fn(),
13+
reply: jest.fn(),
1314
};
1415

15-
const res = await pointsHandler(msg);
16-
expect(res).toThrowError(userNotFound);
16+
await pointsHandler(msg);
17+
expect(msg.channel.send.mock.calls[0]).toMatchSnapshot();
1718
});

src/commands/points/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import { Message } from 'discord.js';
33
import { IUser } from '../../helpful_role';
44
import User from '../../helpful_role/db_model';
55
import { createEmbed } from '../../utils/discordTools';
6-
import { userNotFound } from '../../utils/errors';
76

87
export default async (msg: Message) => {
98
try {
109
const user: IUser = await User.findOne({ user: msg.author.id });
11-
if (!user) throw new Error(userNotFound);
10+
const points = user.points ? user.points : 0;
1211

1312
const output = createEmbed({
14-
description: `You have accumulated ${user.points} point${
15-
user.points !== 1 ? 's' : ''
13+
description: `You have accumulated ${points} point${
14+
points !== 1 ? 's' : ''
1615
}.`,
1716
footerText: 'Helpful User Points',
1817
provider: 'spam',

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,17 @@ const handleReactionAdd = async (reaction: MessageReaction, user: User) => {
207207
};
208208

209209
// Establish a connection with the database
210-
mongoose
211-
.connect(MONGO_URI, {
212-
useNewUrlParser: true,
213-
useUnifiedTopology: true,
214-
})
215-
.then(() => console.log('MongoDB connection established.'))
216-
.catch(error => console.error('mongoose.connect():', error));
210+
export const dbConnect = () => {
211+
mongoose
212+
.connect(MONGO_URI, {
213+
useNewUrlParser: true,
214+
useUnifiedTopology: true,
215+
})
216+
.then(() => console.log('MongoDB connection established.'))
217+
.catch(error => console.error('mongoose.connect():', error));
218+
};
219+
220+
dbConnect();
217221

218222
client.on('message', handleMessage);
219223
client.on('messageReactionAdd', handleReactionAdd);

src/utils/errors.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export const noResults = (search: string) =>
66
`sorry, could not find anything for \`${search}\`. Autodeleting this response in 30 seconds.`;
77
export const unknownError = `sorry, something went wrong. If this issue persists, please file an issue at ${REPO_LINK}`;
88
export const missingRightsDeletion =
9-
'insufficient permissions: unable to delete message';
9+
'insufficient permissions: unable to delete message.';
10+
export const userNotFound =
11+
'sorry, your user ID cannot be found within the database.';

0 commit comments

Comments
 (0)