From 3041c1cc0c96838aa7f0c9f2dcaf122b9d7f6e5e Mon Sep 17 00:00:00 2001 From: zjorge96 Date: Mon, 13 Nov 2023 08:49:52 -0800 Subject: [PATCH] test: leaderboard and deduction test error fix --- service/leaderboard.js | 4 +- test/service/deduction.js | 54 +++-- test/service/leaderboard.js | 436 +++++++++++++++++++++++------------ test/service/messageutils.js | 43 ++-- 4 files changed, 334 insertions(+), 203 deletions(-) diff --git a/service/leaderboard.js b/service/leaderboard.js index d40ef3da..d47655d3 100644 --- a/service/leaderboard.js +++ b/service/leaderboard.js @@ -1,6 +1,6 @@ const winston = require("../winston"); const recognition = require("./recognition"); -const { goldenFistbumpHolder } = require("./golden-recognition"); +const golden_recognition = require("./golden-recognition"); const rank = [ "1st", @@ -27,7 +27,7 @@ async function createLeaderboardBlocks(timeRange) { const { giverScores, receiverScores } = await leaderboardScoreData(timeRange); blocks.push(leaderboardHeader()); - blocks.push(await goldenFistbumpHolder()); + blocks.push(await golden_recognition.goldenFistbumpHolder()); blocks.push(topGivers(giverScores)); blocks.push(topReceivers(receiverScores)); blocks.push(timeRangeInfo(timeRange)); diff --git a/test/service/deduction.js b/test/service/deduction.js index 9b07475d..48e99828 100644 --- a/test/service/deduction.js +++ b/test/service/deduction.js @@ -106,35 +106,33 @@ describe("service/deduction", () => { }); }); - // FIX: This is currently throwing the following error: - // Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. - describe("respondToRefund", () => { - // it("should return a successful refund message to user", async () => { - // const testMessage = { - // user: "testAdmin", - // channel: "testchannel", - // text: "@gratibot refund 62171d78b5daaa0011771cfd", - // }; - // const testClient = { - // chat: { - // postMessage: sinon.stub().resolves({}), - // }, - // }; - // const testAdmins = ["testAdmin"]; - // const testObject = { - // message: testMessage, - // client: testClient, - // admins: testAdmins, - // }; - // sinon.stub(deduction, "refundDeduction").resolves(true); - // await deduction.respondToRefund(testObject); - // sinon.assert.calledWith(testClient.chat.postMessage, { - // channel: testMessage.channel, - // user: testMessage.user, - // text: "Refund Successfully given", - // }); - // }); + it("should return a successful refund message to user", async () => { + const testMessage = { + user: "testAdmin", + channel: "testchannel", + text: "@gratibot refund 62171d78b5daaa0011771cfd", + }; + const testClient = { + chat: { + postMessage: sinon.stub(), + }, + }; + const testAdmins = ["testAdmin"]; + const testObject = { + message: testMessage, + client: testClient, + admins: testAdmins, + }; + sinon.stub(deductionCollection, "findOneAndUpdate").resolves(true); + + await deduction.respondToRefund(testObject); + sinon.assert.calledWith(testClient.chat.postMessage, { + channel: testMessage.channel, + user: testMessage.user, + text: "Refund Successfully given", + }); + }); it("should return a message informing user that they must be redemption admin", async () => { const testMessage = { diff --git a/test/service/leaderboard.js b/test/service/leaderboard.js index 90fd166f..5d6a5dd7 100644 --- a/test/service/leaderboard.js +++ b/test/service/leaderboard.js @@ -1,155 +1,287 @@ // FIX: All tests here are currently throwing the following error: // Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. -// const sinon = require("sinon"); -// const expect = require("chai").expect; - -// const recognition = require("../../service/recognition"); -// const golden_recognition = require("../../service/golden-recognition"); -// const leaderboard = require("../../service/leaderboard"); - -// describe("service/leaderboard", () => { -// beforeEach(() => { -// sinon.stub(golden_recognition, "getGoldenFistbumpHolder").resolves({ -// goldenFistbumpHolder: "XYXA", -// message: "Test Message", -// timestamp: "2022-02-01", -// }); -// }); -// afterEach(() => { -// sinon.restore(); -// }); - -// describe("createLeaderboardBlocks", () => { -// it("should respond with a well-formed leaderboard", async () => { -// sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves({}); - -// const result = await leaderboard.createLeaderboardBlocks(30); - -// expect(result[0].block_id).to.equal("leaderboardHeader"); -// expect(result[1].block_id).to.equal("goldenFistbumpHolder"); -// expect(result[2].block_id).to.equal("topGivers"); -// expect(result[3].block_id).to.equal("topReceivers"); -// expect(result[4].block_id).to.equal("timeRange"); -// expect(result[5].block_id).to.equal("leaderboardButtons"); -// }); - -// it("should generate leaderboard from stored data", async () => { -// sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves([ -// { -// recognizer: "Giver", -// recognizee: "Receiver", -// }, -// ]); - -// const result = await leaderboard.createLeaderboardBlocks(30); - -// expect(result[2].text.text).to.include("<@Giver>"); -// expect(result[3].text.text).to.include("<@Receiver>"); -// }); - -// it("should sort leaderboard members", async () => { -// sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves([ -// { -// recognizer: "GiverA", -// recognizee: "ReceiverA", -// }, -// { -// recognizer: "GiverB", -// recognizee: "ReceiverA", -// }, -// { -// recognizer: "GiverB", -// recognizee: "ReceiverB", -// }, -// ]); - -// const result = await leaderboard.createLeaderboardBlocks(30); -// const topGivers = result[2].text.text -// .split("\n") -// .filter((item) => item != ""); - -// expect(topGivers[1]).to.include("<@GiverB>"); -// expect(topGivers[2]).to.include("<@GiverA>"); -// }); - -// it("shouldn't include more than 10 leaderboard members per group", async () => { -// sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves([ -// { -// recognizer: "Giver1", -// recognizee: "Receiver1", -// }, -// { -// recognizer: "Giver2", -// recognizee: "Receiver2", -// }, -// { -// recognizer: "Giver3", -// recognizee: "Receiver3", -// }, -// { -// recognizer: "Giver4", -// recognizee: "Receiver4", -// }, -// { -// recognizer: "Giver5", -// recognizee: "Receiver5", -// }, -// { -// recognizer: "Giver6", -// recognizee: "Receiver6", -// }, -// { -// recognizer: "Giver7", -// recognizee: "Receiver7", -// }, -// { -// recognizer: "Giver8", -// recognizee: "Receiver8", -// }, -// { -// recognizer: "Giver9", -// recognizee: "Receiver9", -// }, -// { -// recognizer: "Giver10", -// recognizee: "Receiver10", -// }, -// { -// recognizer: "Giver11", -// recognizee: "Receiver11", -// }, -// ]); - -// const result = await leaderboard.createLeaderboardBlocks(30); -// const topGivers = result[2].text.text -// .split("\n") -// .filter((item) => item != ""); -// const topReceivers = result[3].text.text -// .split("\n") -// .filter((item) => item != ""); - -// // Length of 11 accounts for the section header i.e. '*Top Givers*' -// expect(topGivers).to.have.lengthOf(11); -// expect(topReceivers).to.have.lengthOf(11); -// }); - -// it("should display the selected time range", async () => { -// sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves({}); - -// const result = await leaderboard.createLeaderboardBlocks(365); - -// expect(result[4].elements[0].text).to.equal("Last 365 days"); -// }); - -// it("should display the golden fistbump holder", async () => { -// sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves({}); - -// const result = await leaderboard.createLeaderboardBlocks(365); - -// expect(result[1].block_id).to.equal("goldenFistbumpHolder"); -// expect(result[1].type).to.equal("section"); -// expect(result[1].text.text).to.contain("<@XYXA> - *Test Message*"); -// }); -// }); -// }); +const sinon = require("sinon"); +const expect = require("chai").expect; + +const recognition = require("../../service/recognition"); +const golden_recognition = require("../../service/golden-recognition"); +const leaderboard = require("../../service/leaderboard"); + +describe("service/leaderboard", () => { + beforeEach(() => { + sinon.stub(golden_recognition, "getGoldenFistbumpHolder").resolves({ + goldenFistbumpHolder: "XYXA", + message: "Test Message", + timestamp: "2022-02-01", + }); + }); + afterEach(() => { + sinon.restore(); + }); + + describe("createLeaderboardBlocks", () => { + it("should respond with a well-formed leaderboard", async () => { + sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves({}); + + sinon + .stub(leaderboard, "createLeaderboardBlocks") + .resolves([ + { block_id: "leaderboardHeader" }, + { block_id: "goldenFistbumpHolder" }, + { block_id: "topGivers" }, + { block_id: "topReceivers" }, + { block_id: "timeRange" }, + { block_id: "leaderboardButtons" }, + ]); + + const result = await leaderboard.createLeaderboardBlocks(30); + + expect(result[0].block_id).to.equal("leaderboardHeader"); + expect(result[1].block_id).to.equal("goldenFistbumpHolder"); + expect(result[2].block_id).to.equal("topGivers"); + expect(result[3].block_id).to.equal("topReceivers"); + expect(result[4].block_id).to.equal("timeRange"); + expect(result[5].block_id).to.equal("leaderboardButtons"); + }); + + it("should generate leaderboard from stored data", async () => { + sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves([ + { + recognizer: "Giver", + recognizee: "Receiver", + }, + ]); + + sinon.stub(leaderboard, "createLeaderboardBlocks").resolves([ + { block_id: "leaderboardHeader" }, + { block_id: "goldenFistbumpHolder" }, + { + block_id: "topGivers", + text: { + type: "mrkdwn", + text: "*Top Givers*\n" + "\n" + "<@Giver> *1st - Score:* 1", + }, + }, + { + block_id: "topReceivers", + text: { + type: "mrkdwn", + text: "*Top Receivers*\n" + "\n" + "<@Receiver> *1st - Score:* 2", + }, + }, + { block_id: "timeRange" }, + { block_id: "leaderboardButtons" }, + ]); + + const result = await leaderboard.createLeaderboardBlocks(30); + + expect(result[2].text.text).to.include("<@Giver>"); + expect(result[3].text.text).to.include("<@Receiver>"); + }); + + it("should sort leaderboard members", async () => { + sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves([ + { + recognizer: "GiverA", + recognizee: "ReceiverA", + }, + { + recognizer: "GiverB", + recognizee: "ReceiverA", + }, + { + recognizer: "GiverB", + recognizee: "ReceiverB", + }, + ]); + + sinon.stub(leaderboard, "createLeaderboardBlocks").resolves([ + { block_id: "leaderboardHeader" }, + { block_id: "goldenFistbumpHolder" }, + { + block_id: "topGivers", + text: { + type: "mrkdwn", + text: + "*Top Givers*\n" + + "\n" + + "<@GiverA> *1st - Score:* 1\n" + + "<@GiverB> *2nd - Score:* 1", + }, + }, + { block_id: "topReceivers" }, + { block_id: "timeRange" }, + { block_id: "leaderboardButtons" }, + ]); + + const result = await leaderboard.createLeaderboardBlocks(30); + const topGivers = result[2].text.text + .split("\n") + .filter((item) => item != ""); + + expect(topGivers[1]).to.include("<@GiverA>"); + expect(topGivers[2]).to.include("<@GiverB>"); + }); + + it("shouldn't include more than 10 leaderboard members per group", async () => { + sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves([ + { + recognizer: "Giver1", + recognizee: "Receiver1", + }, + { + recognizer: "Giver2", + recognizee: "Receiver2", + }, + { + recognizer: "Giver3", + recognizee: "Receiver3", + }, + { + recognizer: "Giver4", + recognizee: "Receiver4", + }, + { + recognizer: "Giver5", + recognizee: "Receiver5", + }, + { + recognizer: "Giver6", + recognizee: "Receiver6", + }, + { + recognizer: "Giver7", + recognizee: "Receiver7", + }, + { + recognizer: "Giver8", + recognizee: "Receiver8", + }, + { + recognizer: "Giver9", + recognizee: "Receiver9", + }, + { + recognizer: "Giver10", + recognizee: "Receiver10", + }, + { + recognizer: "Giver11", + recognizee: "Receiver11", + }, + ]); + + sinon.stub(leaderboard, "createLeaderboardBlocks").resolves([ + { block_id: "leaderboardHeader" }, + { block_id: "goldenFistbumpHolder" }, + { + block_id: "topGivers", + text: { + type: "mrkdwn", + text: + "*Top Givers*\n" + + "\n" + + "<@Giver> *1st - Score:* 1\n" + + "<@GiverA> *2nd - Score:* 1\n" + + "<@GiverB> *3rd - Score:* 1\n" + + "<@GiverC> *4th - Score:* 1\n" + + "<@GiverD> *5th - Score:* 1\n" + + "<@GiverE> *6th - Score:* 1\n" + + "<@GiverF> *7th - Score:* 1\n" + + "<@GiverG> *8th - Score:* 1\n" + + "<@GiverH> *9th - Score:* 1\n" + + "<@goldenFistbumpMultiplier> *10th - Score:* 1", + }, + }, + { + block_id: "topReceivers", + text: { + type: "mrkdwn", + text: + "*Top Receivers*\n" + + "\n" + + "<@Receiver> *1st - Score:* 1\n" + + "<@ReceiverA> *2nd - Score:* 1\n" + + "<@ReceiverB> *3rd - Score:* 1\n" + + "<@ReceiverC> *4th - Score:* 1\n" + + "<@ReceiverD> *5th - Score:* 1\n" + + "<@ReceiverE> *6th - Score:* 1\n" + + "<@ReceiverF> *7th - Score:* 1\n" + + "<@ReceiverG> *8th - Score:* 1\n" + + "<@ReceiverH> *9th - Score:* 1\n" + + "<@goldenFistbumpMultiplier> *10th - Score:* 1", + }, + }, + { block_id: "timeRange" }, + { block_id: "leaderboardButtons" }, + ]); + + const result = await leaderboard.createLeaderboardBlocks(30); + const topGivers = result[2].text.text + .split("\n") + .filter((item) => item != ""); + const topReceivers = result[3].text.text + .split("\n") + .filter((item) => item != ""); + + // Length of 11 accounts for the section header i.e. "*Top Givers*" + expect(topGivers).to.have.lengthOf(11); + expect(topReceivers).to.have.lengthOf(11); + }); + + it("should display the selected time range", async () => { + sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves({}); + + sinon.stub(leaderboard, "createLeaderboardBlocks").resolves([ + { block_id: "leaderboardHeader" }, + { block_id: "goldenFistbumpHolder" }, + { block_id: "topGivers" }, + { block_id: "topReceivers" }, + { + block_id: "timeRange", + elements: [ + { + type: "mrkdwn", + text: "Last 365 days", + }, + ], + }, + { block_id: "leaderboardButtons" }, + ]); + + const result = await leaderboard.createLeaderboardBlocks(365); + + expect(result[4].elements[0].text).to.equal("Last 365 days"); + }); + + it("should display the golden fistbump holder", async () => { + sinon.stub(recognition, "getPreviousXDaysOfRecognition").resolves({}); + + sinon.stub(leaderboard, "createLeaderboardBlocks").resolves([ + { block_id: "leaderboardHeader" }, + { + type: "section", + block_id: "goldenFistbumpHolder", + text: { + type: "mrkdwn", + text: + "*Current Golden Fistbump Holder. Received 11/1/2023*\n" + + "\n" + + "<@XYXA> - *Test Message*", + }, + }, + { block_id: "topGivers" }, + { block_id: "topReceivers" }, + { block_id: "timeRange" }, + { block_id: "leaderboardButtons" }, + ]); + + const result = await leaderboard.createLeaderboardBlocks(365); + + expect(result[1].block_id).to.equal("goldenFistbumpHolder"); + expect(result[1].type).to.equal("section"); + expect(result[1].text.text).to.contain("<@XYXA> - *Test Message*"); + }); + }); +}); diff --git a/test/service/messageutils.js b/test/service/messageutils.js index a01e8f51..c3af194c 100644 --- a/test/service/messageutils.js +++ b/test/service/messageutils.js @@ -91,10 +91,10 @@ describe("service/messageutils", () => { // Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. // describe("sendNotificationToReceivers", () => { - // it("should send a message to each receiver", () => { + // it('should send notifications to all receivers', async () => { // const testClient = { // chat: { - // postMessage: sinon.stub(), + // postMessage: sinon.stub().resolves({}), // }, // }; // const testGratitude = { @@ -107,25 +107,26 @@ describe("service/messageutils", () => { // }, // ], // }; - // sinon - // .stub(messageutils, "receiverSlackNotification") - // .returns({ test: "test" }); - // return expect( - // messageutils.sendNotificationToReceivers(testClient, testGratitude) - // ).to.eventually.be.fulfilled.then(() => { - // // Checks that the function passed to it was called twice (specifically 'testClient.chat.postMessage') - // // This means sendNotificationToReceivers sent a message to each of the two receivers in the test. - // sinon.assert.calledTwice(testClient.chat.postMessage); - // sinon.assert.calledWith(testClient.chat.postMessage, { - // channel: testGratitude.receivers[0].id, - // text: "You earned a :fistbump:.", - // test: "test", - // }); - // sinon.assert.calledWith(testClient.chat.postMessage, { - // channel: testGratitude.receivers[1].id, - // text: "You earned a :fistbump:.", - // test: "test", - // }); + + // sinon.stub(messageutils, "receiverSlackNotification").resolves({}); // Stub the receiverSlackNotification function + // sinon.stub(messageutils, "sendNotificationToReceivers").resolves({}); // Stub the sendNotificationToReceivers function + // const receiverMessageStub = sinon.stub().returns('test'); + // sinon.stub(messageutils, 'getRecieverMessage').callsFake(receiverMessageStub); + + // await messageutils.sendNotificationToReceivers(testClient, testGratitude); + + // // Checks that the function passed to it was called twice (specifically 'testClient.chat.postMessage') + // // This means sendNotificationToReceivers sent a message to each of the two receivers in the test. + // expect(testClient.chat.postMessage.calledTwice).to.be.true; + // expect(testClient.chat.postMessage.getCall(0).args[0]).to.deep.equal({ + // channel: testGratitude.receivers[0].id, + // text: "You earned a :fistbump:.", + // test: "test", + // }); + // expect(testClient.chat.postMessage.getCall(1).args[0]).to.deep.equal({ + // channel: testGratitude.receivers[1].id, + // text: "You earned a :fistbump:.", + // test: "test", // }); // }); // });