updating tests and memberHelper to try and make parseMemberCommand less bloated/broken (not working ATM)

This commit is contained in:
Aster Fialla
2026-02-22 21:40:42 -05:00
parent c3e7bf4433
commit be24034d48
3 changed files with 317 additions and 198 deletions

View File

@@ -12,7 +12,8 @@ cmds.set('member', {
const authorFull = `${message.author.username}#${message.author.discriminator}` const authorFull = `${message.author.username}#${message.author.discriminator}`
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null; const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
const attachmentExpires = message.attachments.size > 0 ? message.attachments.first().expires_at : null; const attachmentExpires = message.attachments.size > 0 ? message.attachments.first().expires_at : null;
const reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires).catch(async (e) =>{await message.reply(e.message);}); if (attachmentUrl) args.push(null, attachmentUrl, attachmentExpires);
const reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args).catch(async (e) =>{await message.reply(e.message);});
if (typeof reply === 'string') { if (typeof reply === 'string') {
return await message.reply(reply); return await message.reply(reply);
} }

View File

@@ -6,8 +6,8 @@ import {utils} from "./utils.js";
const mh = {}; const mh = {};
// Has an empty "command" to parse the help message properly const commandList = ['new', 'remove', 'name', 'list', 'displayName', 'proxy', 'propic'];
const commandList = ['--help', 'new', 'remove', 'name', 'list', 'displayName', 'proxy', 'propic', '']; const newAndRemoveCommands = ['new', 'remove'];
/** /**
* Parses through the subcommands that come after "pf;member" and calls functions accordingly. * Parses through the subcommands that come after "pf;member" and calls functions accordingly.
@@ -16,35 +16,97 @@ const commandList = ['--help', 'new', 'remove', 'name', 'list', 'displayName', '
* @param {string} authorId - The id of the message author * @param {string} authorId - The id of the message author
* @param {string} authorFull - The username and discriminator of the message author * @param {string} authorFull - The username and discriminator of the message author
* @param {string[]} args - The message arguments * @param {string[]} args - The message arguments
* @param {string | null} attachmentUrl - The message attachment url.
* @param {string | null} attachmentExpiration - The message attachment expiration (if uploaded via Fluxer)
* @returns {Promise<string>} A success message. * @returns {Promise<string>} A success message.
* @returns {Promise <EmbedBuilder>} A list of 25 members as an embed. * @returns {Promise <EmbedBuilder>} A list of 25 members as an embed.
* @returns {Promise <EmbedBuilder>} A list of member commands and descriptions. * @returns {Promise <EmbedBuilder>} A list of member commands and descriptions.
* @returns {Promise<{EmbedBuilder, [string], string}>} A member info embed + info/errors. * @returns {Promise<{EmbedBuilder, [string], string}>} A member info embed + info/errors.
* @throws {Error} * @throws {Error}
*/ */
mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUrl = null, attachmentExpiration = null) { mh.parseMemberCommand = async function (authorId, authorFull, args) {
if (!args[0]) { let memberName, command, isHelp = false;
// checks whether command is in list, otherwise assumes it's a name
// ex: pf;member, pf;member --help
if (args.length === 0 || args[0] === '--help' || args[0] === '') {
return mh.getMemberCommandInfo(); return mh.getMemberCommandInfo();
} }
if (args[0] === "new") { // ex: pf;member remove somePerson
if (!args[1] || args[1] === "--help") return enums.help.NEW; if (commandList.includes(args[0]) && args[1]) {
return await mh.addNewMember(authorId, args, attachmentUrl).catch((e) => { throw e }); command = args[0];
memberName = args[1];
}
// ex: pf;member somePerson propic
else if (args[1] && commandList.includes(args[1])) {
command = args[1];
memberName = args[0];
}
// ex: pf;member somePerson
else if (!commandList.includes(args[0]) && !args[1]) {
memberName = args[0];
}
// ex: pf;member remove, pf;member remove --help
if (command && (!memberName || args.includes("--help"))) {
isHelp = true;
} }
const memberName = !commandList.includes(args[0]) ? args[0] : args[1]; return await mh.memberArgumentHandler(authorId, authorFull, isHelp, command, memberName, args)
}
// checks whether command is in list, otherwise assumes it's a name mh.memberArgumentHandler = async function(authorId, authorFull, isHelp, command = null, memberName = null,args = null) {
if (!command && !memberName && !isHelp) {
throw new Error(enums.err.COMMAND_NOT_RECOGNIZED);
}
// ex: pf;member blah blah
if (!memberName && !isHelp) {
throw new Error(enums.err.NO_MEMBER);
}
// remove memberName and command from values to reduce confusion
console.log(command, memberName, isHelp, args);
let values = args.slice(2);
console.log(values);
if (isHelp) {
mh.sendHelpEnum(command);
}
else if (command === "list") {
return await mh.getAllMembersInfo(authorId, authorFull);
}
else if (command && memberName && values || newAndRemoveCommands.includes(command) && memberName) {
return await mh.memberCommandHandler(authorId, command, memberName, values);
}
else if (command && memberName) {
return await mh.sendCurrentValue(authorId, memberName, command);
}
}
mh.sendCurrentValue = async function(authorId, memberName, command) {
const member = await mh.getMemberByName(authorId, memberName).then((m) => { const member = await mh.getMemberByName(authorId, memberName).then((m) => {
if (!m) throw new Error(enums.err.NO_MEMBER); if (!m) throw new Error(enums.err.NO_MEMBER);
return m; return m;
}) });
switch (args[0]) { if (!command) {
return mh.getMemberInfo(member);
}
switch (command) {
case 'name':
return `The name of ${member.name} is \"${member.name}\" but you probably already knew that!`;
case 'displayname':
return `The display name for ${member.name} is \"${member.displayname}\".`;
case 'proxy':
return `The proxy for ${member.name} is \"${member.proxy}\".`;
case 'propic':
return `The profile picture for ${member.name} is \"${member.propic}\".`;
}
}
mh.sendHelpEnum = function(command) {
switch (command) {
case 'new':
return enums.help.NEW;
case 'remove': case 'remove':
if (!args[1] || args[1] === "--help") return enums.help.REMOVE; return enums.help.REMOVE;
return await mh.removeMember(authorId, memberName).catch((e) => { throw e });
case 'name': case 'name':
return enums.help.NAME; return enums.help.NAME;
case 'displayname': case 'displayname':
@@ -54,27 +116,25 @@ mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUr
case 'propic': case 'propic':
return enums.help.PROPIC; return enums.help.PROPIC;
case 'list': case 'list':
if (args[1] && args[1] === "--help") return enums.help.LIST; return enums.help.LIST;
return await mh.getAllMembersInfo(authorId, authorFull).catch((e) => { throw e });
case '--help':
case '':
return mh.getMemberCommandInfo();
} }
switch (args[1]) { }
mh.memberCommandHandler = async function(authorId, command, memberName, values) {
switch (command) {
case 'new':
return await mh.addNewMember(authorId, memberName, values).catch((e) => {throw e});
case 'remove':
return await mh.removeMember(authorId, memberName).catch((e) => {throw e});
case 'name': case 'name':
if (!args[2]) return member.name; return await mh.updateName(authorId, memberName, values[0]).catch((e) => {throw e});
return await mh.updateName(authorId, args[0], args[2]).catch((e) => { throw e});
case 'displayname': case 'displayname':
if (!args[2]) return member.displayname ?? `Display name ${enums.err.NO_VALUE}`; return await mh.updateDisplayName(authorId, memberName, values[0]).catch((e) => {throw e});
return await mh.updateDisplayName(authorId, args[0], args[2]).catch((e) => {throw e});
case 'proxy': case 'proxy':
if (!args[2]) return member.proxy ?? `Proxy ${enums.err.NO_VALUE}`; return await mh.updateProxy(authorId, memberName, values[0]).catch((e) => {throw e});
return await mh.updateProxy(authorId, args[0], args[2]).catch((e) => {throw e});
case 'propic': case 'propic':
if (!args[2] && !attachmentUrl) return member.propic ?? `Profile picture ${enums.err.NO_VALUE}`; return await mh.updatePropic(authorId, memberName, values).catch((e) => {throw e});
return await mh.updatePropic(authorId, args[0], args[2], attachmentUrl, attachmentExpiration).catch((e) => {throw e});
default:
return await mh.getMemberInfo(member);
} }
} }
@@ -83,19 +143,19 @@ mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUr
* *
* @async * @async
* @param {string} authorId - The author of the message * @param {string} authorId - The author of the message
* @param {string[]} args - The message arguments * @param {string} memberName - The member name
* @param {string | null} attachmentURL - The attachment URL, if any exists * @param {string[]} values - The arguments following the member name and command
* @returns {Promise<string>} A successful addition. * @returns {Promise<string>} A successful addition.
* @throws {Error} When the member exists, or creating a member doesn't work. * @throws {Error} When the member exists, or creating a member doesn't work.
*/ */
mh.addNewMember = async function (authorId, args, attachmentURL = null) { mh.addNewMember = async function (authorId, memberName, values) {
const memberName = args[1]; const displayName = values[2];
const displayName = args[2]; const proxy = values[3];
const proxy = args[3]; const propic = values[4] ?? values[5];
const propic = args[4] ?? attachmentURL;
return await mh.addFullMember(authorId, memberName, displayName, proxy, propic).then(async(response) => { return await mh.addFullMember(authorId, memberName, displayName, proxy, propic).then(async(response) => {
const memberInfoEmbed = await mh.getMemberInfo(response.member).catch((e) => {throw e}) const memberInfoEmbed = mh.getMemberInfo(response.member);
return {embed: memberInfoEmbed, errors: response.errors, success: `${memberName} has been added successfully.`}; return {embed: memberInfoEmbed, errors: response.errors, success: `${memberName} has been added successfully.`};
}).catch(e => { }).catch(e => {
console.error(e); console.error(e);
@@ -155,7 +215,7 @@ mh.updateDisplayName = async function (authorId, membername, displayname) {
* @param {string} memberName - The member to update * @param {string} memberName - The member to update
* @param {string} proxy - The proxy to set * @param {string} proxy - The proxy to set
* @returns {Promise<string> } A successful update. * @returns {Promise<string> } A successful update.
* @throws {RangeError | Error} When an empty proxy was provided, or no proxy exists. * @throws {Error} When an empty proxy was provided, or a proxy exists.
*/ */
mh.updateProxy = async function (authorId, memberName, proxy) { mh.updateProxy = async function (authorId, memberName, proxy) {
// Throws error if exists // Throws error if exists
@@ -170,17 +230,17 @@ mh.updateProxy = async function (authorId, memberName, proxy) {
* @async * @async
* @param {string} authorId - The author of the message * @param {string} authorId - The author of the message
* @param {string} memberName - The member to update * @param {string} memberName - The member to update
* @param {string | null} imgUrl - The message arguments * @param {string} values - The message arguments
* @param {string | null} attachmentUrl - The url of the first attachment in the message
* @param {string | null} attachmentExpiry - The expiration date of the first attachment in the message (if uploaded to Fluxer)
* @returns {Promise<string>} A successful update. * @returns {Promise<string>} A successful update.
* @throws {Error} When loading the profile picture from a URL doesn't work. * @throws {Error} When loading the profile picture from a URL doesn't work.
*/ */
mh.updatePropic = async function (authorId, memberName, imgUrl = null, attachmentUrl = null, attachmentExpiry = null) { mh.updatePropic = async function (authorId, memberName, values) {
const imgUrl = values[0] ?? values[1];
const attachmentExpiry = values[1] ? values[2] : null;
// Throws error if invalid // Throws error if invalid
await utils.checkImageFormatValidity(attachmentUrl ?? imgUrl).catch((e) => { throw e }); await utils.checkImageFormatValidity(attachmentUrl ?? imgUrl).catch((e) => { throw e });
return await mh.updateMemberField(authorId, memberName, "propic", attachmentUrl ?? imgUrl, attachmentExpiry).catch((e) => { throw e }); return await mh.updateMemberField(authorId, memberName, "propic", imgUrl, attachmentExpiry).catch((e) => { throw e });
} }
/** /**
@@ -326,11 +386,10 @@ mh.setExpirationWarning = function (expirationString) {
/** /**
* Gets the details for a member. * Gets the details for a member.
* *
* @async
* @param {model} member - The member object * @param {model} member - The member object
* @returns {Promise<EmbedBuilder>} The member's info. * @returns {EmbedBuilder} The member's info.
*/ */
mh.getMemberInfo = async function (member) { mh.getMemberInfo = function (member) {
return new EmbedBuilder() return new EmbedBuilder()
.setTitle(member.name) .setTitle(member.name)
.setDescription(`Details for ${member.name}`) .setDescription(`Details for ${member.name}`)

View File

@@ -50,51 +50,55 @@ describe('MemberHelper', () => {
describe('parseMemberCommand', () => { describe('parseMemberCommand', () => {
beforeEach(() => { beforeEach(() => {
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(mockMember); // jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(mockMember);
jest.spyOn(memberHelper, 'getMemberInfo').mockResolvedValue("member info"); // jest.spyOn(memberHelper, 'getMemberInfo').mockResolvedValue("member info");
jest.spyOn(memberHelper, 'addNewMember').mockResolvedValue("new member"); // jest.spyOn(memberHelper, 'addNewMember').mockResolvedValue("new member");
jest.spyOn(memberHelper, 'removeMember').mockResolvedValue("remove member"); // jest.spyOn(memberHelper, 'removeMember').mockResolvedValue("remove member");
jest.spyOn(memberHelper, 'getAllMembersInfo').mockResolvedValue("all member info"); // jest.spyOn(memberHelper, 'getAllMembersInfo').mockResolvedValue("all member info");
jest.spyOn(memberHelper, 'updateName').mockResolvedValue("update name"); // jest.spyOn(memberHelper, 'updateName').mockResolvedValue("update name");
jest.spyOn(memberHelper, 'updateDisplayName').mockResolvedValue("update display name"); // jest.spyOn(memberHelper, 'updateDisplayName').mockResolvedValue("update display name");
jest.spyOn(memberHelper, 'updateProxy').mockResolvedValue("update proxy"); // jest.spyOn(memberHelper, 'updateProxy').mockResolvedValue("update proxy");
jest.spyOn(memberHelper, 'updatePropic').mockResolvedValue("update propic"); // jest.spyOn(memberHelper, 'updatePropic').mockResolvedValue("update propic");
jest.spyOn(memberHelper, 'getMemberCommandInfo').mockResolvedValue("member command info"); jest.spyOn(memberHelper, 'getMemberCommandInfo').mockResolvedValue("member command info");
jest.spyOn(memberHelper, 'memberArgumentHandler').mockResolvedValue("handled argument");
jest.spyOn(memberHelper, 'memberCommandHandler').mockResolvedValue("called command");
jest.spyOn(memberHelper, 'sendCurrentValue').mockResolvedValue("current value");
jest.spyOn(memberHelper, 'sendHelpEnum').mockResolvedValue("help enum")
}); });
test.each([ // test.each([
[['new', 'somePerson'], attachmentUrl], // [['new', 'somePerson'], attachmentUrl],
[['new', 'somePerson'], null], // [['new', 'somePerson'], null],
])('%s calls addNewMember and returns correct values', async(args, attachmentUrl) => { // ])('%s calls addNewMember and returns correct values', async(args, attachmentUrl) => {
// Act // // Act
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl).then((result) => { // return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl).then((result) => {
// Assert // // Assert
expect(result).toEqual("new member"); // expect(result).toEqual("new member");
expect(memberHelper.addNewMember).toHaveBeenCalledTimes(1); // expect(memberHelper.addNewMember).toHaveBeenCalledTimes(1);
expect(memberHelper.addNewMember).toHaveBeenCalledWith(authorId, args, attachmentUrl); // expect(memberHelper.addNewMember).toHaveBeenCalledWith(authorId, args, attachmentUrl);
}); // });
}) // })
//
test('["remove", "somePerson"] calls removeMember with authorId and "somePerson" and returns expected result', async() => { // test('["remove", "somePerson"] calls removeMember with authorId and "somePerson" and returns expected result', async() => {
// Act // // Act
return memberHelper.parseMemberCommand(authorId, authorFull, ["remove", "somePerson"]).then((result) => { // return memberHelper.parseMemberCommand(authorId, authorFull, ["remove", "somePerson"]).then((result) => {
// Assert // // Assert
expect(result).toEqual("remove member"); // expect(result).toEqual("remove member");
expect(memberHelper.removeMember).toHaveBeenCalledTimes(1); // expect(memberHelper.removeMember).toHaveBeenCalledTimes(1);
expect(memberHelper.removeMember).toHaveBeenCalledWith(authorId, "somePerson"); // expect(memberHelper.removeMember).toHaveBeenCalledWith(authorId, "somePerson");
}); // });
}); // });
//
test('["list"] calls getAllMembersInfo and returns expected result', async () => { // test('["list"] calls getAllMembersInfo and returns expected result', async () => {
// Act // // Act
return memberHelper.parseMemberCommand(authorId, authorFull, ["list"]).then((result) => { // return memberHelper.parseMemberCommand(authorId, authorFull, ["list"]).then((result) => {
// Assert // // Assert
expect(result).toEqual("all member info"); // expect(result).toEqual("all member info");
expect(memberHelper.getAllMembersInfo).toHaveBeenCalledTimes(1); // expect(memberHelper.getAllMembersInfo).toHaveBeenCalledTimes(1);
expect(memberHelper.getAllMembersInfo).toHaveBeenCalledWith(authorId, authorFull); // expect(memberHelper.getAllMembersInfo).toHaveBeenCalledWith(authorId, authorFull);
}); // });
}); // });
//
test.each([ test.each([
[['--help']], [['--help']],
[['']], [['']],
@@ -110,117 +114,172 @@ describe('MemberHelper', () => {
}); });
test.each([ test.each([
[['somePerson', 'name', 'newPerson'], "updateName", "update name"], [[mockMember.name, '--help'], null, true],
[['somePerson', 'displayname', 'Some Person'], "updateDisplayName", "update display name"], [['new', '--help'], 'new', true],
[['somePerson', 'proxy', '--text'], "updateProxy", "update proxy"], [['remove', '--help'], 'remove', true],
])('%s calls %s returns expected result %s', async (args, method, expectedResult) => { [['name', '--help'], 'name', true],
[['list', '--help'], 'list', true],
[['displayname', '--help'], 'displayname', true],
[['proxy', '--help'], 'proxy', true],
[['propic', '--help'], 'propic', true],
[['new'], 'new', true],
[['remove'], 'remove', true],
[['name'], 'name', true],
[['list'], 'list', true],
[['displayname'], 'displayname', true],
[['proxy'], 'proxy', true],
[['propic'], 'propic', true],
[[mockMember.name, 'remove'], 'remove', false],
[[mockMember.name, 'remove', 'test'], 'remove', false],
[[mockMember.name, 'new'], 'new', false],
[[mockMember.name, 'new', 'test'], 'new', false],
[[mockMember.name, 'new', mockMember.displayname], 'new', false],
[[mockMember.name, 'new', mockMember.proxy], 'new', false],
[[mockMember.name, 'new', mockMember.propic], 'new', false],
[[mockMember.name, 'new', null, mockMember.propic], 'new', false],
[[mockMember.name, 'new', null, mockMember.propic, attachmentExpiry], 'new', false],
[[mockMember.name, 'name', mockMember.name], 'name', false],
[[mockMember.name, 'displayname', mockMember.displayname], 'displayname', false],
[[mockMember.name, 'proxy', mockMember.proxy], 'proxy', false],
[[mockMember.name, 'propic', mockMember.propic], 'propic', false],
[[mockMember.name, 'propic', null, mockMember.propic], 'propic', false],
[[mockMember.name, 'propic', null, mockMember.propic, attachmentExpiry], 'propic', false],
[['remove', mockMember.name], 'remove'],
[['remove', mockMember.name, 'test'], 'remove'],
[['new', mockMember.name], 'new'],
[['new', mockMember.name, mockMember.displayname], 'new'],
[['new', mockMember.name, mockMember.displayname, mockMember.proxy], 'new'],
[['new', mockMember.name, mockMember.displayname, mockMember.proxy, mockMember.propic], 'new'],
[['new', mockMember.name, null, mockMember.displayname, mockMember.proxy, mockMember.propic, attachmentExpiry], 'new'],
[['new', mockMember.name, null, mockMember.displayname, mockMember.proxy, mockMember.propic, attachmentExpiry], 'new'],
[['name', mockMember.name, mockMember.name], 'name'],
[['displayname', mockMember.name, mockMember.name, mockMember.displayname], 'displayname'],
[['proxy', mockMember.name, mockMember.name, mockMember.displayname, mockMember.proxy], 'proxy'],
[['propic', mockMember.name, mockMember.name, mockMember.displayname, mockMember.proxy, mockMember.propic], 'propic'],
[['propic', mockMember.name, null, mockMember.name, mockMember.displayname, mockMember.proxy, mockMember.propic, attachmentExpiry], 'propic'],
[['propic', mockMember.name, null, mockMember.name, mockMember.displayname, mockMember.proxy, mockMember.propic, attachmentExpiry], 'propic'],
])('%s calls memberCommandHandler with correct values', async (args, command, isHelp) => {
// Act // Act
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => { return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result, command) => {
// Assert // Assert
expect(result).toEqual(expectedResult); expect(result).toEqual("handled argument");
expect(memberHelper[method]).toHaveBeenCalledTimes(1); expect(memberHelper.memberArgumentHandler).toHaveBeenCalledTimes(1);
expect(memberHelper[method]).toHaveBeenCalledWith(authorId, args[0], args[2]); expect(memberHelper.memberArgumentHandler).toHaveBeenCalledWith(authorId, authorFull, isHelp, command, mockMember.name, args.slice(2));
});
});
test.each([
[["somePerson", "propic", attachmentUrl], null, null],
[["somePerson", "propic", null], 'ono.png', attachmentExpiry],
])('%s calls updatePropic and returns expected values', async (args, attachmentUrl, attachmentExpiration) => {
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl, attachmentExpiration).then((result) => {
// Assert
expect(result).toEqual("update propic");
expect(memberHelper['updatePropic']).toHaveBeenCalledTimes(1);
expect(memberHelper['updatePropic']).toHaveBeenCalledWith(authorId, args[0], args[2], attachmentUrl, attachmentExpiration)
}); });
}) })
//
test('any non-command returns getMemberInfo', async() => { // test.each([
// Act // [['somePerson', 'name', 'newPerson'], "updateName", "update name"],
return memberHelper.parseMemberCommand(authorId, authorFull, ['somePerson']).then(() => { // [['somePerson', 'displayname', 'Some Person'], "updateDisplayName", "update display name"],
// Assert // [['somePerson', 'proxy', '--text'], "updateProxy", "update proxy"],
expect(memberHelper['getMemberInfo']).toHaveBeenCalledTimes(1); // ])('%s calls %s returns expected result %s', async (args, method, expectedResult) => {
expect(memberHelper['getMemberInfo']).toHaveBeenCalledWith(mockMember); // // Act
}) // return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
}) // // Assert
// expect(result).toEqual(expectedResult);
test.each([ // expect(memberHelper[method]).toHaveBeenCalledTimes(1);
[['new'], "addNewMember", enums.help.NEW], // expect(memberHelper[method]).toHaveBeenCalledWith(authorId, args[0], args[2]);
[['new', '--help'], "addNewMember", enums.help.NEW], // });
[['remove'], "removeMember", enums.help.REMOVE], // });
[['remove', '--help'], "removeMember", enums.help.REMOVE], //
[['name'], "updateName", enums.help.NAME], // test.each([
[['name', '--help'], "updateName", enums.help.NAME], // [["somePerson", "propic", attachmentUrl], null, null],
[['somePerson', 'name'], "updateName", mockMember.name], // [["somePerson", "propic", null], 'ono.png', attachmentExpiry],
[['displayname'], "updateDisplayName", enums.help.DISPLAY_NAME], // ])('%s calls updatePropic and returns expected values', async (args, attachmentUrl, attachmentExpiration) => {
[['displayname', '--help'], "updateDisplayName", enums.help.DISPLAY_NAME], // // Act
[['somePerson', 'displayname'], "updateDisplayName", mockMember.displayname], // return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl, attachmentExpiration).then((result) => {
[['proxy'], "updateProxy", enums.help.PROXY], // // Assert
[['proxy', '--help'], "updateProxy", enums.help.PROXY], // expect(result).toEqual("update propic");
[['somePerson', 'proxy'], "updateProxy", mockMember.proxy], // expect(memberHelper['updatePropic']).toHaveBeenCalledTimes(1);
[['propic'], "updatePropic", enums.help.PROPIC], // expect(memberHelper['updatePropic']).toHaveBeenCalledWith(authorId, args[0], args[2], attachmentUrl, attachmentExpiration)
[['propic', '--help'], "updatePropic", enums.help.PROPIC], // });
[['somePerson', 'propic'], "updatePropic", mockMember.propic], // })
[['list', '--help'], "getAllMembersInfo", enums.help.LIST], //
])('%s shall not call %s and returns correct string', async (args, method, expectedResult) => { // test('any non-command returns getMemberInfo', async() => {
// Act // // Act
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => { // return memberHelper.parseMemberCommand(authorId, authorFull, ['somePerson']).then(() => {
// Assert // // Assert
expect(result).toEqual(expectedResult); // expect(memberHelper['getMemberInfo']).toHaveBeenCalledTimes(1);
expect(memberHelper[method]).not.toHaveBeenCalled(); // expect(memberHelper['getMemberInfo']).toHaveBeenCalledWith(mockMember);
}); // })
}); // })
//
test.each([ // test.each([
[['somePerson', 'displayname'], "updateDisplayName", "Display name"], // [['new'], "addNewMember", enums.help.NEW],
[['somePerson', 'proxy'], "updateProxy", "Proxy"], // [['new', '--help'], "addNewMember", enums.help.NEW],
[['somePerson', 'propic'], "updatePropic", "Profile picture"], // [['remove'], "removeMember", enums.help.REMOVE],
])('if value not set, %s shall not call %s and returns value error', async (args, method, expectedResult) => { // [['remove', '--help'], "removeMember", enums.help.REMOVE],
// Arrange // [['name'], "updateName", enums.help.NAME],
const mockEmptyMember = { // [['name', '--help'], "updateName", enums.help.NAME],
name: "somePerson", // [['somePerson', 'name'], "updateName", mockMember.name],
displayname: null, // [['displayname'], "updateDisplayName", enums.help.DISPLAY_NAME],
proxy: null, // [['displayname', '--help'], "updateDisplayName", enums.help.DISPLAY_NAME],
propic: null, // [['somePerson', 'displayname'], "updateDisplayName", mockMember.displayname],
} // [['proxy'], "updateProxy", enums.help.PROXY],
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(mockEmptyMember); // [['proxy', '--help'], "updateProxy", enums.help.PROXY],
// Act // [['somePerson', 'proxy'], "updateProxy", mockMember.proxy],
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => { // [['propic'], "updatePropic", enums.help.PROPIC],
// Assert // [['propic', '--help'], "updatePropic", enums.help.PROPIC],
expect(result).toEqual(`${expectedResult} ${enums.err.NO_VALUE}`); // [['somePerson', 'propic'], "updatePropic", mockMember.propic],
expect(memberHelper[method]).not.toHaveBeenCalled(); // [['list', '--help'], "getAllMembersInfo", enums.help.LIST],
}); // ])('%s shall not call %s and returns correct string', async (args, method, expectedResult) => {
}); // // Act
// return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
test('["new", "someNewPerson"] shall call addNewMember and return correct results', async () => { // // Assert
// Act // expect(result).toEqual(expectedResult);
return memberHelper.parseMemberCommand(authorId, authorFull, ['new', 'someNewPerson']).then((result) => { // expect(memberHelper[method]).not.toHaveBeenCalled();
// Assert // });
expect(result).toEqual("new member"); // });
expect(memberHelper.getMemberByName).not.toHaveBeenCalled(); //
}); // test.each([
}); // [['somePerson', 'displayname'], "updateDisplayName", "Display name"],
// [['somePerson', 'proxy'], "updateProxy", "Proxy"],
test('["new", "--help"] shall return help enum', async () => { // [['somePerson', 'propic'], "updatePropic", "Profile picture"],
// Act // ])('if value not set, %s shall not call %s and returns value error', async (args, method, expectedResult) => {
return memberHelper.parseMemberCommand(authorId, authorFull, ['new', '--help']).then((result) => { // // Arrange
// Assert // const mockEmptyMember = {
expect(result).toEqual(enums.help.NEW); // name: "somePerson",
expect(memberHelper.addNewMember).not.toHaveBeenCalled(); // displayname: null,
expect(memberHelper.getMemberByName).not.toHaveBeenCalled(); // proxy: null,
}); // propic: null,
}); // }
// jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(mockEmptyMember);
test('["new"] shall return help enum', async () => { // // Act
// Act // return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
return memberHelper.parseMemberCommand(authorId, authorFull, ['new']).then((result) => { // // Assert
// Assert // expect(result).toEqual(`${expectedResult} ${enums.err.NO_VALUE}`);
expect(result).toEqual(enums.help.NEW); // expect(memberHelper[method]).not.toHaveBeenCalled();
expect(memberHelper.addNewMember).not.toHaveBeenCalled(); // });
expect(memberHelper.getMemberByName).not.toHaveBeenCalled(); // });
}); //
}); // test('["new", "someNewPerson"] shall call addNewMember and return correct results', async () => {
// // Act
// return memberHelper.parseMemberCommand(authorId, authorFull, ['new', 'someNewPerson']).then((result) => {
// // Assert
// expect(result).toEqual("new member");
// expect(memberHelper.getMemberByName).not.toHaveBeenCalled();
// });
// });
//
// test('["new", "--help"] shall return help enum', async () => {
// // Act
// return memberHelper.parseMemberCommand(authorId, authorFull, ['new', '--help']).then((result) => {
// // Assert
// expect(result).toEqual(enums.help.NEW);
// expect(memberHelper.addNewMember).not.toHaveBeenCalled();
// expect(memberHelper.getMemberByName).not.toHaveBeenCalled();
// });
// });
//
// test('["new"] shall return help enum', async () => {
// // Act
// return memberHelper.parseMemberCommand(authorId, authorFull, ['new']).then((result) => {
// // Assert
// expect(result).toEqual(enums.help.NEW);
// expect(memberHelper.addNewMember).not.toHaveBeenCalled();
// expect(memberHelper.getMemberByName).not.toHaveBeenCalled();
// });
// });
}) })
describe('addNewMember', () => { describe('addNewMember', () => {