|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { |
| 3 | + render, |
| 4 | + waitFor, |
| 5 | + fireEvent, |
| 6 | + type RenderResult, |
| 7 | +} from "@testing-library/react"; |
| 8 | +import nock from "nock"; |
| 9 | +import BitbucketModal from "./BitbucketModal"; |
| 10 | +import { GitDetails } from "./types.d"; |
| 11 | + |
| 12 | +const apiDomain = process.env.API_DOMAIN || ""; |
| 13 | + |
| 14 | +describe("~/pages/admin/Git/BitbucketModal.tsx", () => { |
| 15 | + let wrapper: RenderResult; |
| 16 | + const mockCloseModal = vi.fn(); |
| 17 | + const mockOnSuccess = vi.fn(); |
| 18 | + |
| 19 | + const findClientID = () => |
| 20 | + wrapper.getByLabelText(/Client ID/) as HTMLInputElement; |
| 21 | + const findClientSecret = () => |
| 22 | + wrapper.getByLabelText(/Client secret/) as HTMLInputElement; |
| 23 | + const findDeployKey = () => |
| 24 | + wrapper.getByLabelText(/Deploy key/) as HTMLInputElement; |
| 25 | + const findCancelButton = () => |
| 26 | + wrapper.getByRole("button", { name: /Cancel/ }) as HTMLButtonElement; |
| 27 | + const findConfigureButton = () => |
| 28 | + wrapper.getByRole("button", { name: /Configure/ }) as HTMLButtonElement; |
| 29 | + |
| 30 | + describe("without existing details", () => { |
| 31 | + beforeEach(() => { |
| 32 | + nock.cleanAll(); |
| 33 | + vi.clearAllMocks(); |
| 34 | + wrapper = render( |
| 35 | + <BitbucketModal closeModal={mockCloseModal} onSuccess={mockOnSuccess} /> |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + nock.cleanAll(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should render modal with correct title and form fields", async () => { |
| 44 | + await waitFor(() => { |
| 45 | + wrapper.getByText("Configure Bitbucket"); |
| 46 | + wrapper.getByText("Client ID"); |
| 47 | + wrapper.getByText("Client secret"); |
| 48 | + wrapper.getByText("Deploy key"); |
| 49 | + wrapper.getByRole("button", { name: "Cancel" }); |
| 50 | + wrapper.getByRole("button", { name: "Configure" }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("with existing details", () => { |
| 56 | + const existingDetails: GitDetails = { |
| 57 | + bitbucket: { |
| 58 | + clientId: "existing-client-id", |
| 59 | + hasDeployKey: true, |
| 60 | + hasClientSecret: true, |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + nock.cleanAll(); |
| 66 | + vi.clearAllMocks(); |
| 67 | + wrapper = render( |
| 68 | + <BitbucketModal |
| 69 | + closeModal={mockCloseModal} |
| 70 | + onSuccess={mockOnSuccess} |
| 71 | + details={existingDetails} |
| 72 | + /> |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + nock.cleanAll(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should populate fields with existing details", () => { |
| 81 | + expect(findClientID().value).toBe("existing-client-id"); |
| 82 | + expect(findClientSecret().placeholder).toBe("***************"); |
| 83 | + expect(findDeployKey().placeholder).toBe("Enter deploy key"); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("form submission", () => { |
| 88 | + beforeEach(() => { |
| 89 | + nock.cleanAll(); |
| 90 | + vi.clearAllMocks(); |
| 91 | + wrapper = render( |
| 92 | + <BitbucketModal closeModal={mockCloseModal} onSuccess={mockOnSuccess} /> |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + afterEach(() => { |
| 97 | + nock.cleanAll(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should call closeModal when cancel button is clicked", () => { |
| 101 | + fireEvent.click(findCancelButton()); |
| 102 | + expect(mockCloseModal).toHaveBeenCalledTimes(1); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should successfully submit configuration", async () => { |
| 106 | + fireEvent.change(findClientID(), { target: { value: "new-client-id" } }); |
| 107 | + fireEvent.change(findClientSecret(), { |
| 108 | + target: { value: "new-client-secret" }, |
| 109 | + }); |
| 110 | + fireEvent.change(findDeployKey(), { |
| 111 | + target: { value: "new-deploy-key" }, |
| 112 | + }); |
| 113 | + |
| 114 | + const scope = nock(apiDomain) |
| 115 | + .post("/admin/git/configure", { |
| 116 | + provider: "bitbucket", |
| 117 | + clientId: "new-client-id", |
| 118 | + clientSecret: "new-client-secret", |
| 119 | + deployKey: "new-deploy-key", |
| 120 | + }) |
| 121 | + .reply(200, { success: true }); |
| 122 | + |
| 123 | + fireEvent.click(findConfigureButton()); |
| 124 | + |
| 125 | + await waitFor(() => { |
| 126 | + expect(scope.isDone()).toBe(true); |
| 127 | + expect(mockOnSuccess).toHaveBeenCalledTimes(1); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments