import { act } from "@testing-library/react";
import { hydrateRoot } from "react-dom/client";
import { renderToString } from "react-dom/server";
import { FiscalNotice } from "./FiscalNotice";
import { isFiscalNoticeActive } from "./fiscal-notice";
describe("isFiscalNoticeActive", () => {
it("shows the notice up to 2027-03-31 in BrasÃlia time and hides it from 2027-04-01 on", () => {
expect(isFiscalNoticeActive(new Date("2026-11-09T12:00:00-03:00"))).toBe(
true,
);
expect(isFiscalNoticeActive(new Date("2027-03-31T23:59:59-03:00"))).toBe(
true,
);
expect(isFiscalNoticeActive(new Date("2027-04-01T00:00:00-03:00"))).toBe(
false,
);
expect(isFiscalNoticeActive(new Date("2027-06-01T12:00:00-03:00"))).toBe(
false,
);
});
});
describe("FiscalNotice", () => {
afterEach(() => {
jest.useRealTimers();
jest.restoreAllMocks();
});
it("path: HTML built before the end, opened after it -> hydrates with no warning, then the notice goes away", async () => {
const html = renderToString();
expect(html).toContain("Seu sistema emite nota fiscal?");
const container = document.createElement("div");
container.innerHTML = html;
document.body.append(container);
const errors = jest.spyOn(console, "error").mockImplementation(() => {});
jest.useFakeTimers({ now: new Date("2027-04-02T10:00:00-03:00") });
await act(async () => {
hydrateRoot(container, , {
onRecoverableError: (error) => console.error(error),
});
});
expect(errors).not.toHaveBeenCalled();
expect(container.textContent).toBe("");
container.remove();
});
it("path: hydrated within the period -> the notice stays", async () => {
jest.useFakeTimers({ now: new Date("2027-01-10T10:00:00-03:00") });
const html = renderToString();
const container = document.createElement("div");
container.innerHTML = html;
document.body.append(container);
const errors = jest.spyOn(console, "error").mockImplementation(() => {});
await act(async () => {
hydrateRoot(container, , {
onRecoverableError: (error) => console.error(error),
});
});
expect(errors).not.toHaveBeenCalled();
expect(container.textContent).toContain("Ver o que muda");
container.remove();
});
});