| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import type { NavigationState } from "@react-navigation/native"; |
| 3 | import { selectedPullRequestKey } from "./pullRequestSelection"; |
| 4 | |
| 5 | const params = { |
| 6 | environmentId: "environment-one", |
| 7 | projectId: "project-one", |
| 8 | host: "GitHub.COM", |
| 9 | repository: "Org/Repo", |
| 10 | number: 42, |
| 11 | }; |
| 12 | const home = { key: "home", name: "Home" }; |
| 13 | const pullRequest = { key: "pr", name: "PullRequest", params }; |
| 14 | function selection(routes: NavigationState["routes"], index = routes.length - 1) { |
| 15 | return selectedPullRequestKey({ routes, index }); |
| 16 | } |
| 17 | |
| 18 | describe("displayed pull request selection", () => { |
| 19 | it("normalizes repository and host while retaining the environment scope", () => { |
| 20 | expect(selection([home, pullRequest])).toBe("environment-one:github.com:org/repo:42"); |
| 21 | expect( |
| 22 | selection([ |
| 23 | home, |
| 24 | { ...pullRequest, params: { ...params, environmentId: "environment-two" } }, |
| 25 | ]), |
| 26 | ).toBe("environment-two:github.com:org/repo:42"); |
| 27 | }); |
| 28 | |
| 29 | it.each([ |
| 30 | "PullRequestFile", |
| 31 | "PullRequestFiles", |
| 32 | "PullRequestGuideStop", |
| 33 | "PullRequestDiscussion", |
| 34 | "PullRequestReviewers", |
| 35 | "PullRequestReview", |
| 36 | "PullRequestInlineComment", |
| 37 | ])("keeps the owning selection on %s", (name) => { |
| 38 | expect(selection([home, pullRequest, { key: name, name, params }])).toBe( |
| 39 | "environment-one:github.com:org/repo:42", |
| 40 | ); |
| 41 | }); |
| 42 | |
| 43 | it("uses the most recently opened PR, including a different hosting service", () => { |
| 44 | expect( |
| 45 | selection([ |
| 46 | home, |
| 47 | pullRequest, |
| 48 | { |
| 49 | key: "other-pr", |
| 50 | name: "PullRequest", |
| 51 | params: { ...params, host: "github.enterprise", repository: "Other/Repo", number: 7 }, |
| 52 | }, |
| 53 | ]), |
| 54 | ).toBe("environment-one:github.enterprise:other/repo:7"); |
| 55 | }); |
| 56 | |
| 57 | it.each(["Home", "Thread", "ThreadFiles"])("clears an older selection on %s", (name) => { |
| 58 | expect(selection([home, pullRequest, { key: "current", name }])).toBeNull(); |
| 59 | }); |
| 60 | |
| 61 | it("ignores inactive routes after the navigation index", () => { |
| 62 | expect(selection([home, pullRequest], 0)).toBeNull(); |
| 63 | }); |
| 64 | |
| 65 | it("retains the owning PR behind an unrelated overlay", () => { |
| 66 | expect(selection([home, pullRequest, { key: "settings", name: "SettingsSheet" }])).toBe( |
| 67 | "environment-one:github.com:org/repo:42", |
| 68 | ); |
| 69 | }); |
| 70 | |
| 71 | it("does not highlight an older PR when the current PR lacks its hosting identity", () => { |
| 72 | expect( |
| 73 | selection([ |
| 74 | home, |
| 75 | pullRequest, |
| 76 | { key: "unresolved", name: "PullRequest", params: { environmentId: "environment-one" } }, |
| 77 | ]), |
| 78 | ).toBeNull(); |
| 79 | }); |
| 80 | }); |