Skip to content
Source
Sign inSign up

pullRequestReactions.test.ts

60 lines2.2 KB

1import type { PullRequestReaction, PullRequestReactionContent } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { applyPendingPullRequestReactions } from "./pullRequestReactions.ts";
5​
6function reaction(overrides: Partial<PullRequestReaction> = {}): PullRequestReaction {
7 return {
8 content: "heart",
9 count: 1,
10 actors: ["octocat"],
11 viewerHasReacted: false,
12 ...overrides,
13 };
15​
16describe("pending reactions", () => {
17 const pending = (
18 entries: ReadonlyArray<readonly [PullRequestReactionContent, boolean]>,
19 ): ReadonlyMap<PullRequestReactionContent, boolean> => new Map(entries);
20​
21 it("adds a reaction nobody had yet, in picker order", () => {
22 const applied = applyPendingPullRequestReactions(
23 [reaction({ content: "rocket", count: 2, actors: ["a", "b"] })],
24 pending([["thumbs-up", true]]),
25 );
26 expect(applied).toEqual([
27 { content: "thumbs-up", count: 1, actors: [], viewerHasReacted: true },
28 { content: "rocket", count: 2, actors: ["a", "b"], viewerHasReacted: false },
29 ]);
30 });
31​
32 it("joins and leaves an existing reaction, and drops the pill nobody is left on", () => {
33 expect(
34 applyPendingPullRequestReactions(
35 [reaction({ count: 2, actors: ["a", "b"] })],
36 pending([["heart", true]]),
37 ),
38 ).toEqual([{ content: "heart", count: 3, actors: ["a", "b"], viewerHasReacted: true }]);
39 expect(
40 applyPendingPullRequestReactions(
41 [reaction({ count: 2, actors: ["a", "b"], viewerHasReacted: true })],
42 pending([["heart", false]]),
43 ),
44 ).toEqual([{ content: "heart", count: 1, actors: ["a", "b"], viewerHasReacted: false }]);
45 expect(
46 applyPendingPullRequestReactions(
47 [reaction({ count: 1, viewerHasReacted: true })],
48 pending([["heart", false]]),
49 ),
50 ).toEqual([]);
51 });
52​
53 it("ignores a pending state the host has already caught up with", () => {
54 const reactions = [reaction({ count: 3, viewerHasReacted: true })];
55 expect(applyPendingPullRequestReactions(reactions, pending([["heart", true]]))).toEqual(
56 reactions,
57 );
58 expect(applyPendingPullRequestReactions([], pending([["heart", false]]))).toEqual([]);
59 });
60});