Skip to content

Commit

Permalink
test(cache): cover restorePreviousSession
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgrasset committed Nov 22, 2023
1 parent 6b94440 commit 26ae922
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/helpers/auth/__tests__/restore-previous-session.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Scraper } from "@the-convocation/twitter-scraper";
import { Cookie } from "tough-cookie";

import * as cookies from "../../cookies/get-cookies.js";
import { restorePreviousSession } from "../restore-previous-session.js";

const getCookiesMock = jest.spyOn(cookies, "getCookies");
const setCookiesMock = jest.fn();

jest.mock("../../../constants.js", () => {
return {};
});

const cookieMock = {
key: "cookie_id",
value: "value",
expires: new Date("2024-09-14T12:39:32.000Z"),
maxAge: 34214400,
domain: "twitter.com",
path: "/",
secure: true,
hostOnly: false,
creation: new Date("2023-11-04T11:15:28.230Z"),
lastAccessed: new Date("2023-11-04T11:15:29.203Z"),
sameSite: "none",
} as Cookie;

describe("restorePreviousSession", () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe("when cookies are available", () => {
beforeEach(() => {
getCookiesMock.mockResolvedValueOnce([cookieMock]);
});

it("should restore previous session", async () => {
await restorePreviousSession({
setCookies: setCookiesMock,
} as unknown as Scraper);

expect(setCookiesMock).toHaveBeenCalledTimes(1);
expect(setCookiesMock).toHaveBeenCalledWith([cookieMock]);
});
});

describe("when cookies are not available", () => {
const originalConsole = console.log;
const consoleMock = jest.fn();
beforeEach(() => {
getCookiesMock.mockResolvedValueOnce(null);
console.log = consoleMock;
});

afterAll(() => {
console.log = originalConsole;
});

it("should not restore previous session", async () => {
await restorePreviousSession({
setCookies: setCookiesMock,
} as unknown as Scraper);

expect(setCookiesMock).not.toHaveBeenCalled();
expect(consoleMock).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit 26ae922

Please sign in to comment.