Skip to content

Commit 3f5dc81

Browse files
committed
test(utilities): add more utilities tests
* also the components BeforeLogin and CollectionArchive
1 parent 6a0fd4d commit 3f5dc81

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed

src/components/BeforeLogin/test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
import { render, screen } from "@testing-library/react";
3+
import BeforeLogin from ".";
4+
5+
describe("component", () => {
6+
describe("BeforeLogin", () => {
7+
it("should render correctly", () => {
8+
render(<BeforeLogin />);
9+
10+
expect(
11+
screen.getByText("Welcome to your dashboard!"),
12+
).toBeInTheDocument();
13+
});
14+
});
15+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
import { render } from "@testing-library/react";
3+
import { CollectionArchive } from ".";
4+
5+
describe("component", () => {
6+
describe("CollectionArchive", () => {
7+
it("should render correctly", () => {
8+
const { container } = render(<CollectionArchive posts={[]} />);
9+
10+
expect(container.querySelector(".container")).toBeInTheDocument();
11+
});
12+
});
13+
});

src/utilities/can-use-dom/node.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @jest-environment node
33
*/
4+
45
import { describe, expect, it } from "@jest/globals";
56
import canUseDOM from ".";
67

src/utilities/generate-meta/test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, it, jest } from "@jest/globals";
2+
import { generateMeta } from ".";
3+
4+
jest.mock("~/utilities/get-url", () => ({
5+
__esModule: true,
6+
getServerSideURL: jest.fn(() => "http://localhost:3000"),
7+
}));
8+
9+
describe("utilities", () => {
10+
describe("generateMeta", () => {
11+
it("null doc", async () => {
12+
expect(
13+
await generateMeta({
14+
doc: null,
15+
}),
16+
).toStrictEqual({
17+
description: undefined,
18+
openGraph: {
19+
description: "",
20+
images: [
21+
{
22+
url: "http://localhost:3000/website-template-OG.webp",
23+
},
24+
],
25+
siteName: "Payload Website Template",
26+
title: "Payload Website Template",
27+
type: "website",
28+
url: "/",
29+
},
30+
title: "Payload Website Template",
31+
});
32+
});
33+
});
34+
});

src/utilities/get-url/node.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @jest-environment node
33
*/
4+
45
import { afterEach, describe, expect, it } from "@jest/globals";
56
import { getClientSideURL, getServerSideURL } from ".";
67

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, expect, it, jest } from "@jest/globals";
2+
import { mergeOpenGraph } from ".";
3+
4+
jest.mock("~/utilities/get-url", () => ({
5+
__esModule: true,
6+
getServerSideURL: jest.fn(() => "http://localhost:3000"),
7+
}));
8+
9+
describe("utilities", () => {
10+
describe("mergeOpenGraph", () => {
11+
it("no arguments", () => {
12+
expect(mergeOpenGraph()).toStrictEqual({
13+
type: "website",
14+
description: "An open-source website built with Payload and Next.js.",
15+
images: [
16+
{
17+
url: "http://localhost:3000/website-template-OG.webp",
18+
},
19+
],
20+
siteName: "Payload Website Template",
21+
title: "Payload Website Template",
22+
});
23+
});
24+
25+
it("with arguments", () => {
26+
expect(
27+
mergeOpenGraph({
28+
description: "Custom description",
29+
}),
30+
).toEqual(
31+
expect.objectContaining({
32+
description: "Custom description",
33+
}),
34+
);
35+
});
36+
37+
expect(
38+
mergeOpenGraph({
39+
description: "Custom description",
40+
}),
41+
).toEqual(
42+
expect.not.objectContaining({
43+
description: "An open-source website built with Payload and Next.js",
44+
}),
45+
);
46+
47+
it("with images arguments", () => {
48+
expect(
49+
mergeOpenGraph({
50+
images: [
51+
{
52+
url: "http://localhost:3000/custom-image.webp",
53+
},
54+
],
55+
}),
56+
).toEqual(
57+
expect.objectContaining({
58+
images: [
59+
{
60+
url: "http://localhost:3000/custom-image.webp",
61+
},
62+
],
63+
}),
64+
);
65+
});
66+
67+
expect(
68+
mergeOpenGraph({
69+
images: [
70+
{
71+
url: "http://localhost:3000/custom-image.webp",
72+
},
73+
],
74+
}),
75+
).toEqual(
76+
expect.not.objectContaining({
77+
images: [
78+
{
79+
url: "http://localhost:3000/website-template-OG.webp",
80+
},
81+
],
82+
}),
83+
);
84+
});
85+
});

src/utilities/ui/test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
import { cn } from ".";
3+
4+
describe("utilities", () => {
5+
describe("cn", () => {
6+
it("works normally", () => {
7+
expect(cn("bg-red hover:bg-dark-red px-2 py-1", "bg-[#B91C1C] p-3")).toBe(
8+
"hover:bg-dark-red bg-[#B91C1C] p-3",
9+
);
10+
});
11+
12+
it("removes undefined", () => {
13+
expect(cn("px-2", undefined)).toBe("px-2");
14+
});
15+
});
16+
});

src/utilities/use-debounce/test.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { describe, expect, it, jest } from "@jest/globals";
2+
import { act, renderHook } from "@testing-library/react";
3+
import { useDebounce } from ".";
4+
5+
describe("utilities", () => {
6+
describe("useDebounce", () => {
7+
jest.useFakeTimers();
8+
9+
it("should return the initial value immediately", () => {
10+
const initialValue = "initial";
11+
12+
const { result } = renderHook(() => useDebounce(initialValue));
13+
14+
expect(result.current).toBe(initialValue);
15+
});
16+
17+
it("should update the debounced value after the specified delay", () => {
18+
const initialValue = "initial";
19+
const newValue = "updated";
20+
const delay = 500;
21+
22+
const { result, rerender } = renderHook(
23+
({ value }) => useDebounce(value, delay),
24+
{ initialProps: { value: initialValue } },
25+
);
26+
27+
expect(result.current).toBe(initialValue);
28+
29+
rerender({ value: newValue });
30+
31+
expect(result.current).toBe(initialValue);
32+
33+
act(() => {
34+
jest.advanceTimersByTime(delay);
35+
});
36+
37+
expect(result.current).toBe(newValue);
38+
});
39+
40+
it("should not update if the value changes within the delay", () => {
41+
const initialValue = "initial";
42+
const intermediateValue = "intermediate";
43+
const finalValue = "final";
44+
const delay = 300;
45+
46+
const { result, rerender } = renderHook(
47+
({ value }) => useDebounce(value, delay),
48+
{ initialProps: { value: initialValue } },
49+
);
50+
51+
expect(result.current).toBe(initialValue);
52+
53+
rerender({ value: intermediateValue });
54+
55+
act(() => {
56+
jest.advanceTimersByTime(delay / 2);
57+
});
58+
59+
expect(result.current).toBe(initialValue);
60+
61+
rerender({ value: finalValue });
62+
63+
act(() => {
64+
jest.advanceTimersByTime(delay / 2);
65+
});
66+
67+
expect(result.current).not.toBe(intermediateValue);
68+
69+
act(() => {
70+
jest.advanceTimersByTime(delay / 2);
71+
});
72+
73+
expect(result.current).toBe(finalValue);
74+
});
75+
76+
it("should use the default delay if none is provided", () => {
77+
const initialValue = "initial";
78+
const newValue = "updated";
79+
const defaultDelay = 200;
80+
81+
const { result, rerender } = renderHook(
82+
({ value }) => useDebounce(value),
83+
{
84+
initialProps: { value: initialValue },
85+
},
86+
);
87+
88+
expect(result.current).toBe(initialValue);
89+
90+
rerender({ value: newValue });
91+
92+
act(() => {
93+
jest.advanceTimersByTime(defaultDelay);
94+
});
95+
96+
expect(result.current).toBe(newValue);
97+
});
98+
99+
it("should clear the timeout on unmount", () => {
100+
const initialValue = "initial";
101+
const newValue = "updated";
102+
const delay = 400;
103+
104+
const { result, rerender, unmount } = renderHook(
105+
({ value, delay }) => useDebounce(value, delay),
106+
{ initialProps: { value: initialValue, delay } },
107+
);
108+
109+
expect(result.current).toBe(initialValue);
110+
111+
rerender({ value: newValue, delay });
112+
113+
unmount();
114+
115+
act(() => {
116+
jest.advanceTimersByTime(delay);
117+
});
118+
119+
expect(result.current).toBe(initialValue);
120+
});
121+
});
122+
});

0 commit comments

Comments
 (0)