Open History to see the last commit
1import { expect, it, vi } from "vite-plus/test";
2
3const state = vi.hoisted(() => ({
4 dispatch: vi.fn(),
5 lookups: new Map<number, PromiseWithResolvers<unknown>>(),
6}));
7
8vi.mock("@react-navigation/native", () => ({
9 useNavigation: () => ({ dispatch: state.dispatch }),
10 useRoute: () => ({ params: undefined }),
11 StackActions: { push: (name: string, params: unknown) => ({ name, params }) },
12}));
13vi.mock("../state/entities", () => ({ useProjects: () => [], useThreadShell: () => null }));
14vi.mock("../state/atom-registry", () => ({
15 appAtomRegistry: {
16 get: () => new Map([["env", { environment: { capabilities: { issues: true } } }]]),
17 },
18}));
19vi.mock("../state/server", () => ({ environmentServerConfigsAtom: {} }));
20vi.mock("../state/issueTrackers", () => ({ issueTrackerEnvironment: {} }));
21vi.mock("../state/pull-requests", () => ({ composerPullRequests: {} }));
22vi.mock("../state/use-thread-pr", () => ({
23 withKnownPullRequestSummary: (params: unknown) => params,
24}));
25vi.mock("../lib/openExternalUrl", () => ({ tryOpenWebLink: vi.fn() }));
26vi.mock("@t3tools/client-runtime/state/runtime", () => ({ isAtomCommandInterrupted: () => false }));
27vi.mock("@t3tools/client-runtime/issue-links", () => ({
28 isStandaloneIssueLink: () => false,
29 resolveIssueLinkTarget: () => ({ ref: { projectId: "project" } }),
30}));
31vi.mock("../lib/pullRequestNavigation", () => ({
32 nativePullRequestDestination: () => null,
33 pullRequestCandidateForIssueLink: (url: string) => ({
34 target: { repository: "o/r", number: Number(url.split("/").pop()) },
35 }),
36}));
37vi.mock("../state/use-atom-query-runner", () => ({
38 useAtomQueryRunner:
39 () =>
40 ({ input }: { input: { number: number } }) => {
41 const lookup = Promise.withResolvers<unknown>();
42 state.lookups.set(input.number, lookup);
43 return lookup.promise;
44 },
45}));
46
47import { useNavigateToPullRequest } from "./useOpenPullRequest";
48
49it("keeps the newer reference when an older one's lookup finishes last", async () => {
50 const open = useNavigateToPullRequest();
51 const context = { environmentId: "env", projectId: "project" };
52 const first = open("https://github.com/o/r/issues/41", context);
53 const second = open("https://github.com/o/r/issues/42", context);
54 state.lookups.get(42)!.resolve({ _tag: "Success", value: {} });
55 await second;
56 state.lookups.get(41)!.resolve({ _tag: "Success", value: {} });
57 await first;
58 expect(state.dispatch.mock.calls.map(([action]) => action.params.number)).toEqual([42]);
59});