Skip to content

Commit

Permalink
don't mock api
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 committed Oct 30, 2024
1 parent 895b0df commit 97c0b62
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
4 changes: 1 addition & 3 deletions packages/sdk/test/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,4 @@ export const BLOCK_NUMBER_MAINNET = BigInt(21020558);
export const BLOCK_NUMBER_ARBITRUM = BigInt(266447962);

export const TENDERLY_KEY = getMaybeEnv("VITE_TENDERLY_KEY");
export const MOCK_API = getMaybeEnv("VITE_MOCK_API")
? Boolean(getMaybeEnv("VITE_MOCK_API"))
: true;
export const MOCK_API = getMaybeEnv("VITE_MOCK_API") === "true";
73 changes: 35 additions & 38 deletions packages/sdk/test/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,39 @@ import {
usdcMainnetArbitrumFees as fees,
} from "./data";
import { getCurrentTimeSeconds } from "../../src";
import { MOCK_API } from "../common/constants";

export const handlers = MOCK_API
? [
// getAvailableRoutes
http.get(`${TEST_BASE_URL}/available-routes`, async ({ request }) => {
const url = new URL(request.url);
console.log(url);
return HttpResponse.json(route);
}),
// getSupportedChains
http.get(`${TEST_BASE_URL}/chains`, async ({ request }) => {
const url = new URL(request.url);
const givenChainIds = new Set(
url.searchParams.getAll("chainId").map(Number),
);
const expectedChainIds = new Set(
MAINNET_SUPPORTED_CHAINS.map((chain) => chain.id),
);
// check if client has in-fact fetched chain info for ALL chains, if not we force test to fail downstream
if (givenChainIds.size !== expectedChainIds.size) {
return HttpResponse.json({
status: 500,
data: undefined,
});
}
return HttpResponse.json(mainnetChainInfo);
}),
// getSuggestedFees
http.get(`${TEST_BASE_URL}/suggested-fees`, async ({ request }) => {
console.log(request.url);
const data = {
...fees,
timestamp: getCurrentTimeSeconds(),
};
return HttpResponse.json(data);
}),
]
: [];
export const handlers = [
// getAvailableRoutes
http.get(`${TEST_BASE_URL}/available-routes`, async ({ request }) => {
const url = new URL(request.url);
console.log(url);
return HttpResponse.json(route);
}),
// getSupportedChains
http.get(`${TEST_BASE_URL}/chains`, async ({ request }) => {
const url = new URL(request.url);
const givenChainIds = new Set(
url.searchParams.getAll("chainId").map(Number),
);
const expectedChainIds = new Set(
MAINNET_SUPPORTED_CHAINS.map((chain) => chain.id),
);
// check if client has in-fact fetched chain info for ALL chains, if not we force test to fail downstream
if (givenChainIds.size !== expectedChainIds.size) {
return HttpResponse.json({
status: 500,
data: undefined,
});
}
return HttpResponse.json(mainnetChainInfo);
}),
// getSuggestedFees
http.get(`${TEST_BASE_URL}/suggested-fees`, async ({ request }) => {
console.log(request.url);
const data = {
...fees,
timestamp: getCurrentTimeSeconds(),
};
return HttpResponse.json(data);
}),
];
7 changes: 6 additions & 1 deletion packages/sdk/test/mocks/setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { setupServer } from "msw/node";
import { handlers } from "./handlers";
import { afterAll, afterEach, beforeAll } from "vitest";
import { MOCK_API } from "../common/constants";

const server = setupServer(...handlers);

// set up
beforeAll(() => server.listen({ onUnhandledRequest: "bypass" }));
beforeAll(() => {
if (MOCK_API) {
server.listen({ onUnhandledRequest: "bypass" });
}
});

// tear down
afterAll(() => server.close());
Expand Down

0 comments on commit 97c0b62

Please sign in to comment.