2021-02-24 08:17:40 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
import { Branch, BranchCollection, Repository } from "@scm-manager/ui-types";
|
|
|
|
|
import fetchMock from "fetch-mock-jest";
|
|
|
|
|
import { renderHook } from "@testing-library/react-hooks";
|
|
|
|
|
import createWrapper from "./tests/createWrapper";
|
|
|
|
|
import createInfiniteCachingClient from "./tests/createInfiniteCachingClient";
|
|
|
|
|
import { useBranch, useBranches, useCreateBranch, useDeleteBranch } from "./branches";
|
|
|
|
|
import { act } from "react-test-renderer";
|
|
|
|
|
|
|
|
|
|
describe("Test branches hooks", () => {
|
|
|
|
|
const repository: Repository = {
|
|
|
|
|
namespace: "hitchhiker",
|
|
|
|
|
name: "heart-of-gold",
|
|
|
|
|
type: "hg",
|
|
|
|
|
_links: {
|
|
|
|
|
branches: {
|
2021-06-28 13:19:03 +02:00
|
|
|
href: "/hog/branches",
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const develop: Branch = {
|
|
|
|
|
name: "develop",
|
|
|
|
|
revision: "42",
|
|
|
|
|
_links: {
|
|
|
|
|
delete: {
|
2021-06-28 13:19:03 +02:00
|
|
|
href: "/hog/branches/develop",
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const branches: BranchCollection = {
|
|
|
|
|
_embedded: {
|
2021-06-28 13:19:03 +02:00
|
|
|
branches: [develop],
|
2021-02-24 08:17:40 +01:00
|
|
|
},
|
2021-06-28 13:19:03 +02:00
|
|
|
_links: {},
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const queryClient = createInfiniteCachingClient();
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
queryClient.clear();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("useBranches tests", () => {
|
|
|
|
|
const fetchBrances = async () => {
|
|
|
|
|
fetchMock.getOnce("/api/v2/hog/branches", branches);
|
|
|
|
|
|
|
|
|
|
const { result, waitFor } = renderHook(() => useBranches(repository), {
|
2021-06-28 13:19:03 +02:00
|
|
|
wrapper: createWrapper(undefined, queryClient),
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
return !!result.current.data;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result.current.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it("should return branches", async () => {
|
|
|
|
|
const branches = await fetchBrances();
|
|
|
|
|
expect(branches).toEqual(branches);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should add branches to cache", async () => {
|
|
|
|
|
await fetchBrances();
|
|
|
|
|
|
|
|
|
|
const data = queryClient.getQueryData<BranchCollection>([
|
|
|
|
|
"repository",
|
|
|
|
|
"hitchhiker",
|
|
|
|
|
"heart-of-gold",
|
2021-06-28 13:19:03 +02:00
|
|
|
"branches",
|
2021-02-24 08:17:40 +01:00
|
|
|
]);
|
|
|
|
|
expect(data).toEqual(branches);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("useBranch tests", () => {
|
|
|
|
|
const fetchBranch = async () => {
|
|
|
|
|
fetchMock.getOnce("/api/v2/hog/branches/develop", develop);
|
|
|
|
|
|
|
|
|
|
const { result, waitFor } = renderHook(() => useBranch(repository, "develop"), {
|
2021-06-28 13:19:03 +02:00
|
|
|
wrapper: createWrapper(undefined, queryClient),
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.error).toBeUndefined();
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
return !!result.current.data;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result.current.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it("should return branch", async () => {
|
|
|
|
|
const branch = await fetchBranch();
|
|
|
|
|
expect(branch).toEqual(develop);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("useCreateBranch tests", () => {
|
|
|
|
|
const createBranch = async () => {
|
|
|
|
|
fetchMock.postOnce("/api/v2/hog/branches", {
|
|
|
|
|
status: 201,
|
|
|
|
|
headers: {
|
2021-06-28 13:19:03 +02:00
|
|
|
Location: "/hog/branches/develop",
|
|
|
|
|
},
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fetchMock.getOnce("/api/v2/hog/branches/develop", develop);
|
|
|
|
|
|
|
|
|
|
const { result, waitForNextUpdate } = renderHook(() => useCreateBranch(repository), {
|
2021-06-28 13:19:03 +02:00
|
|
|
wrapper: createWrapper(undefined, queryClient),
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await act(() => {
|
|
|
|
|
const { create } = result.current;
|
|
|
|
|
create({ name: "develop", parent: "main" });
|
|
|
|
|
return waitForNextUpdate();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result.current;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it("should create branch", async () => {
|
|
|
|
|
const { branch } = await createBranch();
|
|
|
|
|
expect(branch).toEqual(develop);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should cache created branch", async () => {
|
|
|
|
|
await createBranch();
|
|
|
|
|
|
|
|
|
|
const branch = queryClient.getQueryData<Branch>([
|
|
|
|
|
"repository",
|
|
|
|
|
"hitchhiker",
|
|
|
|
|
"heart-of-gold",
|
|
|
|
|
"branch",
|
2021-06-28 13:19:03 +02:00
|
|
|
"develop",
|
2021-02-24 08:17:40 +01:00
|
|
|
]);
|
|
|
|
|
expect(branch).toEqual(develop);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should invalidate cached branches list", async () => {
|
|
|
|
|
queryClient.setQueryData(["repository", "hitchhiker", "heart-of-gold", "branches"], branches);
|
|
|
|
|
await createBranch();
|
|
|
|
|
|
|
|
|
|
const queryState = queryClient.getQueryState(["repository", "hitchhiker", "heart-of-gold", "branches"]);
|
|
|
|
|
expect(queryState!.isInvalidated).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("useDeleteBranch tests", () => {
|
|
|
|
|
const deleteBranch = async () => {
|
|
|
|
|
fetchMock.deleteOnce("/api/v2/hog/branches/develop", {
|
2021-06-28 13:19:03 +02:00
|
|
|
status: 204,
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { result, waitForNextUpdate } = renderHook(() => useDeleteBranch(repository), {
|
2021-06-28 13:19:03 +02:00
|
|
|
wrapper: createWrapper(undefined, queryClient),
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await act(() => {
|
|
|
|
|
const { remove } = result.current;
|
|
|
|
|
remove(develop);
|
|
|
|
|
return waitForNextUpdate();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result.current;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it("should delete branch", async () => {
|
|
|
|
|
const { isDeleted } = await deleteBranch();
|
|
|
|
|
expect(isDeleted).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-28 13:19:03 +02:00
|
|
|
it("should delete branch cache", async () => {
|
2021-02-24 08:17:40 +01:00
|
|
|
queryClient.setQueryData(["repository", "hitchhiker", "heart-of-gold", "branch", "develop"], develop);
|
|
|
|
|
await deleteBranch();
|
|
|
|
|
|
|
|
|
|
const queryState = queryClient.getQueryState(["repository", "hitchhiker", "heart-of-gold", "branch", "develop"]);
|
2021-06-28 13:19:03 +02:00
|
|
|
expect(queryState).toBeUndefined();
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should invalidate cached branches list", async () => {
|
|
|
|
|
queryClient.setQueryData(["repository", "hitchhiker", "heart-of-gold", "branches"], branches);
|
|
|
|
|
await deleteBranch();
|
|
|
|
|
|
|
|
|
|
const queryState = queryClient.getQueryState(["repository", "hitchhiker", "heart-of-gold", "branches"]);
|
|
|
|
|
expect(queryState!.isInvalidated).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|