Skip to content

Commit

Permalink
Merge pull request #551 from 18F/mgwalker/support-random-user-icons
Browse files Browse the repository at this point in the history
Random default emoji!
  • Loading branch information
mgwalker authored Sep 25, 2024
2 parents 3d0b882 + e00bf09 commit 8539c12
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/scripts/random-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,18 @@ const responseFrom =
) ?? [false, false, false, false];

const message = { thread_ts: thread, unfurl_links: false };

if (defaultEmoji) {
message.icon_emoji = defaultEmoji;
// The default emoji defined by the config may be a single emoji or a list.
// If it's a list, pick one at random.
if (Array.isArray(defaultEmoji)) {
if (defaultEmoji.length > 0) {
message.icon_emoji =
defaultEmoji[Math.floor(Math.random() * defaultEmoji.length)];
}
} else {
message.icon_emoji = defaultEmoji;
}
}
if (botName) {
message.username = botName;
Expand Down
61 changes: 52 additions & 9 deletions src/scripts/random-responses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,62 @@ describe("random responder", () => {
});

describe("with default emoji set", () => {
const baseExpectation = { icon_emoji: ":default-emoji:" };
describe("as a single emoji", () => {
const baseExpectation = { icon_emoji: ":default-emoji:" };

responsePermutations.forEach(({ testName, responseList, expected }) => {
it(testName, async () => {
config.defaultEmoji = ":default-emoji:";
random.mockReturnValue(0);
responsePermutations.forEach(({ testName, responseList, expected }) => {
it(testName, async () => {
config.defaultEmoji = ":default-emoji:";
random.mockReturnValue(0);

await script.responseFrom({ ...config, responseList })(message);

expect(random).toHaveBeenCalled();
expect(message.say).toHaveBeenCalledWith({
...baseExpectation,
...expected,
});
});
});
});

describe("as a list of random emoji", () => {
afterAll(() => {
config.defaultEmoji = null;
});

it("and the list of emoji is empty", async () => {
config.defaultEmoji = [];
random
.mockReturnValueOnce(3 / 5)
.mockReturnValueOnce(0)
.mockReturnValueOnce(0);

const { responseList, expected } = responsePermutations[0];
await script.responseFrom({ ...config, responseList })(message);

expect(random).toHaveBeenCalled();
expect(message.say).toHaveBeenCalledWith({
...baseExpectation,
...expected,
expect(message.say).toHaveBeenCalledWith(expected);
});

responsePermutations.forEach(({ testName, responseList, expected }) => {
it(testName, async () => {
config.defaultEmoji = [
":default-emoji-1:",
":default-emoji-2:",
":default-emoji-3:",
":default-emoji-4:",
":default-emoji-5:",
];
random
.mockReturnValueOnce(3 / 5)
.mockReturnValueOnce(0)
.mockReturnValueOnce(0);
await script.responseFrom({ ...config, responseList })(message);

expect(message.say).toHaveBeenCalledWith({
icon_emoji: config.defaultEmoji[3],
...expected,
});
});
});
});
Expand Down

0 comments on commit 8539c12

Please sign in to comment.