From 93b31c81f1f7137e17ce93dd60da6a5f7cb830f7 Mon Sep 17 00:00:00 2001 From: EresDev Date: Wed, 11 Dec 2024 22:49:52 +0500 Subject: [PATCH] test: offer second fallback mastercard --- functions/utils/best-card-finder.ts | 4 +-- .../get-best-card/card-18732-not-found.json | 8 +++++ tests/fixtures/get-best-card/no-card-kr.json | 8 +++++ tests/unit/get-best-card.test.ts | 32 +++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/get-best-card/card-18732-not-found.json create mode 100644 tests/fixtures/get-best-card/no-card-kr.json diff --git a/functions/utils/best-card-finder.ts b/functions/utils/best-card-finder.ts index 2db37a08..bd59ae0a 100644 --- a/functions/utils/best-card-finder.ts +++ b/functions/utils/best-card-finder.ts @@ -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; } } @@ -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; } } diff --git a/tests/fixtures/get-best-card/card-18732-not-found.json b/tests/fixtures/get-best-card/card-18732-not-found.json new file mode 100644 index 00000000..0dfa848c --- /dev/null +++ b/tests/fixtures/get-best-card/card-18732-not-found.json @@ -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": [] +} diff --git a/tests/fixtures/get-best-card/no-card-kr.json b/tests/fixtures/get-best-card/no-card-kr.json new file mode 100644 index 00000000..448e5ff1 --- /dev/null +++ b/tests/fixtures/get-best-card/no-card-kr.json @@ -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": [] +} diff --git a/tests/unit/get-best-card.test.ts b/tests/unit/get-best-card.test.ts index d29c8686..96513215 100644 --- a/tests/unit/get-best-card.test.ts +++ b/tests/unit/get-best-card.test.ts @@ -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"; @@ -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);