Skip to content

Commit

Permalink
fix: set defaults on .endpoint (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 13, 2022
1 parent dd7ce12 commit 024d946
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/with-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export function withDefaults(

return Object.assign(newApi, {
defaults: withDefaults.bind(null, newRequest),
endpoint: Request.endpoint,
endpoint: newRequest.endpoint,
});
}
60 changes: 60 additions & 0 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fetchMock from "fetch-mock";
import { getUserAgent } from "universal-user-agent";

import { VERSION } from "../src/version";
import { graphql } from "../src";

const userAgent = `octokit-graphql.js/${VERSION} ${getUserAgent()}`;

describe("graphql.defaults()", () => {
it("is a function", () => {
expect(graphql.defaults).toBeInstanceOf(Function);
Expand Down Expand Up @@ -147,4 +151,60 @@ describe("graphql.defaults()", () => {
}
}`);
});

it("set defaults on .endpoint", () => {
const mockData = {
repository: {
issues: {
edges: [
{
node: {
title: "Foo",
},
},
{
node: {
title: "Bar",
},
},
{
node: {
title: "Baz",
},
},
],
},
},
};

const authenticatedGraphql = graphql.defaults({
headers: {
authorization: `token secret123`,
},
request: {
fetch: fetchMock.sandbox().post(
"https://github.acme-inc.com/api/graphql",
{ data: mockData },
{
headers: {
authorization: "token secret123",
},
}
),
},
});

const { request: _request, ...requestOptions } =
// @ts-expect-error - TODO: expects to set { url } but it really shouldn't
authenticatedGraphql.endpoint();
expect(requestOptions).toStrictEqual({
method: "POST",
url: "https://api.github.com/graphql",
headers: {
accept: "application/vnd.github.v3+json",
authorization: "token secret123",
"user-agent": userAgent,
},
});
});
});

0 comments on commit 024d946

Please sign in to comment.