Skip to content

Commit

Permalink
work in progress for #464
Browse files Browse the repository at this point in the history
  • Loading branch information
jcolson committed Nov 29, 2021
1 parent 8aea17e commit 3344936
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
52 changes: 51 additions & 1 deletion __test__/handlers/poll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('handlePoll default choices no guildconfig channel', async () => {
});
return aSentMessage;
});
let msgParms = [{}];
let msgParms = [{ name: 'poll_question', value: 'The Question - we need to make this longer than 256 chars. - we need to make this longer than 256 chars. - we need to make this longer than 256 chars. - we need to make this longer than 256 chars. - we need to make this longer than 256 chars. - we need to make this longer than 256 chars.' }];
let guildConfig = {};
let sendDirectOrFallbackToChannel = jest.spyOn(utils, 'sendDirectOrFallbackToChannel').mockImplementation((fields, msg, user, skipDM) => {
// console.debug(fields, msg, user, skipDM);
Expand Down Expand Up @@ -65,3 +65,53 @@ test('handleReactionAdd', async () => {

await testables.handleReactionAdd(reaction, user, guildConfig);
});

test('handleReactionAdd trash', async () => {
let reaction = {
message: {
embeds: [
{
fields: [{ name: testables.POLLSTER_AUTHOR_FIELD_NAME, value: '<@' + TEST_USER_ID + '>' }],
setTitle: ((titleString) => { return }),
addFields: ((fieldObject) => { return })
}
],
guild: {
members: {
resolve: ((userId) => { console.debug(userId) })
}
},
reactions: {
cache: {
values: (() => { return [] })
}
},
delete: (() => { return })
},
emoji: { name: utils.EMOJIS.TRASH },
users: {
remove: ((userId) => { return })
}
};
let sendDirectOrFallbackToChannelEmbeds = jest.spyOn(utils, 'sendDirectOrFallbackToChannelEmbeds').mockImplementation((embeds, msg, user) => {
// console.debug(fields, msg, user, skipDM);
});
let user = { id: TEST_USER_ID };
let guildConfig = {};

await testables.handleReactionAdd(reaction, user, guildConfig);

expect(sendDirectOrFallbackToChannelEmbeds).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
'fields':
expect.arrayContaining([
expect.objectContaining({
'name': `Author`,
'value': expect.stringMatching(/^<@1234567890>/)
})
])
})
]), reaction.message, user
);
});
10 changes: 7 additions & 3 deletions handlers/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ async function handlePoll(msg, msgParms, guildConfig) {
}

function embedForPoll(msg, thePoll, allowMultiple) {
let title = `${thePoll.question}${allowMultiple ? POLLSTER_MULTIPLE_ALLOWED : ''}`;
let title = `${thePoll.question ? thePoll.question : ''}${allowMultiple ? POLLSTER_MULTIPLE_ALLOWED : ''}`;
// console.debug(`embedForPoll: ${title} // ${thePoll.question} // ${allowMultiple}`);
let pollQuestion;
if (title.length > 255) {
if (allowMultiple) {
title = `Poll${allowMultiple ? POLLSTER_MULTIPLE_ALLOWED : ''}`;
} else {
title = undefined;
title = `Poll`;
}
pollQuestion = thePoll.question.substring(0, 1023);
}
Expand Down Expand Up @@ -77,9 +78,11 @@ function parseMessageForPoll(pollParams) {
if (pollParams.length > 11) {
throw new Error('Too many choices, please reduce to 10 or fewer');
}
// console.debug(`parseMessageForPoll:`, pollParams);
let thePoll = {};
if (pollParams.length > 0) {
thePoll.question = pollParams[0].value;
// console.debug(`parseMessageForPoll: ${thePoll.question}`)
if (pollParams.length > 1) {
thePoll.choices = pollParams.slice(1).map(entity => entity.value);
thePoll.emojis = [utils.EMOJIS.ONE, utils.EMOJIS.TWO, utils.EMOJIS.THREE,
Expand Down Expand Up @@ -168,5 +171,6 @@ exports.POLLSTER_AUTHOR = POLLSTER_AUTHOR;

exports.testables = {
handlePoll: handlePoll,
handleReactionAdd: handleReactionAdd
handleReactionAdd: handleReactionAdd,
POLLSTER_AUTHOR_FIELD_NAME: POLLSTER_AUTHOR_FIELD_NAME
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"main": "index.js",
"scripts": {
"test": "npx cross-env NODE_OPTIONS=--experimental-vm-modules jest roll.test.js --runInBand && npx cross-env NODE_OPTIONS=--experimental-vm-modules jest commands.test.js --runInBand && npx cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand --testPathIgnorePatterns roll.test.js --testPathIgnorePatterns commands.test.js",
"stest": "npx cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test:coverage": "npx cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage",
"dockertest": "docker build --target test .",
"preinstall": "git lfs update --force && pre-commit install -t pre-commit -t pre-push",
"preinstall:old": "npx npm-force-resolutions && pre-commit install -t pre-commit -t pre-push"
},
"scriptsComments": {
"test": "use jest for testing",
"stest": "use jest for testing, for a single test use '-- roll.js.test' for example",
"dockertest": "run tests inside docker container --> --progress plain",
"preinstall": "insure that pre-commit hooks are installed",
"preinstall:old": "this runs automatically on install to enforce versions of dependencies, see 'resolutions' below"
Expand Down

0 comments on commit 3344936

Please sign in to comment.