|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act, type ReactNode } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, expect, it, vi } from "vitest"; |
| 6 | +import { baseDictionary } from "@/i18n"; |
| 7 | +import type { Service } from "@/lib/api/services"; |
| 8 | +import { Deployments } from "./Deployments"; |
| 9 | + |
| 10 | +const mocks = vi.hoisted(() => ({ |
| 11 | + context: vi.fn(), |
| 12 | + trigger: vi.fn(), |
| 13 | + showModal: vi.fn(), |
| 14 | + hideModal: vi.fn(), |
| 15 | + setActiveTab: vi.fn(), |
| 16 | + openBuild: vi.fn(), |
| 17 | +})); |
| 18 | +vi.mock("@/context/ProjectSettingsContext", () => ({ useProjectSettings: mocks.context })); |
| 19 | +vi.mock("@/context/ToastContext", () => ({ useToast: () => ({ showToast: vi.fn() }) })); |
| 20 | +vi.mock("@/context/ModalContext", () => ({ |
| 21 | + useModal: () => ({ showModal: mocks.showModal, hideModal: mocks.hideModal }), |
| 22 | +})); |
| 23 | +vi.mock("@/lib/api", () => ({ |
| 24 | + deployApi: { trigger: mocks.trigger }, |
| 25 | + projectsApi: { getCommitStatus: async () => ({ data: { supported: false } }) }, |
| 26 | + isAbortError: () => false, |
| 27 | +})); |
| 28 | +vi.mock("@/lib/deploy-nav", () => ({ openTriggeredBuild: mocks.openBuild })); |
| 29 | +vi.mock("next/navigation", () => ({ useRouter: () => ({ push: vi.fn() }) })); |
| 30 | +vi.mock("@/components/i18n-provider", () => ({ |
| 31 | + useI18n: () => ({ t: baseDictionary }), |
| 32 | + interpolate: (text: string, values: Record<string, string>) => |
| 33 | + text.replace(/\{(\w+)\}/g, (_, key: string) => values[key] ?? key), |
| 34 | +})); |
| 35 | +vi.mock("@/app/(dashboard)/deployments/components", () => ({ DeploymentsContent: () => null })); |
| 36 | + |
| 37 | +const service: Service = { |
| 38 | + id: "web", |
| 39 | + name: "web", |
| 40 | + kind: "compose", |
| 41 | + image: "web:1", |
| 42 | + build: null, |
| 43 | + dockerfile: null, |
| 44 | + buildArgs: null, |
| 45 | + ports: ["20020:9000"], |
| 46 | + dependsOn: [], |
| 47 | + environment: {}, |
| 48 | + volumes: [], |
| 49 | + command: null, |
| 50 | + restart: "unless-stopped", |
| 51 | + exposed: false, |
| 52 | + exposedPort: "9000", |
| 53 | + domain: null, |
| 54 | + customDomain: null, |
| 55 | + domainType: "custom", |
| 56 | + publicEndpoints: [], |
| 57 | + enabled: true, |
| 58 | + sortOrder: 0, |
| 59 | +}; |
| 60 | +let root: Root; |
| 61 | +let container: HTMLDivElement; |
| 62 | +let context: { |
| 63 | + id: string; |
| 64 | + projectData: { id: string; name: string; port: number }; |
| 65 | + hasMultipleServices: boolean; |
| 66 | + servicesData: { services: Service[] }; |
| 67 | + domainsData: { domains: Array<{ hostname: string; serviceId?: string; targetPort?: number }> }; |
| 68 | + refreshServices: ReturnType<typeof vi.fn>; |
| 69 | + setActiveTab: typeof mocks.setActiveTab; |
| 70 | +}; |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + vi.clearAllMocks(); |
| 74 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 75 | + container = document.createElement("div"); |
| 76 | + document.body.append(container); |
| 77 | + root = createRoot(container); |
| 78 | + context = { |
| 79 | + id: "project", |
| 80 | + projectData: { id: "project", name: "demo", port: 9000 }, |
| 81 | + hasMultipleServices: true, |
| 82 | + servicesData: { services: [service] }, |
| 83 | + domainsData: { domains: [] }, |
| 84 | + refreshServices: vi.fn().mockResolvedValue([service]), |
| 85 | + setActiveTab: mocks.setActiveTab, |
| 86 | + }; |
| 87 | + mocks.context.mockImplementation(() => context); |
| 88 | + mocks.trigger.mockResolvedValue({ data: { deploymentId: "deployment" } }); |
| 89 | + mocks.showModal.mockReturnValue("warning"); |
| 90 | +}); |
| 91 | + |
| 92 | +afterEach(async () => { |
| 93 | + await act(async () => root.unmount()); |
| 94 | + container.remove(); |
| 95 | + vi.unstubAllGlobals(); |
| 96 | +}); |
| 97 | + |
| 98 | +function button(text: string) { |
| 99 | + const result = [...container.querySelectorAll("button")].find((el) => el.textContent === text); |
| 100 | + expect(result, text).toBeDefined(); |
| 101 | + return result!; |
| 102 | +} |
| 103 | + |
| 104 | +async function redeploy() { |
| 105 | + await act(async () => root.render(<Deployments />)); |
| 106 | + await act(async () => button(baseDictionary.projects.redeploy.redeployProject).click()); |
| 107 | +} |
| 108 | + |
| 109 | +it.each([ |
| 110 | + { hostname: "app.example.test", serviceId: "web", targetPort: 9000 }, |
| 111 | + { hostname: "app.example.test", targetPort: 20020 }, |
| 112 | + { hostname: "app.example.test" }, |
| 113 | +])("redeploys using the loaded domain route without a false warning: %j", async (domain) => { |
| 114 | + context.domainsData.domains = [domain]; |
| 115 | + await redeploy(); |
| 116 | + expect(mocks.showModal).not.toHaveBeenCalled(); |
| 117 | + expect(mocks.trigger).toHaveBeenCalledWith({ projectId: "project", smartRoute: true }); |
| 118 | + expect(mocks.openBuild).toHaveBeenCalled(); |
| 119 | +}); |
| 120 | + |
| 121 | +it("checks freshly loaded services against the project's domain routes", async () => { |
| 122 | + context.servicesData.services = []; |
| 123 | + context.domainsData.domains = [{ hostname: "app.example.test", serviceId: "web" }]; |
| 124 | + await redeploy(); |
| 125 | + expect(context.refreshServices).toHaveBeenCalledOnce(); |
| 126 | + expect(mocks.trigger).toHaveBeenCalledOnce(); |
| 127 | + expect(mocks.showModal).not.toHaveBeenCalled(); |
| 128 | +}); |
| 129 | + |
| 130 | +it("keeps the warning for another service's route and sends the operator to Domains", async () => { |
| 131 | + context.domainsData.domains = [ |
| 132 | + { hostname: "other.example.test", serviceId: "other", targetPort: 9000 }, |
| 133 | + ]; |
| 134 | + await redeploy(); |
| 135 | + expect(mocks.trigger).not.toHaveBeenCalled(); |
| 136 | + expect(mocks.showModal).toHaveBeenCalledOnce(); |
| 137 | + const modal = mocks.showModal.mock.calls[0][0] as { customContent: ReactNode }; |
| 138 | + await act(async () => root.render(modal.customContent)); |
| 139 | + await act(async () => button(baseDictionary.projects.redeploy.openDomains).click()); |
| 140 | + expect(mocks.setActiveTab).toHaveBeenCalledWith("domains"); |
| 141 | + expect(mocks.hideModal).toHaveBeenCalledWith("warning"); |
| 142 | +}); |
0 commit comments