Files
nobat724_front/components/specialties/detail/SpecialtyTree.test.js
T

91 lines
3.5 KiB
JavaScript

import { describe, expect, it } from "vitest";
import { render, screen } from "@testing-library/react";
import specialtiesData from "@/data/specialties.json";
import { childSpecialties, parentSpecialty } from "@/lib/specialtyIndexing";
import SpecialtyTree from "@/components/specialties/detail/SpecialtyTree";
const byName = (name) => specialtiesData.find((s) => s.name === name);
const GENERAL_SURGERY = byName("جراحی عمومی");
const GI = byName("جراح گوارش");
describe("childSpecialties / parentSpecialty", () => {
it("والد فرزندان فعالش را می‌دهد", () => {
const names = childSpecialties(GENERAL_SURGERY).map((s) => s.name);
expect(names).toContain("جراح گوارش");
expect(names).toContain("جراحی پلاستیک و زیبایی");
});
it("برگ فرزندی ندارد", () => {
expect(childSpecialties(GI)).toEqual([]);
});
it("زیرتخصص والدش را می‌دهد، ریشه null", () => {
expect(parentSpecialty(GI)?.id).toBe(GENERAL_SURGERY.id);
expect(parentSpecialty(GENERAL_SURGERY)).toBeNull();
});
it("ورودی تهی کرش نمی‌کند", () => {
expect(childSpecialties(null)).toEqual([]);
expect(parentSpecialty(undefined)).toBeNull();
});
it("ترتیب فرزندان پایدار است", () => {
const once = childSpecialties(GENERAL_SURGERY).map((s) => s.id);
const twice = childSpecialties(GENERAL_SURGERY).map((s) => s.id);
expect(once).toEqual(twice);
});
});
describe("SpecialtyTree", () => {
it("روی صفحهٔ والد، «همه تخصص‌های X» حالت جاری است نه لینک", () => {
render(<SpecialtyTree specialty={GENERAL_SURGERY} />);
const all = screen.getByText(`همه تخصص‌های ${GENERAL_SURGERY.name}`);
expect(all).toHaveAttribute("aria-current", "page");
expect(all.tagName).not.toBe("A");
});
it("روی صفحهٔ والد، همهٔ زیرتخصص‌ها لینک دارند", () => {
render(<SpecialtyTree specialty={GENERAL_SURGERY} />);
const link = screen.getByRole("link", { name: "جراح گوارش" });
expect(link).toHaveAttribute("href", `/specialties/${GI.slug}`);
});
it("روی صفحهٔ زیرتخصص، «همه» لینکِ بازگشت به گروه است", () => {
render(<SpecialtyTree specialty={GI} />);
const back = screen.getByRole("link", {
name: `همه تخصص‌های ${GENERAL_SURGERY.name}`,
});
expect(back).toHaveAttribute("href", `/specialties/${GENERAL_SURGERY.slug}`);
});
it("روی صفحهٔ زیرتخصص، خودش حالت جاری است و خواهرانش لینک", () => {
render(<SpecialtyTree specialty={GI} />);
expect(screen.getByText(GI.name)).toHaveAttribute("aria-current", "page");
expect(screen.getByRole("link", { name: "جراح تیروئید" })).toBeInTheDocument();
});
it("تخصص بدون خواهر و فرزند چیزی رندر نمی‌کند", () => {
const lonely = specialtiesData.find(
(s) => !s.parent_id && childSpecialties(s).length === 0
);
const { container } = render(<SpecialtyTree specialty={lonely} />);
expect(container).toBeEmptyDOMElement();
});
it("ناوبری برچسب دسترسی‌پذیر دارد", () => {
render(<SpecialtyTree specialty={GENERAL_SURGERY} />);
expect(
screen.getByRole("navigation", { name: `زیرتخصص‌های ${GENERAL_SURGERY.name}` })
).toBeInTheDocument();
});
});