Skip to content

Commit 08515fe

Browse files
committed
test: Octokit.defaults(options)
1 parent 0bac42f commit 08515fe

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

test/defaults.test.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import fetchMock from "fetch-mock";
2+
import { getUserAgent } from "universal-user-agent";
3+
import { createActionAuth } from "@octokit/auth";
4+
5+
import { Octokit } from "../src";
6+
7+
const userAgent = `octokit-core.js/0.0.0-development ${getUserAgent()}`;
8+
9+
describe("Octokit.defaults", () => {
10+
it("is a function", () => {
11+
expect(Octokit.defaults).toBeInstanceOf(Function);
12+
});
13+
14+
it("Octokit.defaults({baseUrl})", () => {
15+
const mock = fetchMock.sandbox().getOnce(
16+
"https://github.acme-inc.test/api/v3/",
17+
{ ok: true },
18+
{
19+
headers: {
20+
accept: "application/vnd.github.v3+json",
21+
"user-agent": userAgent
22+
}
23+
}
24+
);
25+
26+
const OctokitWithDefaults = Octokit.defaults({
27+
baseUrl: "https://github.acme-inc.test/api/v3",
28+
request: {
29+
fetch: mock
30+
}
31+
});
32+
33+
const octokit = new OctokitWithDefaults();
34+
35+
return octokit.request("GET /").then(response => {
36+
expect(response.data).toStrictEqual({ ok: true });
37+
});
38+
});
39+
40+
it("Octokit.defaults({userAgent})", () => {
41+
const mock = fetchMock.sandbox().getOnce(
42+
"https://api.github.com/",
43+
{ ok: true },
44+
{
45+
headers: {
46+
accept: "application/vnd.github.v3+json",
47+
"user-agent": `my-app/v1.2.3 ${userAgent}`
48+
}
49+
}
50+
);
51+
52+
const OctokitWithDefaults = Octokit.defaults({
53+
userAgent: "my-app/v1.2.3",
54+
request: {
55+
fetch: mock
56+
}
57+
});
58+
59+
const octokit = new OctokitWithDefaults();
60+
61+
return octokit.request("GET /").then(response => {
62+
expect(response.data).toStrictEqual({ ok: true });
63+
});
64+
});
65+
66+
it("Octokit.defaults({auth})", async () => {
67+
const mock = fetchMock.sandbox().getOnce(
68+
"https://api.github.com/app",
69+
{ id: 123 },
70+
{
71+
headers: {
72+
accept: "application/vnd.github.v3+json",
73+
authorization: `token githubtoken123`,
74+
"user-agent": userAgent
75+
}
76+
}
77+
);
78+
const currentEnv = process.env;
79+
process.env = {
80+
GITHUB_ACTION: "1",
81+
GITHUB_TOKEN: "githubtoken123"
82+
};
83+
84+
const OctokitWithDefaults = Octokit.defaults({
85+
auth: createActionAuth(),
86+
request: {
87+
fetch: mock
88+
}
89+
});
90+
91+
const octokit = new OctokitWithDefaults();
92+
93+
await octokit.request("/app");
94+
process.env = currentEnv;
95+
});
96+
97+
it("Octokit.defaults().defaults()", () => {
98+
const mock = fetchMock.sandbox().getOnce(
99+
"https://github.acme-inc.test/api/v3/",
100+
{ ok: true },
101+
{
102+
headers: {
103+
accept: "application/vnd.github.v3+json",
104+
"user-agent": `my-app/v1.2.3 ${userAgent}`
105+
}
106+
}
107+
);
108+
109+
const OctokitWithDefaults = Octokit.defaults({
110+
baseUrl: "https://github.acme-inc.test/api/v3",
111+
request: {
112+
fetch: mock
113+
}
114+
}).defaults({
115+
userAgent: "my-app/v1.2.3"
116+
});
117+
118+
const octokit = new OctokitWithDefaults();
119+
120+
return octokit.request("GET /").then(response => {
121+
expect(response.data).toStrictEqual({ ok: true });
122+
});
123+
});
124+
125+
it("Octokit.plugins().defaults()", () => {
126+
const mock = fetchMock.sandbox().getOnce(
127+
"https://github.acme-inc.test/api/v3/",
128+
{ ok: true },
129+
{
130+
headers: {
131+
accept: "application/vnd.github.v3+json",
132+
"user-agent": userAgent
133+
}
134+
}
135+
);
136+
137+
const OctokitWithDefaults = Octokit.plugin(octokit => {
138+
octokit.foo = "bar";
139+
}).defaults({
140+
baseUrl: "https://github.acme-inc.test/api/v3",
141+
request: {
142+
fetch: mock
143+
}
144+
});
145+
146+
const octokit = new OctokitWithDefaults();
147+
148+
expect(octokit.foo).toEqual("bar");
149+
150+
return octokit.request("GET /").then(response => {
151+
expect(response.data).toStrictEqual({ ok: true });
152+
});
153+
});
154+
});

0 commit comments

Comments
 (0)