Skip to content

Commit 91cf47e

Browse files
committed
test: octokit.hook
1 parent 4f8aad7 commit 91cf47e

File tree

2 files changed

+217
-1
lines changed

2 files changed

+217
-1
lines changed

test/hook.test.ts

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
import getUserAgent from "universal-user-agent";
2+
import fetchMock from "fetch-mock";
3+
4+
import { Octokit } from "../src";
5+
6+
const userAgent = `octokit-core.js/0.0.0-development ${getUserAgent()}`;
7+
8+
describe("octokit.hook", () => {
9+
it("octokit.hook is a function", () => {
10+
const octokit = new Octokit();
11+
expect(octokit.hook).toBeInstanceOf(Function);
12+
});
13+
it(`octokit.hook.before is a function`, () => {
14+
const octokit = new Octokit();
15+
expect(octokit.hook.before).toBeInstanceOf(Function);
16+
});
17+
it(`octokit.hook.after is a function`, () => {
18+
const octokit = new Octokit();
19+
expect(octokit.hook.after).toBeInstanceOf(Function);
20+
});
21+
it(`octokit.hook.error is a function`, () => {
22+
const octokit = new Octokit();
23+
expect(octokit.hook.error).toBeInstanceOf(Function);
24+
});
25+
it(`octokit.hook.wrap is a function`, () => {
26+
const octokit = new Octokit();
27+
expect(octokit.hook.wrap).toBeInstanceOf(Function);
28+
});
29+
30+
it("octokit.hook.before('request')", () => {
31+
const mock = fetchMock
32+
.sandbox()
33+
.getOnce(
34+
"https://api.github.com/foo/daz/baz?qux=quux&beforeAddition=works",
35+
{ ok: true }
36+
);
37+
38+
const octokit = new Octokit({
39+
request: {
40+
fetch: mock
41+
}
42+
});
43+
44+
// We don't need to test all of before-after-hook's functionality, it's well tested itself.
45+
// But we do want to test common use cases in case we switch to a different hook implementation in future.
46+
octokit.hook.before("request", options => {
47+
expect(options).toStrictEqual({
48+
baseUrl: "https://api.github.com",
49+
method: "GET",
50+
url: "/foo/:bar/baz",
51+
headers: {
52+
accept: "application/vnd.github.v3+json",
53+
"user-agent": userAgent,
54+
"x-foo": "bar"
55+
},
56+
mediaType: {
57+
previews: ["octicon"],
58+
format: "rad"
59+
},
60+
bar: "daz",
61+
qux: "quux",
62+
request: {
63+
fetch: mock,
64+
hook: options.request.hook
65+
}
66+
});
67+
68+
// test alternating options
69+
options.beforeAddition = "works";
70+
});
71+
72+
return octokit.request("/foo/:bar/baz", {
73+
bar: "daz",
74+
qux: "quux",
75+
headers: {
76+
"x-foo": "bar"
77+
},
78+
mediaType: {
79+
previews: ["octicon"],
80+
format: "rad"
81+
}
82+
});
83+
});
84+
85+
it("octokit.hook.after('request')", async () => {
86+
const mock = fetchMock
87+
.sandbox()
88+
.getOnce("https://api.github.com/", { ok: true });
89+
90+
const octokit = new Octokit({
91+
request: {
92+
fetch: mock
93+
}
94+
});
95+
96+
octokit.hook.after("request", (response: any, requestOptions: any) => {
97+
expect(requestOptions).toStrictEqual({
98+
baseUrl: "https://api.github.com",
99+
method: "GET",
100+
url: "/",
101+
headers: {
102+
accept: "application/vnd.github.v3+json",
103+
"user-agent": userAgent
104+
},
105+
mediaType: {
106+
previews: [],
107+
format: ""
108+
},
109+
request: {
110+
fetch: mock,
111+
hook: requestOptions.request.hook
112+
}
113+
});
114+
115+
response.data.afterAddition = "works";
116+
});
117+
118+
const { data } = await octokit.request("/");
119+
120+
expect(data).toStrictEqual({
121+
ok: true,
122+
afterAddition: "works"
123+
});
124+
});
125+
126+
it("octokit.hook.error('request')", async () => {
127+
const mock = fetchMock.sandbox().getOnce("https://api.github.com/", 500);
128+
129+
const octokit = new Octokit({
130+
request: {
131+
fetch: mock
132+
}
133+
});
134+
135+
octokit.hook.error("request", (error: any, requestOptions: any) => {
136+
expect(error.status).toEqual(500);
137+
expect(requestOptions).toStrictEqual({
138+
baseUrl: "https://api.github.com",
139+
method: "GET",
140+
url: "/",
141+
headers: {
142+
accept: "application/vnd.github.v3+json",
143+
"user-agent": userAgent
144+
},
145+
mediaType: {
146+
previews: [],
147+
format: ""
148+
},
149+
request: {
150+
fetch: mock,
151+
hook: requestOptions.request.hook
152+
}
153+
});
154+
155+
return { data: { ok: true } };
156+
});
157+
158+
const { data } = await octokit.request("/");
159+
160+
expect(data).toStrictEqual({
161+
ok: true
162+
});
163+
});
164+
165+
it("octokit.hook.wrap('request')", async () => {
166+
const octokit = new Octokit();
167+
168+
octokit.hook.wrap("request", (request, options) => {
169+
expect(request).toBeInstanceOf(Function);
170+
expect(options).toStrictEqual({
171+
baseUrl: "https://api.github.com",
172+
method: "GET",
173+
url: "/",
174+
headers: {
175+
accept: "application/vnd.github.v3+json",
176+
"user-agent": userAgent
177+
},
178+
mediaType: {
179+
previews: [],
180+
format: ""
181+
},
182+
request: {
183+
hook: options.request.hook
184+
}
185+
});
186+
187+
return { data: { ok: true } };
188+
});
189+
190+
const { data } = await octokit.request("/");
191+
192+
expect(data).toStrictEqual({
193+
ok: true
194+
});
195+
});
196+
197+
it("octokit.hook()", async () => {
198+
const octokit = new Octokit();
199+
200+
let beforeMagicCalled = false;
201+
octokit.hook.before("magic", (options: any) => {
202+
beforeMagicCalled = true;
203+
});
204+
205+
await octokit.hook("magic", (options: any) => {
206+
return {
207+
magic: true
208+
};
209+
});
210+
211+
expect(beforeMagicCalled).toEqual(true);
212+
});
213+
});

test/request.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ describe("octokit.request()", () => {
129129
accept: "application/vnd.github.v3+json",
130130
"user-agent": userAgent
131131
},
132-
request: {}
132+
request: {
133+
// @ts-ignore
134+
hook: requestOptions.request.hook
135+
}
133136
});
134137
});
135138
});

0 commit comments

Comments
 (0)