Skip to content

Commit b0cdd70

Browse files
committed
chore: add Vitest
1 parent bf6b2dd commit b0cdd70

File tree

9 files changed

+840
-27
lines changed

9 files changed

+840
-27
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ check:
5858
test_image: files/ansible/install-deps.yaml files/ansible/recipe-tests.yaml
5959
$(CONTAINER_ENGINE) build --rm -t $(TEST_IMAGE) -f Dockerfile.tests .
6060

61+
test_frontend:
62+
cd frontend && pnpm run test
63+
6164
check_in_container: test_image
6265
$(CONTAINER_ENGINE) run --rm \
6366
--security-opt label=disable \

frontend/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
"@tanstack/react-router": "^1.58.15",
2121
"@tanstack/router-devtools": "^1.58.15",
2222
"@tanstack/router-plugin": "^1.58.12",
23+
"@testing-library/react": "^16.0.1",
2324
"@types/node": "^22.7.4",
2425
"@types/react": "^18.3.10",
2526
"@types/react-dom": "^18.3.0",
2627
"@vitejs/plugin-react": "^4.3.2",
28+
"happy-dom": "^15.7.4",
2729
"react": "^18.3.1",
2830
"react-dom": "^18.3.1",
2931
"sharp": "^0.33.5",
@@ -40,7 +42,8 @@
4042
"start": "vite",
4143
"build": "vite build",
4244
"build:analyze": "vite-bundle-visualizer --open",
43-
"preview": "vite preview"
45+
"preview": "vite preview",
46+
"test": "vitest"
4447
},
4548
"browserslist": {
4649
"production": [
@@ -57,7 +60,8 @@
5760
"devDependencies": {
5861
"@biomejs/biome": "1.8.3",
5962
"babel-plugin-named-exports-order": "^0.0.2",
60-
"prop-types": "^15.8.1"
63+
"prop-types": "^15.8.1",
64+
"vitest": "^2.1.2"
6165
},
6266
"engines": {
6367
"node": ">=20",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright Contributors to the Packit project.
2+
// SPDX-License-Identifier: MIT
3+
import { assertType, test, vi } from "vitest";
4+
import { getHostName } from "./forgeUrls";
5+
6+
test("can get hostname from string", ({ expect }) => {
7+
expect(getHostName("https://example.com?foo=bar")).toBe("example.com");
8+
});
9+
10+
test("can get hostname from URL instance", ({ expect }) => {
11+
const host = new URL("https://example.com");
12+
expect(getHostName(host)).toBe("example.com");
13+
});
14+
15+
test("hostname logs error for invalid URL", ({ expect }) => {
16+
const consoleMock = vi
17+
.spyOn(console, "error")
18+
.mockImplementation(() => undefined);
19+
expect(getHostName("invalid url")).toBe("");
20+
21+
expect(consoleMock.mock.lastCall?.[0].toString()).toContain("Invalid URL");
22+
expect(consoleMock.mock.lastCall?.[0]).toBeInstanceOf(TypeError);
23+
});

frontend/src/components/forgeUrls.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
// SPDX-License-Identifier: MIT
33

44
// getHostName - returns the hostname if possible, otherwise an empty string
5-
function getHostName(url: string | URL) {
5+
export function getHostName(url: string | URL) {
66
let hostname = "";
77
try {
88
hostname = new URL(url).hostname;
99
} catch (error) {
10-
console.log("Invalid URL");
1110
console.error(error);
1211
}
1312
return hostname;
1413
}
1514

1615
// getPRLink - returns the PR link if possible otherwise an empty string
17-
function getPRLink(gitRepo: string, prID: number) {
16+
export function getPRLink(gitRepo: string, prID: number) {
1817
const forge = getHostName(gitRepo);
1918
switch (forge) {
2019
case "github.com":
@@ -28,7 +27,7 @@ function getPRLink(gitRepo: string, prID: number) {
2827
}
2928

3029
// getBranchLink - returns the branch link if possible otherwise an empty string
31-
function getBranchLink(gitRepo: string, branchName: string) {
30+
export function getBranchLink(gitRepo: string, branchName: string) {
3231
const forge = getHostName(gitRepo);
3332
switch (forge) {
3433
case "github.com":
@@ -42,7 +41,7 @@ function getBranchLink(gitRepo: string, branchName: string) {
4241
}
4342

4443
// getIssueLink - returns the issue link if possible otherwise an empty string
45-
function getIssueLink(gitRepo: string, issueID: number) {
44+
export function getIssueLink(gitRepo: string, issueID: number) {
4645
const forge = getHostName(gitRepo);
4746
switch (forge) {
4847
case "github.com":
@@ -56,7 +55,7 @@ function getIssueLink(gitRepo: string, issueID: number) {
5655
}
5756

5857
// getReleaseLink - returns the link to release if possible otherwise an empty string
59-
function getReleaseLink(gitRepo: string, release: string) {
58+
export function getReleaseLink(gitRepo: string, release: string) {
6059
const forge = getHostName(gitRepo);
6160
switch (forge) {
6261
case "github.com":
@@ -69,7 +68,7 @@ function getReleaseLink(gitRepo: string, release: string) {
6968
}
7069

7170
// getCommitLink - returns a link to the commit
72-
function getCommitLink(gitRepo: string, commit_hash: string) {
71+
export function getCommitLink(gitRepo: string, commit_hash: string) {
7372
const forge = getHostName(gitRepo);
7473
switch (forge) {
7574
case "github.com":
@@ -81,12 +80,3 @@ function getCommitLink(gitRepo: string, commit_hash: string) {
8180
return `${gitRepo}/-/commit/${commit_hash}`;
8281
}
8382
}
84-
85-
export {
86-
getHostName,
87-
getPRLink,
88-
getBranchLink,
89-
getIssueLink,
90-
getReleaseLink,
91-
getCommitLink,
92-
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright Contributors to the Packit project.
2+
// SPDX-License-Identifier: MIT
3+
4+
import { IceCreamIcon } from "@patternfly/react-icons";
5+
import { render } from "@testing-library/react";
6+
import { test } from "vitest";
7+
import { BaseStatusLabel } from "./BaseStatusLabel";
8+
9+
test("default label", async ({ expect }) => {
10+
const { baseElement } = render(
11+
<BaseStatusLabel
12+
color="blue"
13+
tooltipText="tooltipText"
14+
icon={<IceCreamIcon />}
15+
label="Label fsdf"
16+
/>,
17+
);
18+
expect(baseElement).toMatchSnapshot("default label");
19+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`default label > default label 1`] = `
4+
<body>
5+
<div>
6+
<div
7+
style="display: contents;"
8+
>
9+
<span
10+
style="padding: 2px;"
11+
>
12+
<span
13+
class="pf-v5-c-label pf-m-blue"
14+
>
15+
<span
16+
class="pf-v5-c-label__icon"
17+
>
18+
<svg
19+
aria-hidden="true"
20+
class="pf-v5-svg"
21+
fill="currentColor"
22+
height="1em"
23+
role="img"
24+
viewBox="0 0 448 512"
25+
width="1em"
26+
>
27+
<path
28+
d="M368 160h-.94a144 144 0 1 0-286.12 0H80a48 48 0 0 0 0 96h288a48 48 0 0 0 0-96zM195.38 493.69a31.52 31.52 0 0 0 57.24 0L352 288H96z"
29+
/>
30+
</svg>
31+
</span>
32+
<span
33+
class="pf-v5-c-label__text"
34+
>
35+
Label fsdf
36+
<span
37+
class="pf-v5-u-screen-reader"
38+
>
39+
tooltipText
40+
</span>
41+
</span>
42+
</span>
43+
</span>
44+
</div>
45+
</div>
46+
</body>
47+
`;

frontend/vite.config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright Contributors to the Packit project.
22
// SPDX-License-Identifier: MIT
3-
3+
/// <reference types="vitest" />
44
import { defineConfig } from "vite";
55
import react from "@vitejs/plugin-react";
66
import { sentryVitePlugin } from "@sentry/vite-plugin";
@@ -14,11 +14,7 @@ export default defineConfig(() => ({
1414
rollupOptions: {
1515
output: {
1616
manualChunks: function manualChunks(id) {
17-
if (
18-
id.includes("victory-") ||
19-
id.includes("d3") ||
20-
id.includes("@patternfly/react-charts")
21-
) {
17+
if (id.includes("victory-") || id.includes("d3") || id.includes("@patternfly/react-charts")) {
2218
return "charts";
2319
} else if (id.includes("@patternfly")) {
2420
return "patternfly";
@@ -46,9 +42,13 @@ export default defineConfig(() => ({
4642
authToken: process.env.SENTRY_AUTH_TOKEN,
4743
telemetry: false,
4844
reactComponentAnnotation: { enabled: true },
45+
disable: process.env.SENTRY_AUTH_TOKEN === undefined,
4946
}),
5047
],
5148
server: {
5249
open: true,
5350
},
51+
test: {
52+
environment: "happy-dom",
53+
},
5454
}));

0 commit comments

Comments
 (0)