saleor-dashboard/testUtils/api.ts

72 lines
1.7 KiB
TypeScript
Raw Normal View History

import { ApolloClient, InMemoryCache } from "@apollo/client";
import { BatchHttpLink } from "@apollo/client/link/batch-http";
import { getApiUrl } from "@dashboard/config";
import { createFetch } from "@saleor/sdk";
import isCI from "is-ci";
2020-07-20 10:05:47 +00:00
import path from "path";
import { setupPolly } from "setup-polly-jest";
const POLLY_MODES = ["replay", "record", "passthrough", "stopped"] as const;
function getPollyMode() {
const env = process.env.POLLY_MODE as typeof POLLY_MODES[number];
if (!env) {
return POLLY_MODES[0]; // replay
}
if (POLLY_MODES.includes(env)) {
return env;
}
console.warn(`Unrecognised POLLY_MODE env variable value: ${env}`);
return POLLY_MODES[0]; // replay
}
function getPollyRecordIfMissing() {
const env = process.env.POLLY_RECORD_IF_MISSING;
if (!env) {
return !isCI;
}
return env === "true";
}
2020-07-20 10:05:47 +00:00
function setupApi() {
setupPolly({
adapters: [require("@pollyjs/adapter-node-http")],
matchRequestsBy: {
headers: false,
url: {
hash: false,
hostname: true,
password: false,
pathname: true,
port: false,
protocol: false,
query: false,
username: false,
},
body: false,
},
mode: getPollyMode(),
recordIfMissing: getPollyRecordIfMissing(),
persister: require("@pollyjs/persister-fs"),
2020-07-20 10:05:47 +00:00
persisterOptions: {
keepUnusedRequests: false,
2020-07-20 10:05:47 +00:00
fs: {
recordingsDir: path.resolve(__dirname, "../recordings"),
},
},
2020-07-20 10:05:47 +00:00
});
const cache = new InMemoryCache();
const link = new BatchHttpLink({
fetch: createFetch(),
uri: getApiUrl(),
2020-07-20 10:05:47 +00:00
});
const apolloClient = new ApolloClient({
cache,
link,
2020-07-20 10:05:47 +00:00
});
return apolloClient;
}
export default setupApi;