react-hooks/tests/useLink.test.ts

33 lines
975 B
TypeScript
Raw Normal View History

2023-10-31 03:20:24 +02:00
import { renderHook, act } from "@testing-library/react-hooks";
import { useLink } from "../src";
describe("useLink", () => {
it("should create a link element in the document head", () => {
const href = "https://example.com";
const rel = "stylesheet";
renderHook(() => useLink(href, rel));
const linkElement = document.querySelector(`link[href="${href}"][rel="${rel}"]`);
expect(linkElement).not.toBeNull();
});
it("should remove the link element from the document head when unmounted", () => {
const href = "https://example.com";
const rel = "stylesheet";
const { unmount } = renderHook(() => useLink(href, rel));
const linkElement = document.querySelector(`link[href="${href}"][rel="${rel}"]`);
expect(linkElement).not.toBeNull();
act(() => {
unmount();
});
const removedLinkElement = document.querySelector(`link[href="${href}"][rel="${rel}"]`);
expect(removedLinkElement).toBeNull();
});
});