Feature/12 create tests #102
4 changed files with 188 additions and 4 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -104,3 +104,4 @@ dist
|
||||||
.tern-port
|
.tern-port
|
||||||
|
|
||||||
config.json
|
config.json
|
||||||
|
.DS_Store
|
6
src/contracts/IEventReturnContext.ts
Normal file
6
src/contracts/IEventReturnContext.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { MessageEmbed } from "discord.js";
|
||||||
|
|||||||
|
import { ICommandContext } from "./ICommandContext";
|
||||||
|
|
||||||
|
export default interface ICommandReturnContext {
|
||||||
|
embeds: MessageEmbed[]
|
||||||
|
}
|
|
@ -2,35 +2,48 @@ import { Event } from "../type/event";
|
||||||
import { GuildMember } from "discord.js";
|
import { GuildMember } from "discord.js";
|
||||||
import EventEmbed from "../helpers/embeds/EventEmbed";
|
import EventEmbed from "../helpers/embeds/EventEmbed";
|
||||||
import GuildMemberUpdate from "./MemberEvents/GuildMemberUpdate";
|
import GuildMemberUpdate from "./MemberEvents/GuildMemberUpdate";
|
||||||
|
import IEventReturnContext from "../contracts/IEventReturnContext";
|
||||||
|
|
||||||
export default class MemberEvents extends Event {
|
export default class MemberEvents extends Event {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override guildMemberAdd(member: GuildMember) {
|
public override guildMemberAdd(member: GuildMember): IEventReturnContext {
|
||||||
const embed = new EventEmbed(member.guild, "Member Joined");
|
const embed = new EventEmbed(member.guild, "Member Joined");
|
||||||
embed.AddUser("User", member.user, true);
|
embed.AddUser("User", member.user, true);
|
||||||
embed.addField("Created", member.user.createdAt);
|
embed.addField("Created", member.user.createdAt);
|
||||||
embed.setFooter(`Id: ${member.user.id}`);
|
embed.setFooter(`Id: ${member.user.id}`);
|
||||||
|
|
||||||
embed.SendToMemberLogsChannel();
|
embed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
return {
|
||||||
|
embeds: [embed]
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override guildMemberRemove(member: GuildMember) {
|
public override guildMemberRemove(member: GuildMember): IEventReturnContext {
|
||||||
const embed = new EventEmbed(member.guild, "Member Left");
|
const embed = new EventEmbed(member.guild, "Member Left");
|
||||||
embed.AddUser("User", member.user, true);
|
embed.AddUser("User", member.user, true);
|
||||||
embed.addField("Joined", member.joinedAt);
|
embed.addField("Joined", member.joinedAt);
|
||||||
embed.setFooter(`Id: ${member.user.id}`);
|
embed.setFooter(`Id: ${member.user.id}`);
|
||||||
|
|
||||||
embed.SendToMemberLogsChannel();
|
embed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
return {
|
||||||
|
embeds: [embed]
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember) {
|
public override guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember): IEventReturnContext {
|
||||||
const handler = new GuildMemberUpdate(oldMember, newMember);
|
const handler = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
if (oldMember.nickname != newMember.nickname) { // Nickname change
|
if (oldMember.nickname != newMember.nickname) { // Nickname change
|
||||||
handler.NicknameChanged();
|
handler.NicknameChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
embeds: []
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
164
tests/events/MemberEvents.test.ts
Normal file
164
tests/events/MemberEvents.test.ts
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
import { GuildMember, TextChannel, User } from "discord.js";
|
||||||
|
import MemberEvents from "../../src/events/MemberEvents";
|
||||||
|
import GuildMemberUpdate from "../../src/events/MemberEvents/GuildMemberUpdate";
|
||||||
|
|
||||||
|
describe('GuildMemberAdd', () => {
|
||||||
|
test('When event is fired, expect embed to be sent to logs channel', () => {
|
||||||
|
const currentDate = new Date();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
send: jest.fn()
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const userDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const guildUser = {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
createdAt: currentDate,
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: userDisplayAvatarURL
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const guildMember = {
|
||||||
|
user: guildUser,
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = memberEvents.guildMemberAdd(guildMember);
|
||||||
|
|
||||||
|
expect(textChannel.send).toBeCalledTimes(1);
|
||||||
|
expect(userDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe("Member Joined");
|
||||||
|
expect(embed.footer?.text).toBe("Id: USERID");
|
||||||
|
expect(embed.fields.length).toBe(2);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe("User");
|
||||||
|
expect(embedFieldUser.value).toBe("[object Object] `USERTAG`");
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Created Field
|
||||||
|
const embedFieldCreated = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldCreated.name).toBe("Created");
|
||||||
|
expect(embedFieldCreated.value).toBe(currentDate.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GuildMemberRemove', () => {
|
||||||
|
test('When event is fired, expect embed to be sent to logs channel', () => {
|
||||||
|
const currentDate = new Date();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
send: jest.fn()
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const userDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const guildUser = {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
createdAt: currentDate,
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: userDisplayAvatarURL
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const guildMember = {
|
||||||
|
user: guildUser,
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
joinedAt: currentDate
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = memberEvents.guildMemberRemove(guildMember);
|
||||||
|
|
||||||
|
expect(textChannel.send).toBeCalledTimes(1);
|
||||||
|
expect(userDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe("Member Left");
|
||||||
|
expect(embed.footer?.text).toBe("Id: USERID");
|
||||||
|
expect(embed.fields.length).toBe(2);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe("User");
|
||||||
|
expect(embedFieldUser.value).toBe("[object Object] `USERTAG`");
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Joined Field
|
||||||
|
const embedFieldJoined = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldJoined.name).toBe("Joined");
|
||||||
|
expect(embedFieldJoined.value).toBe(currentDate.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GuildMemberUpdate', () => {
|
||||||
|
test('Given nicknames are the same, expect NicknameChanged NOT to be called', () => {
|
||||||
|
const member = {
|
||||||
|
nickname: 'member'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const nicknameChanged = jest.fn();
|
||||||
|
|
||||||
|
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = memberEvents.guildMemberUpdate(member, member);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
expect(nicknameChanged).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given nicknames are the different, expect NicknameChanged to be called', () => {
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'oldMember'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'newMember'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const nicknameChanged = jest.fn();
|
||||||
|
|
||||||
|
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = memberEvents.guildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
expect(nicknameChanged).toBeCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue
Since this will be the same once
ICommandReturnContext
is changed, merge these into aIReturnContext
maybe