import { CookieUtils } from "./cookie-utils"; let cookie: string; beforeAll(() => { Object.defineProperty(document, "cookie", { get: jest.fn().mockImplementation(() => cookie), set: jest.fn().mockImplementation((value) => (cookie += ";" + value)), }); }); beforeEach(() => { cookie = ""; document.domain = "busca.havaianas.com.br"; }); test("set cookie should create a cookie", () => { expect(document.cookie).not.toContain("hello=world"); CookieUtils.set("hello", "world"); expect(document.cookie).toContain("hello=world"); }); test("get cookie should get cookies that exists", () => { expect(document.cookie).not.toContain("hello=world"); expect(CookieUtils.get("hello")).toBeUndefined(); CookieUtils.set("hello", "world"); expect(CookieUtils.get("hello")).toEqual("world"); expect(CookieUtils.get("world")).toBeUndefined(); CookieUtils.set("world", "!!!"); expect(CookieUtils.get("hello")).toEqual("world"); expect(CookieUtils.get("world")).toEqual("!!!"); }); test("getDomain should get only the main domain", () => { expect(CookieUtils.getDomain()).toEqual("havaianas.com.br"); }); test("getDomain should get only the main domain for country code TLDs", () => { document.domain = "busca.example.eu"; expect(CookieUtils.getDomain()).toEqual("example.eu"); });