|
| 1 | +import { RecoilRoot } from "recoil"; |
| 2 | +import { BrowserRouter } from "react-router-dom"; |
| 3 | +import { QueryClient, QueryClientProvider } from "react-query"; |
| 4 | +import { http, HttpResponse } from "msw"; |
| 5 | +import { setupServer } from "msw/node"; |
| 6 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 7 | +import "@testing-library/jest-dom"; |
| 8 | + |
| 9 | +import Builds from "../Builds"; |
| 10 | + |
| 11 | +jest.mock("react-router-dom", () => { |
| 12 | + return { |
| 13 | + ...jest.requireActual("react-router-dom"), |
| 14 | + useParams: () => ({ |
| 15 | + snapId: "test-snap-id", |
| 16 | + }), |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +const queryClient = new QueryClient(); |
| 21 | + |
| 22 | +function renderComponent() { |
| 23 | + return render( |
| 24 | + <RecoilRoot> |
| 25 | + <QueryClientProvider client={queryClient}> |
| 26 | + <BrowserRouter> |
| 27 | + <Builds /> |
| 28 | + </BrowserRouter> |
| 29 | + </QueryClientProvider> |
| 30 | + </RecoilRoot>, |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +const server = setupServer(); |
| 35 | + |
| 36 | +beforeAll(() => { |
| 37 | + server.listen(); |
| 38 | +}); |
| 39 | + |
| 40 | +afterEach(() => { |
| 41 | + server.resetHandlers(); |
| 42 | +}); |
| 43 | + |
| 44 | +afterAll(() => { |
| 45 | + server.close(); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("Build", () => { |
| 49 | + test("it shows login to GitHub", async () => { |
| 50 | + server.use( |
| 51 | + http.get("/api/test-snap-id/repo", () => { |
| 52 | + return HttpResponse.json({ data: null }); |
| 53 | + }), |
| 54 | + ); |
| 55 | + |
| 56 | + renderComponent(); |
| 57 | + |
| 58 | + await waitFor(() => { |
| 59 | + expect( |
| 60 | + screen.getByText(/Login to your GitHub account to start building/), |
| 61 | + ).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test("it shows connected to GitHub", async () => { |
| 66 | + server.use( |
| 67 | + http.get("/api/test-snap-id/repo", () => { |
| 68 | + return HttpResponse.json({ |
| 69 | + success: true, |
| 70 | + message: "", |
| 71 | + data: { |
| 72 | + github_orgs: [ |
| 73 | + { |
| 74 | + login: "canonical", |
| 75 | + name: "Canonical", |
| 76 | + }, |
| 77 | + ], |
| 78 | + github_repository: null, |
| 79 | + github_user: { |
| 80 | + avatarUrl: "https://gravatar.com/avatar", |
| 81 | + login: "johndoe", |
| 82 | + name: "John Doe", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + }), |
| 87 | + ); |
| 88 | + |
| 89 | + renderComponent(); |
| 90 | + |
| 91 | + await waitFor(() => { |
| 92 | + expect( |
| 93 | + screen.getByText(/Your GitHub account is connected/), |
| 94 | + ).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments