Skip to content

Commit c7418fe

Browse files
committed
feat: implement unit test
1 parent e2b25b4 commit c7418fe

File tree

7 files changed

+75
-5
lines changed

7 files changed

+75
-5
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"lucide-react": "^0.451.0",
4040
"next": "14.2.5",
4141
"next-intl": "^3.17.6",
42+
"next-router-mock": "^0.9.13",
4243
"next-themes": "^0.3.0",
4344
"react": "^18",
4445
"react-dom": "^18",

src/components/common/LocaleToggle/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use client";
12
import { useTransition } from "react";
23
import { useLocale } from "next-intl";
34
import { useParams } from "next/navigation";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { render, screen } from "@testing-library/react";
3+
import { NextIntlClientProvider } from "next-intl";
4+
import Navbar from ".";
5+
6+
const messages = {
7+
Layout: {
8+
navbar: {
9+
"link-1": "About Us",
10+
"link-2": "Events",
11+
"link-3": "Donate",
12+
},
13+
},
14+
Announcement: {
15+
"pdd-2024": "🚀 Hi there!...",
16+
},
17+
};
18+
19+
vi.mock("@/lib/navigation", () => ({
20+
usePathname: vi.fn(),
21+
useRouter: vi.fn(),
22+
Link: vi.fn(),
23+
}));
24+
25+
describe("Navbar", () => {
26+
it("renders path link correctly", () => {
27+
render(
28+
<NextIntlClientProvider timeZone="Europe/London" locale="en" messages={messages}>
29+
<Navbar />
30+
</NextIntlClientProvider>
31+
);
32+
33+
const link = screen.getByRole("link");
34+
expect(link).toBeInTheDocument();
35+
});
36+
37+
it("renders the announcement correctly", () => {
38+
render(
39+
<NextIntlClientProvider timeZone="Europe/London" locale="en" messages={messages}>
40+
<Navbar />
41+
</NextIntlClientProvider>
42+
);
43+
44+
const annnouncement = screen.getByRole("heading", { level: 2 });
45+
expect(annnouncement).toHaveTextContent("Hi there!...");
46+
});
47+
});

src/components/layout/Navbar/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import IconHmcTextDark from "@/components/icon/IconHmcTextDark";
1313
const Navbar = () => {
1414
const t = useTranslations("Layout");
1515
return (
16-
<header className="sticky w-full z-50 top-0">
16+
<header aria-label="navbar" className="sticky w-full z-50 top-0">
1717
<AnnouncementLayout />
1818
<div className="border-b bg-white dark:bg-slate-950">
1919
<div className="container mx-auto py-5">

src/components/ui/Announcement/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Announcement({ message, url }: AnnouncementProps) {
2121
role="alert"
2222
className="bg-slate-950 flex justify-center items-center gap-2 text-white text-center px-4 py-2 text-sm md:text-normal"
2323
>
24-
{messageEl}
24+
<h2>{messageEl}</h2>
2525
<button onClick={() => setHidden(true)} className="bg-slate-500 rounded-full p-1 ">
2626
<X className="h-4 w-4" />
2727
<span className="sr-only">Close</span>

src/lib/navigation/NavLink.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function NavLink<Pathname extends keyof typeof pathnames>({ href, onClick, ...re
1010
const path = usePathname();
1111
return (
1212
<Link href={href} onClick={onClick}>
13-
<span className={path.includes(href as string) ? "text-hmc-base-blue" : "text-hmc-base-darkblue"}>
13+
<span className={path?.includes(href as string) ? "text-hmc-base-blue" : "text-hmc-base-darkblue"}>
1414
{rest.title}
1515
</span>
1616
</Link>

yarn.lock

+23-2
Original file line numberDiff line numberDiff line change
@@ -3733,6 +3733,11 @@ next-intl@^3.17.6:
37333733
negotiator "^0.6.3"
37343734
use-intl "^3.19.1"
37353735

3736+
next-router-mock@^0.9.13:
3737+
version "0.9.13"
3738+
resolved "https://registry.yarnpkg.com/next-router-mock/-/next-router-mock-0.9.13.tgz#bdee2011ea6c09e490121c354ef917f339767f72"
3739+
integrity sha512-906n2RRaE6Y28PfYJbaz5XZeJ6Tw8Xz1S6E31GGwZ0sXB6/XjldD1/2azn1ZmBmRk5PQRkzjg+n+RHZe5xQzWA==
3740+
37363741
next-themes@^0.3.0:
37373742
version "0.3.0"
37383743
resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.3.0.tgz#b4d2a866137a67d42564b07f3a3e720e2ff3871a"
@@ -4465,7 +4470,16 @@ string-argv@~0.3.2:
44654470
resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.2.tgz#2b6d0ef24b656274d957d54e0a4bbf6153dc02b6"
44664471
integrity sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==
44674472

4468-
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0:
4473+
"string-width-cjs@npm:string-width@^4.2.0":
4474+
version "4.2.3"
4475+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
4476+
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
4477+
dependencies:
4478+
emoji-regex "^8.0.0"
4479+
is-fullwidth-code-point "^3.0.0"
4480+
strip-ansi "^6.0.1"
4481+
4482+
string-width@^4.1.0:
44694483
version "4.2.3"
44704484
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
44714485
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -4554,7 +4568,14 @@ string.prototype.trimstart@^1.0.8:
45544568
define-properties "^1.2.1"
45554569
es-object-atoms "^1.0.0"
45564570

4557-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
4571+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
4572+
version "6.0.1"
4573+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
4574+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
4575+
dependencies:
4576+
ansi-regex "^5.0.1"
4577+
4578+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
45584579
version "6.0.1"
45594580
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
45604581
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==

0 commit comments

Comments
 (0)