Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions apps/mobile/src/features/pull-requests/usePullRequestDiff.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,46 @@ function patch(path: string, nextCursor: string | null = null): PullRequestDiffR
};
}

function setup() {
function setup(guideHeadSha?: string) {
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const registry = AtomRegistry.make();
const requests: {
cursor: string | null;
resolve: (value: PullRequestDiffResult) => void;
reject: (error: Error) => void;
}[] = [];
const queries = Atom.family((cursor: string | null) =>
const queries = Atom.family((key: string) =>
Atom.make(
Effect.tryPromise({
try: () =>
new Promise<PullRequestDiffResult>((resolve, reject) => {
requests.push({ cursor, resolve, reject });
requests.push({
cursor: (JSON.parse(key) as { cursor: string | null }).cursor,
resolve,
reject,
});
}),
catch: (cause) =>
new PullRequestOperationError({ operation: "diff", detail: "Connection lost", cause }),
}),
).pipe(Atom.keepAlive),
);
mocks.diff.mockImplementation((target: { input: { cursor?: string } }) =>
queries(target.input.cursor ?? null),
mocks.diff.mockImplementation((target: { input: { cursor?: string; cacheHeadSha?: string } }) =>
queries(
JSON.stringify({ cursor: target.input.cursor ?? null, head: target.input.cacheHeadSha }),
),
);
const root = createRoot(document.createElement("div"));
let current: ReturnType<typeof usePullRequestDiff>;
function Probe() {
const result = usePullRequestDiff(
{
environmentId: "environment",
projectId: "project",
repository: "repo",
number: 1,
},
sourceControlClients.get("github").changeRequest,
);
const params = {
environmentId: "environment",
projectId: "project",
repository: "repo",
number: 1,
guideHeadSha,
};
const result = usePullRequestDiff(params, sourceControlClients.get("github").changeRequest);
useLayoutEffect(() => {
current = result;
});
Expand All @@ -71,7 +76,8 @@ function setup() {
);
return {
requests,
remount() {
remount(nextGuideHeadSha = guideHeadSha) {
guideHeadSha = nextGuideHeadSha;
act(() =>
root.render(
<RegistryContext.Provider value={registry}>
Expand Down Expand Up @@ -246,3 +252,20 @@ it("still refreshes from page one after successfully loading a continuation", as
if (screen.current.parsed.kind !== "files") throw new Error("Expected files");
expect(screen.current.parsed.files.map((file) => file.path)).toEqual(["refreshed.ts"]);
});

it("does not show the previous push's patch when a rewritten guide opens", async () => {
using screen = setup("old-head");
await screen.succeed(0, {
...patch("shared.ts"),
patch: patch("shared.ts").patch.replace("+new", "+previous code"),
});
expect(screen.current.patch).toContain("+previous code");

screen.remount("new-head");
expect(screen.current.patch).not.toContain("+previous code");
await screen.succeed(1, {
...patch("shared.ts"),
patch: patch("shared.ts").patch.replace("+new", "+current code"),
});
expect(screen.current.patch).toContain("+current code");
});
12 changes: 10 additions & 2 deletions apps/mobile/src/features/pull-requests/usePullRequestDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export type PullRequestParams = {
readonly initialSummary?: PullRequestListEntry | PullRequestSummary;
};

export type PullRequestDiffParams = PullRequestParams & { readonly commit?: string };
export type PullRequestDiffParams = PullRequestParams & {
readonly commit?: string;
// A guide stop reads the diff at the push its guide was written for, not a cached older one.
readonly guideHeadSha?: string;
};

export function pullRequestTarget(params: PullRequestParams) {
return {
Expand Down Expand Up @@ -115,7 +119,11 @@ export function usePullRequestDiff(
const reference = pullRequestTarget(params);
const target = {
...reference,
input: { ...reference.input, ...(params.commit ? { commit: params.commit } : {}) },
input: {
...reference.input,
...(params.commit ? { commit: params.commit } : {}),
...(params.guideHeadSha ? { cacheHeadSha: params.guideHeadSha } : {}),
},
};
const key = JSON.stringify(target);
const firstPageAtom = composerPullRequests.diff(target);
Expand Down
Loading