Skip to content

Commit 40efab7

Browse files
committed
updates tests to use the new v12 fetch-mock apis
1 parent e99c9ee commit 40efab7

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

test/app.test.ts

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("OAuthApp.defaults", () => {
3535
describe("app", () => {
3636
it("app.getUserOctokit(options)", async () => {
3737
const mock = fetchMock
38-
.sandbox()
38+
.createInstance()
3939
.postOnce(
4040
"https://github.com/login/oauth/access_token",
4141
{
@@ -63,7 +63,7 @@ describe("app", () => {
6363

6464
const Mocktokit = OAuthAppOctokit.defaults({
6565
request: {
66-
fetch: mock,
66+
fetch: mock.fetchHandler,
6767
},
6868
});
6969

@@ -113,7 +113,7 @@ describe("app", () => {
113113

114114
it("app.createToken(options) for web flow", async () => {
115115
const mock = fetchMock
116-
.sandbox()
116+
.createInstance()
117117
.postOnce(
118118
"https://github.com/login/oauth/access_token",
119119
{
@@ -141,7 +141,7 @@ describe("app", () => {
141141

142142
const Mocktokit = OAuthAppOctokit.defaults({
143143
request: {
144-
fetch: mock,
144+
fetch: mock.fetchHandler,
145145
},
146146
});
147147

@@ -197,7 +197,7 @@ describe("app", () => {
197197

198198
it("app.createToken(options) for device flow", async () => {
199199
const mock = fetchMock
200-
.sandbox()
200+
.createInstance()
201201
.postOnce(
202202
"https://github.com/login/device/code",
203203
{
@@ -231,7 +231,7 @@ describe("app", () => {
231231

232232
const Mocktokit = OAuthAppOctokit.defaults({
233233
request: {
234-
fetch: mock,
234+
fetch: mock.fetchHandler,
235235
},
236236
});
237237

@@ -285,7 +285,7 @@ describe("app", () => {
285285
});
286286

287287
it("app.checkToken(options)", async () => {
288-
const mock = fetchMock.sandbox().postOnce(
288+
const mock = fetchMock.createInstance().postOnce(
289289
"https://api.github.com/applications/0123/token",
290290
{ id: 1 },
291291
{
@@ -301,7 +301,7 @@ describe("app", () => {
301301

302302
const Mocktokit = OAuthAppOctokit.defaults({
303303
request: {
304-
fetch: mock,
304+
fetch: mock.fetchHandler,
305305
},
306306
});
307307

@@ -334,13 +334,13 @@ describe("app", () => {
334334
"content-type": "application/json",
335335
},
336336
"status": 200,
337-
"url": "",
337+
"url": "https://api.github.com/applications/0123/token",
338338
}
339339
`);
340340
});
341341

342342
it("app.resetToken(options)", async () => {
343-
const mock = fetchMock.sandbox().patchOnce(
343+
const mock = fetchMock.createInstance().patchOnce(
344344
"https://api.github.com/applications/0123/token",
345345
{
346346
id: 2,
@@ -360,7 +360,7 @@ describe("app", () => {
360360

361361
const Mocktokit = OAuthAppOctokit.defaults({
362362
request: {
363-
fetch: mock,
363+
fetch: mock.fetchHandler,
364364
},
365365
});
366366

@@ -411,7 +411,7 @@ describe("app", () => {
411411
"content-type": "application/json",
412412
},
413413
"status": 200,
414-
"url": "",
414+
"url": "https://api.github.com/applications/0123/token",
415415
}
416416
`);
417417
expect(onTokenCallback.mock.calls.length).toEqual(1);
@@ -427,7 +427,7 @@ describe("app", () => {
427427
});
428428

429429
it("app.resetToken(options) with empty scopes", async () => {
430-
const mock = fetchMock.sandbox().patchOnce(
430+
const mock = fetchMock.createInstance().patchOnce(
431431
"https://api.github.com/applications/0123/token",
432432
{
433433
id: 2,
@@ -447,7 +447,7 @@ describe("app", () => {
447447

448448
const Mocktokit = OAuthAppOctokit.defaults({
449449
request: {
450-
fetch: mock,
450+
fetch: mock.fetchHandler,
451451
},
452452
});
453453

@@ -492,7 +492,7 @@ describe("app", () => {
492492
"content-type": "application/json",
493493
},
494494
"status": 200,
495-
"url": "",
495+
"url": "https://api.github.com/applications/0123/token",
496496
}
497497
`);
498498
expect(onTokenCallback.mock.calls.length).toEqual(1);
@@ -507,7 +507,7 @@ describe("app", () => {
507507
});
508508

509509
it("app.resetToken(options) for GitHub App", async () => {
510-
const mock = fetchMock.sandbox().patchOnce(
510+
const mock = fetchMock.createInstance().patchOnce(
511511
"https://api.github.com/applications/lv1.1234567890abcdef/token",
512512
{
513513
id: 2,
@@ -529,7 +529,7 @@ describe("app", () => {
529529

530530
const Mocktokit = OAuthAppOctokit.defaults({
531531
request: {
532-
fetch: mock,
532+
fetch: mock.fetchHandler,
533533
},
534534
});
535535

@@ -573,7 +573,7 @@ describe("app", () => {
573573
"content-type": "application/json",
574574
},
575575
"status": 200,
576-
"url": "",
576+
"url": "https://api.github.com/applications/lv1.1234567890abcdef/token",
577577
}
578578
`);
579579
expect(onTokenCallback.mock.calls.length).toEqual(1);
@@ -588,7 +588,7 @@ describe("app", () => {
588588
});
589589

590590
it("app.refreshToken(options)", async () => {
591-
const mock = fetchMock.sandbox().postOnce(
591+
const mock = fetchMock.createInstance().postOnce(
592592
"https://github.com/login/oauth/access_token",
593593
{
594594
body: {
@@ -615,7 +615,7 @@ describe("app", () => {
615615

616616
const Mocktokit = OAuthAppOctokit.defaults({
617617
request: {
618-
fetch: mock,
618+
fetch: mock.fetchHandler,
619619
},
620620
});
621621

@@ -667,7 +667,7 @@ describe("app", () => {
667667
"date": "Thu, 1 Jan 1970 00:00:00 GMT",
668668
},
669669
"status": 200,
670-
"url": "",
670+
"url": "https://github.com/login/oauth/access_token",
671671
}
672672
`);
673673
expect(onTokenCallback.mock.calls.length).toEqual(1);
@@ -699,7 +699,7 @@ describe("app", () => {
699699
});
700700

701701
it("app.scopeToken(options)", async () => {
702-
const mock = fetchMock.sandbox().postOnce(
702+
const mock = fetchMock.createInstance().postOnce(
703703
"https://api.github.com/applications/lv1.1234567890abcdef/token/scoped",
704704
{
705705
token: "token456",
@@ -720,7 +720,7 @@ describe("app", () => {
720720

721721
const Mocktokit = OAuthAppOctokit.defaults({
722722
request: {
723-
fetch: mock,
723+
fetch: mock.fetchHandler,
724724
},
725725
});
726726

@@ -771,7 +771,7 @@ describe("app", () => {
771771
"content-type": "application/json",
772772
},
773773
"status": 200,
774-
"url": "",
774+
"url": "https://api.github.com/applications/lv1.1234567890abcdef/token/scoped",
775775
}
776776
`);
777777
expect(onTokenCallback.mock.calls.length).toEqual(1);
@@ -804,7 +804,7 @@ describe("app", () => {
804804

805805
it("app.deleteToken(options)", async () => {
806806
const mock = fetchMock
807-
.sandbox()
807+
.createInstance()
808808
.deleteOnce("https://api.github.com/applications/0123/token", 204, {
809809
headers: {
810810
authorization:
@@ -813,11 +813,12 @@ describe("app", () => {
813813
body: {
814814
access_token: "token123",
815815
},
816+
matchPartialBody: true,
816817
});
817818

818819
const Mocktokit = OAuthAppOctokit.defaults({
819820
request: {
820-
fetch: mock,
821+
fetch: mock.fetchHandler,
821822
},
822823
});
823824

@@ -863,7 +864,7 @@ describe("app", () => {
863864

864865
it("app.deleteAuthorization(options)", async () => {
865866
const mock = fetchMock
866-
.sandbox()
867+
.createInstance()
867868
.deleteOnce("https://api.github.com/applications/0123/grant", 204, {
868869
headers: {
869870
authorization:
@@ -876,7 +877,7 @@ describe("app", () => {
876877

877878
const Mocktokit = OAuthAppOctokit.defaults({
878879
request: {
879-
fetch: mock,
880+
fetch: mock.fetchHandler,
880881
},
881882
});
882883

@@ -930,7 +931,7 @@ describe("app", () => {
930931

931932
it('app.on("token.created", ({ octokit }) => octokit.auth({ type: "reset" })', async () => {
932933
const mock = fetchMock
933-
.sandbox()
934+
.createInstance()
934935
.postOnce(
935936
"https://github.com/login/oauth/access_token",
936937
{
@@ -946,18 +947,23 @@ describe("app", () => {
946947
},
947948
},
948949
)
949-
.deleteOnce((url, options) => {
950-
expect(url).toEqual("https://api.github.com/applications/0123/token");
951-
// @ts-expect-error options.headers is not guaranteed to exist
952-
expect(options.headers.authorization).toEqual(
950+
.deleteOnce((request: any) => {
951+
expect(request.url).toEqual(
952+
"https://api.github.com/applications/0123/token",
953+
);
954+
const authorization = request.options.headers["authorization"];
955+
expect(authorization).toEqual(
953956
"basic " + Buffer.from("0123:0123secret").toString("base64"),
954957
);
958+
// expect(request.headers["authorization"]).toEqual(
959+
// "basic " + Buffer.from("0123:0123secret").toString("base64"),
960+
// );
955961
return true;
956962
}, {});
957963

958964
const Mocktokit = OAuthAppOctokit.defaults({
959965
request: {
960-
fetch: mock,
966+
fetch: mock.fetchHandler,
961967
},
962968
});
963969

@@ -984,12 +990,12 @@ describe("app", () => {
984990

985991
await app.createToken({ code: "code123" });
986992

987-
expect(mock.done()).toEqual(true);
993+
expect(mock.callHistory.done()).toBe(true);
988994
});
989995

990996
it("app.on multiple events", async () => {
991997
const mock = fetchMock
992-
.sandbox()
998+
.createInstance()
993999
.postOnce(
9941000
"https://github.com/login/oauth/access_token",
9951001
{
@@ -1017,7 +1023,7 @@ describe("app", () => {
10171023

10181024
const Mocktokit = OAuthAppOctokit.defaults({
10191025
request: {
1020-
fetch: mock,
1026+
fetch: mock.fetchHandler,
10211027
},
10221028
});
10231029

0 commit comments

Comments
 (0)