From cc531b69289fa3f6853c8c71516bcda6f455107c Mon Sep 17 00:00:00 2001 From: Yannick Mortier Date: Fri, 28 Aug 2020 12:06:02 +0200 Subject: [PATCH] Fix joining team chosen by bot, expand tests --- src/commands/team.js | 10 +++++----- test/team.test.js | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/commands/team.js b/src/commands/team.js index ae17897..0686500 100644 --- a/src/commands/team.js +++ b/src/commands/team.js @@ -31,7 +31,7 @@ const teams = [ 'Blue Team', 'Gold Team', 'Green Team', 'Pink Team', 'Purple Tea */ module.exports = async function team(message, team) { const { author, channel, guild, member } = message; - const newTeam = await getTeam(message, team); + const newTeam = await determineTeam(message, team); if (!teams.includes(newTeam)) { return channel.send(`<@${ author.id }>, the \`${ newTeam }\` isn't a valid role.`); @@ -65,16 +65,16 @@ module.exports = async function team(message, team) { * @example * const team = getTeam(message, 'blue'); // 'Blue' */ -async function getTeam(message, team) { +async function determineTeam(message, team) { const { author, channel } = message; if (!isDefined(team)) { await channel.send(`If you won't pick a team, <@${ author.id }>, then I'll pick one for you!`); + + return pick(teams); } - const newTeam = isDefined(team) - ? titleCase(team) - : pick(teams); + const newTeam = titleCase(team); return `${ newTeam } Team`; } diff --git a/test/team.test.js b/test/team.test.js index fe5b30a..a7d5d4e 100644 --- a/test/team.test.js +++ b/test/team.test.js @@ -14,11 +14,15 @@ const channel = { }; const blueTeam = { name: 'Blue Team', id: 'blueteamid', toString: () => '@Blue Team' }; +const goldTeam = { name: 'Gold Team', id: 'goldteamid', toString: () => '@Gold Team' }; +const greenTeam = { name: 'Green Team', id: 'greenteamid', toString: () => '@Green Team' }; +const pinkTeam = { name: 'Pink Team', id: 'pinkteamid', toString: () => '@Pink Team' }; +const purpleTeam = { name: 'Purple Team', id: 'purpleteamid', toString: () => '@Purple Team' }; const redTeam = { name: 'Red Team', id: 'redteamid', toString: () => '@Red Team' }; const guild = { roles: { - cache: [ blueTeam, redTeam ] + cache: [ blueTeam, goldTeam, greenTeam, pinkTeam, purpleTeam, redTeam ] } }; @@ -36,9 +40,21 @@ test('#team: should randomly pick a role if none is provided', async (t) => { send: sinon.stub() }; - await team({ author, channel: channelStub, guild, member }); + const memberStub = { + roles: { + add: sinon.stub(), + cache: [] + } + }; + + await team({ author, channel: channelStub, guild, member: memberStub }); t.true(channelStub.send.calledWith('If you won\'t pick a team, <@original>, then I\'ll pick one for you!')); + for (const call of channelStub.send.getCalls()) { + t.true(call.notCalledWithMatch(sinon.match(/^<@original>, the `.+` isn't a valid role.$/u))); + } + t.true(memberStub.roles.add.calledWithMatch(sinon.match(/^\w+teamid$/u))); + t.true(channelStub.send.calledWithMatch(/^<@original> joined the @\w+ Team!$/u)); }); test('#team: should join the target team if not part of a team yet', async (t) => {