Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/commands/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -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`;
}
Expand Down
20 changes: 18 additions & 2 deletions test/team.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}
};

Expand All @@ -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) => {
Expand Down