From cc15fc8eed151d8e1f7a43f7984ae7c38bcda701 Mon Sep 17 00:00:00 2001 From: Aristides Staffieri Date: Fri, 3 Nov 2023 20:19:28 -0600 Subject: [PATCH] updates test for renew client split --- src/helper/test-helper.ts | 34 ++++++++++++++++++++++++------- src/service/mercury/index.test.ts | 24 +++++++++++----------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/helper/test-helper.ts b/src/helper/test-helper.ts index 0bc21c5..4802b3f 100644 --- a/src/helper/test-helper.ts +++ b/src/helper/test-helper.ts @@ -30,6 +30,11 @@ const client = new Client({ }, }); +const renewClient = new Client({ + url: `::1:5000/graphql`, + exchanges: [fetchExchange], +}); + const mercurySession = { token: "mercury-token", backend: "mercury-url", @@ -115,14 +120,23 @@ const queryMockResponse = { }, }; +jest + .spyOn(renewClient, "mutation") + .mockImplementation((_mutation: any): any => { + switch (_mutation) { + case mutation.authenticate: { + return Promise.resolve({ + data: queryMockResponse[mutation.authenticate], + error: null, + }); + } + default: + throw new Error("unknown mutation in mock"); + } + }); + jest.spyOn(client, "query").mockImplementation((_query: any): any => { switch (_query) { - case mutation.authenticate: { - return Promise.resolve({ - data: queryMockResponse[mutation.authenticate], - error: null, - }); - } case mutation.newAccountSubscription: { return Promise.resolve({ data: queryMockResponse[mutation.newAccountSubscription], @@ -157,7 +171,13 @@ jest.spyOn(client, "query").mockImplementation((_query: any): any => { } }); -const mockMercuryClient = new MercuryClient(mercurySession, client, testLogger); +const mockMercuryClient = new MercuryClient( + "http://example.com/graphql", + mercurySession, + client, + renewClient, + testLogger +); async function getDevServer() { const config = { hostname: "localhost", diff --git a/src/service/mercury/index.test.ts b/src/service/mercury/index.test.ts index 0800d0b..cab9161 100644 --- a/src/service/mercury/index.test.ts +++ b/src/service/mercury/index.test.ts @@ -8,18 +8,6 @@ import { } from "../../helper/test-helper"; describe("Mercury Service", () => { - it("can renew a token", async () => { - const response = await mockMercuryClient.renewMercuryToken(); - const expected = { - data: queryMockResponse[mutation.authenticate], - error: null, - }; - expect(response).toEqual(expected); - expect(mockMercuryClient.mercurySession.token).toEqual( - queryMockResponse[mutation.authenticate].authenticate?.jwtToken - ); - }); - it("can fetch account history with a payment-to in history", async () => { const { data } = await mockMercuryClient.getAccountHistory(pubKey); const paymentsToPublicKey = data?.data.paymentsToPublicKey.edges[0].node; @@ -61,4 +49,16 @@ describe("Mercury Service", () => { ) ).toEqual(contracts); }); + + it("can renew a token", async () => { + const response = await mockMercuryClient.renewMercuryToken(); + const expected = { + data: queryMockResponse[mutation.authenticate], + error: null, + }; + expect(response).toEqual(expected); + expect(mockMercuryClient.mercurySession.token).toEqual( + queryMockResponse[mutation.authenticate].authenticate?.jwtToken + ); + }); });