Feature/12 create tests #102
2 changed files with 250 additions and 10 deletions
|
@ -1,25 +1,30 @@
|
||||||
import { GuildMember } from "discord.js";
|
import { GuildMember } from "discord.js";
|
||||||
|
import IEventReturnContext from "../../contracts/IEventReturnContext";
|
||||||
import EventEmbed from "../../helpers/embeds/EventEmbed";
|
import EventEmbed from "../../helpers/embeds/EventEmbed";
|
||||||
|
|
||||||
export default class GuildMemberUpdate {
|
export default class GuildMemberUpdate {
|
||||||
private _oldMember: GuildMember;
|
public oldMember: GuildMember;
|
||||||
private _newMember: GuildMember;
|
public newMember: GuildMember;
|
||||||
|
|
||||||
constructor(oldMember: GuildMember, newMember: GuildMember) {
|
constructor(oldMember: GuildMember, newMember: GuildMember) {
|
||||||
this._oldMember = oldMember;
|
this.oldMember = oldMember;
|
||||||
this._newMember = newMember;
|
this.newMember = newMember;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NicknameChanged() {
|
public NicknameChanged(): IEventReturnContext {
|
||||||
const oldNickname = this._oldMember.nickname || "*none*";
|
const oldNickname = this.oldMember.nickname || "*none*";
|
||||||
const newNickname = this._newMember.nickname || "*none*";
|
const newNickname = this.newMember.nickname || "*none*";
|
||||||
|
|
||||||
const embed = new EventEmbed(this._newMember.guild, "Nickname Changed");
|
const embed = new EventEmbed(this.newMember.guild, "Nickname Changed");
|
||||||
embed.AddUser("User", this._newMember.user, true);
|
embed.AddUser("User", this.newMember.user, true);
|
||||||
embed.addField("Before", oldNickname, true);
|
embed.addField("Before", oldNickname, true);
|
||||||
embed.addField("After", newNickname, true);
|
embed.addField("After", newNickname, true);
|
||||||
embed.setFooter(`Id: ${this._newMember.user.id}`);
|
embed.setFooter(`Id: ${this.newMember.user.id}`);
|
||||||
|
|
||||||
embed.SendToMemberLogsChannel();
|
embed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
return {
|
||||||
|
embeds: [embed]
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
235
tests/events/MemberEvents/GuildMemberUpdate.test.ts
Normal file
235
tests/events/MemberEvents/GuildMemberUpdate.test.ts
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
import { GuildMember, TextChannel } from "discord.js";
|
||||||
|
import GuildMemberUpdate from "../../../src/events/MemberEvents/GuildMemberUpdate";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties are set', () => {
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'Old Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'New Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
expect(guildMemberUpdate.oldMember).toBe(oldMember);
|
||||||
|
expect(guildMemberUpdate.newMember).toBe(newMember);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('NicknameChanged', () => {
|
||||||
|
test('Given nickname has changed from one to another, expect embed to be sent with both', () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const memberUserDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'Old Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'New Nickname',
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: memberUserDisplayAvatarURL
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
const result = guildMemberUpdate.NicknameChanged();
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Nickname Changed');
|
||||||
|
expect(embed.footer?.text).toBe('Id: USERID');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('Old Nickname');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('New Nickname');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given old nickname was null, expect embed to say old nickname was none', () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const memberUserDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMember = {} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'New Nickname',
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: memberUserDisplayAvatarURL
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
const result = guildMemberUpdate.NicknameChanged();
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Nickname Changed');
|
||||||
|
expect(embed.footer?.text).toBe('Id: USERID');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('*none*');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('New Nickname');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given new nickname was null, expect embed to say new nickname was none', () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const memberUserDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'Old Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: memberUserDisplayAvatarURL
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
const result = guildMemberUpdate.NicknameChanged();
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Nickname Changed');
|
||||||
|
expect(embed.footer?.text).toBe('Id: USERID');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('Old Nickname');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('*none*');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue