Skip to content

Commit

Permalink
updates test for renew client split
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidesstaffieri committed Nov 4, 2023
1 parent a33bdff commit cc15fc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
34 changes: 27 additions & 7 deletions src/helper/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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",
Expand Down
24 changes: 12 additions & 12 deletions src/service/mercury/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
});
});

0 comments on commit cc15fc8

Please sign in to comment.