Skip to content

Commit 5a58a8b

Browse files
authored
chore: Add tests for publisher builds page (#5027)
1 parent 1302d2d commit 5a58a8b

4 files changed

Lines changed: 286 additions & 5 deletions

File tree

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
testEnvironment: "jsdom",
2+
testEnvironment: "jest-fixed-jsdom",
33
testEnvironmentOptions: {
44
url: "http://localhost.test",
55
},

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
"identity-obj-proxy": "3.0.0",
128128
"jest": "29.7.0",
129129
"jest-environment-jsdom": "29.7.0",
130+
"jest-fixed-jsdom": "0.0.9",
131+
"msw": "2.7.0",
130132
"sass-loader": "16.0.4",
131133
"stylelint": "16.12.0",
132134
"stylelint-config-standard-scss": "14.0.0",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)