Skip to content

Commit

Permalink
add ioWalletService unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silvicir committed Oct 3, 2024
1 parent 78bd155 commit 0d0f997
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions src/services/__tests__/ioWalletService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Grant_typeEnum } from "../../../generated/io-wallet-api/CreateWalletAtt
import { TrialSystemAPIClient } from "../../clients/trial-system.client";
import { SubscriptionStateEnum } from "../../../generated/trial-system-api/SubscriptionState";
import { IO_WALLET_TRIAL_ID } from "../../config";
import { StatusEnum } from "../../../generated/io-wallet-api/SetWalletInstanceStatusWithFiscalCodeData";

const mockGetEntityConfiguration = jest.fn();
const mockGetNonce = jest.fn();
Expand All @@ -14,6 +15,7 @@ const mockCreateWalletAttestation = jest.fn();
const mockHealthCheck = jest.fn();
const mockGetCurrentWalletInstanceStatus = jest.fn();
const mockSetWalletInstanceStatus = jest.fn();
const mockSetCurrentWalletInstanceStatus = jest.fn();

mockGetUserByFiscalCode.mockImplementation(() =>
t.success({
Expand Down Expand Up @@ -60,6 +62,12 @@ mockSetWalletInstanceStatus.mockImplementation(() =>
})
);

mockSetCurrentWalletInstanceStatus.mockImplementation(() =>
t.success({
status: 204,
})
);

const api = {
getEntityConfiguration: mockGetEntityConfiguration,
getNonce: mockGetNonce,
Expand All @@ -69,6 +77,7 @@ const api = {
healthCheck: mockHealthCheck,
getCurrentWalletInstanceStatus: mockGetCurrentWalletInstanceStatus,
setWalletInstanceStatus: mockSetWalletInstanceStatus,
setCurrentWalletInstanceStatus: mockSetCurrentWalletInstanceStatus,
};

const mockCreateSubscription = jest.fn();
Expand Down Expand Up @@ -476,6 +485,127 @@ describe("IoWalletService#createWalletAttestation", () => {
});
});

describe("IoWalletService#setCurrentWalletInstanceStatus", () => {
beforeEach(() => {
jest.clearAllMocks();
});

const status = StatusEnum["REVOKED"];

it("should make the correct api call", async () => {
const service = new IoWalletService(api, trialSystemApi);

await service.setCurrentWalletInstanceStatus(status, aFiscalCode);

expect(mockSetCurrentWalletInstanceStatus).toHaveBeenCalledWith({
body: {
status,
fiscal_code: aFiscalCode,
},
});
});

it("should handle a success response", async () => {
const service = new IoWalletService(api, trialSystemApi);

const res = await service.setCurrentWalletInstanceStatus(
status,
aFiscalCode
);

expect(res).toMatchObject({
kind: "IResponseSuccessNoContent",
});
});

it("should handle a generic error when the API client returns 422", async () => {
mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() =>
t.success({ status: 422 })
);

const service = new IoWalletService(api, trialSystemApi);

const res = await service.setCurrentWalletInstanceStatus(
status,
aFiscalCode
);

expect(res).toMatchObject({
kind: "IResponseErrorGeneric",
});
});

it("should handle an internal error when the API client returns 500", async () => {
const aGenericProblem = {};
mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() =>
t.success({ status: 500, value: aGenericProblem })
);

const service = new IoWalletService(api, trialSystemApi);

const res = await service.setCurrentWalletInstanceStatus(
status,
aFiscalCode
);

expect(res).toMatchObject({
kind: "IResponseErrorInternal",
});
});

it("should handle a service unavailable error when the API client returns 503", async () => {
const aGenericProblem = {};
mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() =>
t.success({ status: 503, value: aGenericProblem })
);

const service = new IoWalletService(api, trialSystemApi);

const res = await service.setCurrentWalletInstanceStatus(
status,
aFiscalCode
);

expect(res).toMatchObject({
kind: "IResponseErrorServiceUnavailable",
});
});

it("should handle an internal error when the API client returns a code not specified in spec", async () => {
const aGenericProblem = {};
mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() =>
t.success({ status: 599, value: aGenericProblem })
);

const service = new IoWalletService(api, trialSystemApi);

const res = await service.setCurrentWalletInstanceStatus(
status,
aFiscalCode
);

expect(res).toMatchObject({
kind: "IResponseErrorInternal",
});
});

it("should return an error if the api call throws an error", async () => {
mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() => {
throw new Error();
});
const service = new IoWalletService(api, trialSystemApi);

const res = await service.setCurrentWalletInstanceStatus(
status,
aFiscalCode
);

expect(res).toMatchObject({
kind: "IResponseErrorInternal",
});
});
});

describe("IoWalletService#getSubscription", () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down

0 comments on commit 0d0f997

Please sign in to comment.