Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestReactions.logic.test.ts

170 lines6.1 KB

1import type { PullRequestReaction, PullRequestReactionContent } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 applyPendingPullRequestReactions,
6 pullRequestReactionTooltip,
7} from "./pullRequestReactions.logic";
8​
9function reaction(overrides: Partial<PullRequestReaction> = {}): PullRequestReaction {
10 return {
11 content: "heart",
12 count: 1,
13 actors: ["octocat"],
14 viewerHasReacted: false,
15 ...overrides,
16 };
18​
19describe("reaction tooltip", () => {
20 it("reads as GitHub's sentence for one, two and three names", () => {
21 expect(
22 pullRequestReactionTooltip(reaction({ content: "thumbs-up", count: 1, actors: ["Bil0000"] })),
23 ).toBe("Bil0000 reacted with thumbs up emoji");
24 expect(pullRequestReactionTooltip(reaction({ count: 2, actors: ["Bil0000", "octocat"] }))).toBe(
25 "Bil0000 and octocat reacted with heart emoji",
26 );
27 expect(
28 pullRequestReactionTooltip(
29 reaction({
30 content: "eyes",
31 count: 3,
32 actors: ["Bil0000", "octocat"],
33 viewerHasReacted: true,
34 }),
35 ),
36 ).toBe("You, Bil0000, and octocat reacted with eyes emoji");
37 });
38​
39 it("counts everyone past the third name, including the ones the host never named", () => {
40 expect(
41 pullRequestReactionTooltip(
42 reaction({
43 content: "rocket",
44 count: 15,
45 actors: ["a", "b", "c", "d"],
46 viewerHasReacted: true,
47 }),
48 ),
49 ).toBe("You, a, b, and 12 others reacted with rocket emoji");
50 expect(pullRequestReactionTooltip(reaction({ count: 2, actors: ["octocat"] }))).toBe(
51 "octocat and 1 other reacted with heart emoji",
52 );
53 expect(pullRequestReactionTooltip(reaction({ count: 4, actors: [] }))).toBe(
54 "4 people reacted with heart emoji",
55 );
56 expect(pullRequestReactionTooltip(reaction({ count: 1, actors: [] }))).toBe(
57 "1 person reacted with heart emoji",
58 );
59 });
60​
61 it("names the viewer as You, ahead of the other people who reacted", () => {
62 expect(
63 pullRequestReactionTooltip(
64 reaction({ count: 3, actors: ["Bil0000", "octocat"], viewerHasReacted: true }),
65 ),
66 ).toBe("You, Bil0000, and octocat reacted with heart emoji");
67 });
68​
69 it("names nobody as You when the viewer has not reacted", () => {
70 expect(
71 pullRequestReactionTooltip(
72 reaction({ count: 2, actors: ["Bil0000", "octocat"], viewerHasReacted: false }),
73 ),
74 ).toBe("Bil0000 and octocat reacted with heart emoji");
75 });
76​
77 it("names actors as given, and leaves off You, for a host with no room for the viewer", () => {
78 expect(
79 pullRequestReactionTooltip(
80 reaction({ count: 2, actors: ["Bil0000", "octocat"], viewerHasReacted: true }),
81 ),
82 ).toBe("Bil0000 and octocat reacted with heart emoji");
83 expect(
84 pullRequestReactionTooltip(
85 reaction({
86 count: 4,
87 actors: ["Bil0000", "octocat", "hubot", "zzz"],
88 viewerHasReacted: true,
89 }),
90 ),
91 ).toBe("Bil0000, octocat, hubot, and 1 other reacted with heart emoji");
92 });
93​
94 it("keeps naming the viewer You when the host does leave them room", () => {
95 expect(
96 pullRequestReactionTooltip(
97 reaction({ count: 2, actors: ["octocat"], viewerHasReacted: true }),
98 ),
99 ).toBe("You and octocat reacted with heart emoji");
100 });
101});
102​
103describe("reaction tooltip, with a reaction in flight", () => {
104 it("still says You after an optimistic react, even at full capacity", () => {
105 const applied = applyPendingPullRequestReactions(
106 [reaction({ count: 2, actors: ["a", "b"], viewerHasReacted: false })],
107 new Map([["heart", true] as const]),
108 );
109 expect(pullRequestReactionTooltip(applied[0]!)).toBe("You, a, and b reacted with heart emoji");
110 });
111​
112 it("drops You after an optimistic un-react, without treating the host as non-compliant", () => {
113 const applied = applyPendingPullRequestReactions(
114 [reaction({ count: 2, actors: ["octocat"], viewerHasReacted: true })],
115 new Map([["heart", false] as const]),
116 );
117 expect(pullRequestReactionTooltip(applied[0]!)).toBe("octocat reacted with heart emoji");
118 });
119});
120​
121describe("pending reactions", () => {
122 const pending = (
123 entries: ReadonlyArray<readonly [PullRequestReactionContent, boolean]>,
124 ): ReadonlyMap<PullRequestReactionContent, boolean> => new Map(entries);
125​
126 it("returns the host's list untouched while nothing is in flight", () => {
127 const reactions = [reaction()];
128 expect(applyPendingPullRequestReactions(reactions, pending([]))).toBe(reactions);
129 });
130​
131 it("adds a reaction nobody had yet, in picker order", () => {
132 const applied = applyPendingPullRequestReactions(
133 [reaction({ content: "rocket", count: 2, actors: ["a", "b"] })],
134 pending([["thumbs-up", true]]),
135 );
136 expect(applied).toEqual([
137 { content: "thumbs-up", count: 1, actors: [], viewerHasReacted: true },
138 { content: "rocket", count: 2, actors: ["a", "b"], viewerHasReacted: false },
139 ]);
140 });
141​
142 it("joins and leaves an existing reaction, and drops the pill nobody is left on", () => {
143 expect(
144 applyPendingPullRequestReactions(
145 [reaction({ count: 2, actors: ["a", "b"] })],
146 pending([["heart", true]]),
147 ),
148 ).toEqual([{ content: "heart", count: 3, actors: ["a", "b"], viewerHasReacted: true }]);
149 expect(
150 applyPendingPullRequestReactions(
151 [reaction({ count: 2, actors: ["a", "b"], viewerHasReacted: true })],
152 pending([["heart", false]]),
153 ),
154 ).toEqual([{ content: "heart", count: 1, actors: ["a", "b"], viewerHasReacted: false }]);
155 expect(
156 applyPendingPullRequestReactions(
157 [reaction({ count: 1, viewerHasReacted: true })],
158 pending([["heart", false]]),
159 ),
160 ).toEqual([]);
161 });
162​
163 it("ignores a pending state the host has already caught up with", () => {
164 const reactions = [reaction({ count: 3, viewerHasReacted: true })];
165 expect(applyPendingPullRequestReactions(reactions, pending([["heart", true]]))).toEqual(
166 reactions,
167 );
168 expect(applyPendingPullRequestReactions([], pending([["heart", false]]))).toEqual([]);
169 });
170});