Skip to content

Commit

Permalink
test: offer second fallback mastercard
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Dec 11, 2024
1 parent 69e0880 commit 93b31c8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions functions/utils/best-card-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function getFirstFallbackIntlMastercard(accessToken: AccessToken): Promise
try {
return await getGiftCardById(fallbackIntlMastercardFirst.sku, accessToken);
} catch (e) {
console.error(`Failed to load international US mastercard: ${JSON.stringify(fallbackIntlMastercardFirst)}`, e);
console.error(`Failed to load first fallback mastercard: ${JSON.stringify(fallbackIntlMastercardFirst)}`, e);
return null;
}
}
Expand All @@ -96,7 +96,7 @@ async function getSecondFallbackIntlMastercard(accessToken: AccessToken): Promis
try {
return await getGiftCardById(fallbackIntlMastercardSecond.sku, accessToken);
} catch (e) {
console.error(`Failed to load international US mastercard: ${JSON.stringify(fallbackIntlMastercardSecond)}`, e);
console.error(`Failed to load second fallback mastercard: ${JSON.stringify(fallbackIntlMastercardSecond)}`, e);
return null;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/get-best-card/card-18732-not-found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"timeStamp": "2024-12-11 17:23:46",
"message": "The product was either not found or is no longer available, Please contact support",
"path": "/products/18732",
"errorCode": "PRODUCT_NOT_FOUND",
"infoLink": null,
"details": []
}
8 changes: 8 additions & 0 deletions tests/fixtures/get-best-card/no-card-kr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"timeStamp": "2024-12-11 16:32:37",
"message": "No products were found for the given country code. For a list of valid country codes visit https://www.nationsonline.org/oneworld/country_code_list.htm",
"path": "/countries/KR/products",
"errorCode": null,
"infoLink": null,
"details": []
}
32 changes: 32 additions & 0 deletions tests/unit/get-best-card.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { parseEther } from "@ethersproject/units";
import { createExecutionContext, waitOnExecutionContext } from "cloudflare:test";
import { http, HttpResponse } from "msw";
import { setupServer, SetupServerApi } from "msw/node";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import { onRequest as pagesFunction } from "../../functions/get-best-card";
import { RELOADLY_PRODUCTION_API_URL } from "../../functions/utils/shared";
import bestCard from "../fixtures/get-best-card/best-card-sandbox.json";
import card18597 from "../fixtures/get-best-card/card-18597.json";
import card18732 from "../fixtures/get-best-card/card-18732.json";
import card18732NotFound from "../fixtures/get-best-card/card-18732-not-found.json";
import noCardKr from "../fixtures/get-best-card/no-card-kr.json";
import { httpMocks } from "../fixtures/http-mocks";
import { createEventContext, TESTS_BASE_URL } from "./shared-utils";

Expand Down Expand Up @@ -66,6 +71,33 @@ describe("Get best payment card", () => {
expect(await response.json()).toEqual({ message: "There are no gift cards available." });
});

it("should respond with fallbackMastercardSecond when other cards are unavailable", async () => {
try {
server.use(
...[
http.get(`${RELOADLY_PRODUCTION_API_URL}/countries/KR/products`, () => {
return HttpResponse.json(noCardKr, { status: 404 });
}),
http.get(`${RELOADLY_PRODUCTION_API_URL}/products/18732`, () => {
return HttpResponse.json(card18732NotFound, { status: 404 });
}),
http.get(`${RELOADLY_PRODUCTION_API_URL}/products/18597`, () => {
return HttpResponse.json(card18597, { status: 200 });
}),
]
);
} catch (e) {
console.log(`Error starting msw server: ${e}`);
}

const request = new Request(`${TESTS_BASE_URL}/get-best-card?country=KR&amount=${parseEther("50")}`);
const eventCtx = createEventContext(request, execContext);
const response = await pagesFunction(eventCtx);
await waitOnExecutionContext(execContext);
expect(await response.json()).toEqual(card18597);
expect(response.status).toBe(200);
});

it("should respond with correct payment card for sandbox", async () => {
const request = new Request(`${TESTS_BASE_URL}/get-best-card?country=US&amount=${parseEther("50")}`);
const eventCtx = createEventContext(request, execContext, true);
Expand Down

0 comments on commit 93b31c8

Please sign in to comment.