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

pullRequestReactions.logic.test.ts

200 lines6.8 KB

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