33 lines
975 B
TypeScript
33 lines
975 B
TypeScript
|
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();
|
||
|
});
|
||
|
});
|