mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 20:15:52 +01:00
This change introduces a new hook useConfigLink and a new component ConfigurationForm. These combination should replace the old Configuration component, which is not typed and which could not be used with react-hook-form.
170 lines
4.5 KiB
TypeScript
170 lines
4.5 KiB
TypeScript
/*
|
|
* 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 fetchMock from "fetch-mock-jest";
|
|
import { HalRepresentation } from "@scm-manager/ui-types";
|
|
import { renderHook } from "@testing-library/react-hooks";
|
|
import createWrapper from "./tests/createWrapper";
|
|
import { useConfigLink } from "./configLink";
|
|
import createInfiniteCachingClient from "./tests/createInfiniteCachingClient";
|
|
import { act } from "react-test-renderer";
|
|
|
|
describe("useConfigLink tests", () => {
|
|
type MyConfig = HalRepresentation & {
|
|
name: string;
|
|
};
|
|
|
|
const myReadOnlyConfig: MyConfig = {
|
|
name: "Hansolo",
|
|
_links: {},
|
|
};
|
|
|
|
const myConfig: MyConfig = {
|
|
name: "Lea",
|
|
_links: {
|
|
update: {
|
|
href: "/my/config-write",
|
|
},
|
|
},
|
|
};
|
|
|
|
afterEach(() => {
|
|
fetchMock.reset();
|
|
});
|
|
|
|
const fetchConfiguration = async (config: MyConfig) => {
|
|
fetchMock.getOnce("/api/v2/my/config", config);
|
|
|
|
const queryClient = createInfiniteCachingClient();
|
|
|
|
const { result, waitFor } = renderHook(() => useConfigLink<MyConfig>("/my/config"), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
await waitFor(() => {
|
|
return !!result.current.initialConfiguration;
|
|
});
|
|
|
|
return result.current;
|
|
};
|
|
|
|
it("should return read only configuration without update link", async () => {
|
|
const { isReadOnly } = await fetchConfiguration(myReadOnlyConfig);
|
|
|
|
expect(isReadOnly).toBe(true);
|
|
});
|
|
|
|
it("should not be read only with update link", async () => {
|
|
const { isReadOnly } = await fetchConfiguration(myConfig);
|
|
|
|
expect(isReadOnly).toBe(false);
|
|
});
|
|
|
|
it("should call update url", async () => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
|
|
fetchMock.get("/api/v2/my/config", myConfig);
|
|
|
|
const { result, waitFor, waitForNextUpdate } = renderHook(() => useConfigLink<MyConfig>("/my/config"), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
|
|
await waitFor(() => {
|
|
return !!result.current.initialConfiguration;
|
|
});
|
|
|
|
const { update } = result.current;
|
|
|
|
const lukesConfig = {
|
|
...myConfig,
|
|
name: "Luke",
|
|
};
|
|
|
|
fetchMock.putOnce(
|
|
{
|
|
url: "/api/v2/my/config-write",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: lukesConfig,
|
|
},
|
|
{
|
|
status: 204,
|
|
}
|
|
);
|
|
|
|
await act(() => {
|
|
update(lukesConfig);
|
|
return waitForNextUpdate();
|
|
});
|
|
|
|
expect(result.current.isUpdated).toBe(true);
|
|
});
|
|
|
|
it("should capture content type update url", async () => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
|
|
fetchMock.get("/api/v2/my/config", {
|
|
headers: {
|
|
"Content-Type": "application/myconfig+json",
|
|
},
|
|
body: myConfig,
|
|
});
|
|
|
|
const { result, waitFor, waitForNextUpdate } = renderHook(() => useConfigLink<MyConfig>("/my/config"), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
|
|
await waitFor(() => {
|
|
return !!result.current.initialConfiguration;
|
|
});
|
|
|
|
const { update } = result.current;
|
|
|
|
const lukesConfig = {
|
|
...myConfig,
|
|
name: "Luke",
|
|
};
|
|
|
|
fetchMock.putOnce(
|
|
{
|
|
url: "/api/v2/my/config-write",
|
|
headers: {
|
|
"Content-Type": "application/myconfig+json",
|
|
},
|
|
body: lukesConfig,
|
|
},
|
|
{
|
|
status: 204,
|
|
}
|
|
);
|
|
|
|
await act(() => {
|
|
update(lukesConfig);
|
|
return waitForNextUpdate();
|
|
});
|
|
|
|
expect(result.current.isUpdated).toBe(true);
|
|
});
|
|
});
|