import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import { ErrorState, TableSkeleton } from "./states";
describe("ErrorState", () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.spyOn(Element.prototype, "scrollIntoView").mockImplementation(() => {});
});
it("scrolls itself into view on mount", () => {
render();
expect(Element.prototype.scrollIntoView).toHaveBeenCalledWith({ block: "nearest" });
});
it("scrolls itself into view again when the message changes", () => {
const { rerender } = render();
expect(Element.prototype.scrollIntoView).toHaveBeenCalledTimes(1);
rerender();
expect(Element.prototype.scrollIntoView).toHaveBeenCalledTimes(2);
});
it("does not scroll again on a rerender with the same message", () => {
const { rerender } = render();
expect(Element.prototype.scrollIntoView).toHaveBeenCalledTimes(1);
rerender();
expect(Element.prototype.scrollIntoView).toHaveBeenCalledTimes(1);
});
});
describe("TableSkeleton", () => {
it("renders a labelled status block of rows x columns bars", () => {
const { container } = render();
expect(screen.getByRole("status")).toHaveTextContent("Loading");
expect(container.querySelectorAll('[data-slot="skeleton"]')).toHaveLength(6);
});
});