Skip to content

Commit 8f996a7

Browse files
committed
test(utils): add tests for createTokenPromise
1 parent e09d4d9 commit 8f996a7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { createTokenPromise } from "../../src/utils/create-token-promise";
3+
4+
vi.mock("../../src/utils/get-superuser-token");
5+
6+
describe("createTokenPromise", async () => {
7+
const superuserToken = "superuser-token";
8+
9+
const gst = await import("../../src/utils/get-superuser-token");
10+
11+
beforeEach(() => {
12+
gst.getSuperuserToken = vi.fn().mockResolvedValue(superuserToken);
13+
});
14+
15+
it("should return impersonate token when provided", async () => {
16+
const impersonateToken = "impersonate-token";
17+
18+
const token = await createTokenPromise({
19+
superuserCredentials: { impersonateToken },
20+
url: "doesnt-matter"
21+
});
22+
23+
expect(token).toBe(impersonateToken);
24+
expect(gst.getSuperuserToken).not.toHaveBeenCalled();
25+
});
26+
27+
it("should return superuser token when email and password are provided", async () => {
28+
const token = await createTokenPromise({
29+
superuserCredentials: {
30+
email: "doesnt-matter",
31+
password: "doesnt-matter"
32+
},
33+
url: "doesnt-matter"
34+
});
35+
36+
expect(token).toBe(superuserToken);
37+
expect(gst.getSuperuserToken).toHaveBeenCalledWith("doesnt-matter", {
38+
email: "doesnt-matter",
39+
password: "doesnt-matter"
40+
});
41+
});
42+
43+
it("should return undefined when no credentials are provided", async () => {
44+
const token = await createTokenPromise({
45+
url: "doesnt-matter"
46+
});
47+
48+
expect(token).toBeUndefined();
49+
expect(gst.getSuperuserToken).not.toHaveBeenCalled();
50+
});
51+
});

0 commit comments

Comments
 (0)