Fix diff for mercurial and subversion (#1588)

The useDiff hook appends the query param for the limit always with a question mark, even if the link has already query parameters.
Now we check if the url has an question mark and then we use an ampersand instead if the question mark to append the limit.
This commit is contained in:
Sebastian Sdorra
2021-03-16 09:11:19 +01:00
committed by GitHub
parent 89e938c6d7
commit 9b254b4a8d
3 changed files with 18 additions and 3 deletions

View File

@@ -222,4 +222,16 @@ describe("Test diff", () => {
expect(result.current.data).toEqual({ ...partialDiff2, files: [partialDiff1.files[0], partialDiff2.files[0]] });
});
it("should append query parameters to url which has already query params", async () => {
fetchMock.getOnce("/api/v2/diff?format=GIT&limit=25", {
body: simpleDiff,
headers: { "Content-Type": "application/vnd.scmm-diffparsed+json;v=2" }
});
const { result, waitFor } = renderHook(() => useDiff("/diff?format=GIT", { limit: 25 }), {
wrapper: createWrapper()
});
await waitFor(() => !!result.current.data);
expect(result.current.data).toEqual(simpleDiff);
});
});

View File

@@ -35,7 +35,8 @@ type UseDiffOptions = {
export const useDiff = (link: string, options: UseDiffOptions = {}) => {
let initialLink = link;
if (options.limit) {
initialLink = `${initialLink}?limit=${options.limit}`;
const separator = initialLink.includes("?") ? "&" : "?";
initialLink = `${initialLink}${separator}limit=${options.limit}`;
}
const { isLoading, error, data, isFetchingNextPage, fetchNextPage } = useInfiniteQuery<Diff, Error, Diff>(
["link", link],
@@ -48,9 +49,9 @@ export const useDiff = (link: string, options: UseDiffOptions = {}) => {
return response
.text()
.then(parser.parse)
.then(data => {
.then(parsedGit => {
return {
files: data,
files: parsedGit,
partial: false,
_links: {}
};