|
| 1 | +import { waitFor } from "@testing-library/react"; |
| 2 | +import * as M from "../../testHelpers/entities"; |
| 3 | +import { type Workspace } from "api/typesGenerated"; |
| 4 | +import { useWorkspaceDuplication } from "./useWorkspaceDuplication"; |
| 5 | +import { MockWorkspace } from "testHelpers/entities"; |
| 6 | +import CreateWorkspacePage from "./CreateWorkspacePage"; |
| 7 | +import { renderHookWithAuth } from "testHelpers/renderHelpers"; |
| 8 | + |
| 9 | +function render(workspace?: Workspace) { |
| 10 | + return renderHookWithAuth( |
| 11 | + ({ workspace }: { workspace?: Workspace }) => { |
| 12 | + return useWorkspaceDuplication(workspace); |
| 13 | + }, |
| 14 | + { |
| 15 | + initialProps: { workspace }, |
| 16 | + extraRoutes: [ |
| 17 | + { |
| 18 | + path: "/templates/:template/workspace", |
| 19 | + element: <CreateWorkspacePage />, |
| 20 | + }, |
| 21 | + ], |
| 22 | + }, |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +type RenderResult = Awaited<ReturnType<typeof render>>; |
| 27 | + |
| 28 | +async function performNavigation( |
| 29 | + result: RenderResult["result"], |
| 30 | + router: RenderResult["router"], |
| 31 | +) { |
| 32 | + await waitFor(() => expect(result.current.isDuplicationReady).toBe(true)); |
| 33 | + result.current.duplicateWorkspace(); |
| 34 | + |
| 35 | + return waitFor(() => { |
| 36 | + expect(router.state.location.pathname).toEqual( |
| 37 | + `/templates/${MockWorkspace.template_name}/workspace`, |
| 38 | + ); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +describe(`${useWorkspaceDuplication.name}`, () => { |
| 43 | + it("Will never be ready when there is no workspace passed in", async () => { |
| 44 | + const { result, rerender } = await render(undefined); |
| 45 | + expect(result.current.isDuplicationReady).toBe(false); |
| 46 | + |
| 47 | + for (let i = 0; i < 10; i++) { |
| 48 | + rerender({ workspace: undefined }); |
| 49 | + expect(result.current.isDuplicationReady).toBe(false); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("Will become ready when workspace is provided and build params are successfully fetched", async () => { |
| 54 | + const { result } = await render(MockWorkspace); |
| 55 | + |
| 56 | + expect(result.current.isDuplicationReady).toBe(false); |
| 57 | + await waitFor(() => expect(result.current.isDuplicationReady).toBe(true)); |
| 58 | + }); |
| 59 | + |
| 60 | + it("Is able to navigate the user to the workspace creation page", async () => { |
| 61 | + const { result, router } = await render(MockWorkspace); |
| 62 | + await performNavigation(result, router); |
| 63 | + }); |
| 64 | + |
| 65 | + test("Navigating populates the URL search params with the workspace's build params", async () => { |
| 66 | + const { result, router } = await render(MockWorkspace); |
| 67 | + await performNavigation(result, router); |
| 68 | + |
| 69 | + const parsedParams = new URLSearchParams(router.state.location.search); |
| 70 | + const mockBuildParams = [ |
| 71 | + M.MockWorkspaceBuildParameter1, |
| 72 | + M.MockWorkspaceBuildParameter2, |
| 73 | + M.MockWorkspaceBuildParameter3, |
| 74 | + M.MockWorkspaceBuildParameter4, |
| 75 | + M.MockWorkspaceBuildParameter5, |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const { name, value } of mockBuildParams) { |
| 79 | + const key = `param.${name}`; |
| 80 | + expect(parsedParams.get(key)).toEqual(value); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + test("Navigating appends other necessary metadata to the search params", async () => { |
| 85 | + const { result, router } = await render(MockWorkspace); |
| 86 | + await performNavigation(result, router); |
| 87 | + |
| 88 | + const parsedParams = new URLSearchParams(router.state.location.search); |
| 89 | + const extraMetadataEntries = [ |
| 90 | + ["mode", "duplicate"], |
| 91 | + ["name", MockWorkspace.name], |
| 92 | + ["version", MockWorkspace.template_active_version_id], |
| 93 | + ] as const; |
| 94 | + |
| 95 | + for (const [key, value] of extraMetadataEntries) { |
| 96 | + expect(parsedParams.get(key)).toBe(value); |
| 97 | + } |
| 98 | + }); |
| 99 | +}); |
0 commit comments