Skip to content

Commit

Permalink
Merge branch 'vitest-tests' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Oct 24, 2024
2 parents 9b88dd6 + ffe2253 commit c218921
Show file tree
Hide file tree
Showing 7 changed files with 8,609 additions and 11,342 deletions.
6 changes: 3 additions & 3 deletions functions/post-order.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionReceipt, TransactionResponse } from "@ethersproject/providers";
import { JsonRpcProvider } from "@ethersproject/providers/lib/json-rpc-provider";
import { JsonRpcProvider, TransactionReceipt, TransactionResponse } from "@ethersproject/providers";

import { BigNumber } from "ethers";
import { Interface, TransactionDescription } from "ethers/lib/utils";
import { Interface, TransactionDescription } from "@ethersproject/abi";
import { Tokens, chainIdToRewardTokenMap, giftCardTreasuryAddress, permit2Address } from "../shared/constants";
import { getFastestRpcUrl, getGiftCardOrderId } from "../shared/helpers";
import { getGiftCardValue, isClaimableForAmount } from "../shared/pricing";
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.5.21",
"@cloudflare/workers-types": "^4.20240423.0",
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
Expand Down Expand Up @@ -84,6 +85,7 @@
"ts-jest": "29.1.2",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"vitest": "2.0.5",
"wrangler": "^3.51.2"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion shared/pricing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber, BigNumberish } from "ethers";
import { formatEther, parseEther } from "ethers/lib/utils";
import { formatEther, parseEther } from "@ethersproject/units";
import { PriceToValueMap, GiftCard } from "./types";

/**
Expand Down
79 changes: 79 additions & 0 deletions tests/get-best-card.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { env, createExecutionContext, waitOnExecutionContext } from "cloudflare:test";
import { describe, it, expect } from "vitest";
import { onRequest as pagesFunction } from "../functions/get-best-card";

describe(
"Get best virtual card",
() => {
it("should respond with correct virtual card", async () => {
// Create an empty context to pass to `worker.fetch()`
const execContext = createExecutionContext();
const eventCtx = getEventContext(execContext);
const response = await pagesFunction(eventCtx);
await waitOnExecutionContext(execContext);
expect(response.status).toBe(200);
expect(await response.text()).toBe(JSON.stringify(expectedResponse));
});
},
{ timeout: 20000 }
);

const expectedResponse = {
productId: 13959,
productName: "Vanilla® eGift Visa",
global: false,
supportsPreOrder: true,
senderFee: 6,
senderFeePercentage: 0,
discountPercentage: 0,
denominationType: "RANGE",
recipientCurrencyCode: "USD",
minRecipientDenomination: 20,
maxRecipientDenomination: 100,
senderCurrencyCode: "USD",
minSenderDenomination: 20,
maxSenderDenomination: 100,
fixedRecipientDenominations: [],
fixedSenderDenominations: null,
fixedRecipientToSenderDenominationsMap: null,
metadata: null,
logoUrls: ["https://cdn.reloadly.com/giftcards/cdf0a915-a88d-4eb5-8cb8-a4fd00accc7eVanilla.jpg"],
brand: { brandId: 95, brandName: "Vanilla® eGift Visa" },
category: { id: 1, name: "Payment Cards" },
country: { isoName: "US", name: "United States", flagUrl: "https://s3.amazonaws.com/rld-flags/us.svg" },
redeemInstruction: {
concise: "To redeem, visit yourrewardcard.com",
verbose:
"Virtual Account is a prepaid Virtual Account loaded by the Corporate Sponsor, redeemable to buy goods and services anywhere Visa debit Virtual Accounts are accepted, as described in the Virtual Account Use and Fees section. The Virtual Account is NOT a credit card. The Virtual Account is not a checking account or connected in any way to any account other than a stored value account where your funds are held. The expiration date of the Virtual Account and the Virtual Account funds is identified on the Virtual Account. 
eReward Visa Virtual Accountholder Agreement CUSTOMER SERVICE CONTACT INFORMATION: 
Address: P.O. Box 826 Fortson, GA 31808 
Website: YourRewardCard.com 
Phone Number: 1-833-634-3155",
},
};

function getEventContext(execContext: ExecutionContext) {
const request = new Request("http://localhost/get-best-card?country=US&amount=50000000000000000000");

const params = { slug: "hello" };
const data = {};
const eventCtx: Parameters<typeof pagesFunction>[0] = {
request,
functionPath: "",
waitUntil: execContext.waitUntil.bind(execContext),
passThroughOnException: execContext.passThroughOnException.bind(execContext),
async next(input, init) {
const request = new Request(input ?? "http://placeholder", init);
return new Response(`next:${request.method} ${request.url}`);
},
env: {
...env,
ASSETS: {
async fetch(input, init) {
const request = new Request(input, init);
return new Response(`ASSETS:${request.method} ${request.url}`);
},
},
},
params,
data,
};

return eventCtx;
}
13 changes: 12 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"include": ["functions", "build", "scripts/typescript", "globals.d.ts", "cypress/**/*.ts", "cypress.config.ts", "static/**/*.ts", ".github/**/*.ts"],
"include": [
"functions",
"build",
"tests",
"scripts/typescript",
"globals.d.ts",
"cypress/**/*.ts",
"cypress.config.ts",
"static/**/*.ts",
".github/**/*.ts",
"vitest.config.ts"
],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

Expand Down
13 changes: 13 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersConfig({
test: {
dir: "tests",
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.toml" },
},
main: "./functions/get-best-card.ts",
},
},
});
Loading

0 comments on commit c218921

Please sign in to comment.