Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Update tests as per PR review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
melissaluu committed Feb 28, 2024
1 parent b2f29ae commit 8c85639
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fetchMock from "jest-fetch-mock";

import { GraphQLClient } from "../../types";
import {
SDK_VARIANT_HEADER,
SDK_VERSION_HEADER,
DEFAULT_CLIENT_VERSION,
DEFAULT_SDK_VARIANT,
} from "../../constants";

import { operation, variables, clientConfig, getValidClient } from "./fixtures";
import {
operation,
variables,
clientConfig,
getValidClient,
defaultHeaders,
} from "./fixtures";
import {
fetchApiTests,
parametersTests,
Expand Down Expand Up @@ -136,11 +136,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
];

Expand Down Expand Up @@ -266,11 +262,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
];

Expand Down Expand Up @@ -335,11 +327,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
],
},
Expand Down Expand Up @@ -657,11 +645,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
];

Expand Down Expand Up @@ -802,11 +786,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
];

Expand Down Expand Up @@ -872,11 +852,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import fetchMock from "jest-fetch-mock";

import { GraphQLClient, ClientStreamResponse } from "../../types";
import {
SDK_VARIANT_HEADER,
SDK_VERSION_HEADER,
DEFAULT_CLIENT_VERSION,
DEFAULT_SDK_VARIANT,
} from "../../constants";

import {
clientConfig,
getValidClient,
createIterableResponse,
createIterableBufferResponse,
createReaderStreamResponse,
defaultHeaders,
} from "./fixtures";
import {
fetchApiTests,
Expand Down Expand Up @@ -1186,11 +1181,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
];

Expand Down Expand Up @@ -1352,11 +1343,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
];

Expand Down Expand Up @@ -1430,11 +1417,7 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
headers: defaultHeaders,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import {
DEFAULT_SDK_VARIANT,
} from "../../constants";

import { operation, variables, clientConfig, getValidClient } from "./fixtures";
import {
operation,
variables,
clientConfig,
getValidClient,
defaultHeaders,
} from "./fixtures";

type ClientFunctionNames = keyof Omit<GraphQLClient, "config">;

const defaultHeaders = {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
};

export const fetchApiTests = (
functionName: ClientFunctionNames,
gqlOperation: string = operation,
Expand Down Expand Up @@ -252,6 +252,34 @@ export const sdkHeadersTests = (
});
});

it("includes the function parameter custom SDK variant and version headers if both function parameter and client init config includes SDK headers", async () => {
const initCustomHeaders = {
[SDK_VARIANT_HEADER]: "custom-client",
[SDK_VERSION_HEADER]: "0.0.1",
};

const customHeaders = {
[SDK_VARIANT_HEADER]: "custom-client-1",
[SDK_VERSION_HEADER]: "0.0.2",
};

client = getValidClient({
headers: initCustomHeaders,
});

await client[functionName](gqlOperation, { headers: customHeaders });
expect(fetchMock).toHaveBeenCalledWith(clientConfig.url, {
method: "POST",
headers: {
...clientConfig.headers,
...customHeaders,
},
body: JSON.stringify({
query: gqlOperation,
}),
});
});

it("does not includes the default SDK variant and version headers if an SDK variant was provided in the function parameter headers", async () => {
const customHeaders = {
[SDK_VARIANT_HEADER]: "custom-client",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import {
ClientOptions,
Headers as TypesHeaders,
} from "../../types";
import {
SDK_VARIANT_HEADER,
SDK_VERSION_HEADER,
DEFAULT_CLIENT_VERSION,
DEFAULT_SDK_VARIANT,
} from "../../constants";

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as any;
Expand All @@ -33,6 +39,12 @@ export const variables = {
country: "US",
};

export const defaultHeaders = {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
};

export function getValidClient({
retries,
logger,
Expand Down

0 comments on commit 8c85639

Please sign in to comment.