Skip to content

Commit

Permalink
PYIC-7329: Refactor core-front tests (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeCollingwood authored Nov 14, 2024
2 parents f8409d0 + 7983e7b commit 840756e
Show file tree
Hide file tree
Showing 27 changed files with 1,264 additions and 1,139 deletions.
431 changes: 154 additions & 277 deletions src/app/credential-issuer/middleware.test.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/app/credential-issuer/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const sendParamsToAPI: RequestHandler = async (req, res) => {
}

const apiResponse = await postCriCallback(req, body);
return handleBackendResponse(req, res, apiResponse?.data);
return handleBackendResponse(req, res, apiResponse.data);
};

// Temporary - this will replace the above method once all CRI's have been migrated across to use the new endpoint
Expand Down
41 changes: 22 additions & 19 deletions src/app/ipv/tests/checkFormRadioButtons.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
import { expect } from "chai";
import sinon from "sinon";
import { checkFormRadioButtonSelected } from "../middleware";
import {
specifyCreateRequest,
specifyCreateResponse,
} from "../../../test-utils/mock-express";

describe("checkFormRadioButtonSelected middleware", () => {
const testReq = {
body: {},
// Mock handler parameters
const createRequest = specifyCreateRequest({
params: { pageId: "page-ipv-identity-document-start" },
csrfToken: sinon.fake(),
session: {
currentPage: "page-ipv-identity-document-start",
ipvSessionId: "ipv-session-id",
save: sinon.fake.yields(null),
},
log: { error: sinon.fake() },
} as any;

const res = {
status: sinon.fake(),
redirect: sinon.fake(),
send: sinon.fake(),
render: sinon.fake(),
log: { info: sinon.fake(), error: sinon.fake() },
locals: { contactUsUrl: "contactUrl", deleteAccountUrl: "deleteAccount" },
} as any;

const next = sinon.fake() as any;
});
const createResponse = specifyCreateResponse();
const next: any = sinon.fake();

beforeEach(() => {
res.render = sinon.fake();
next.resetHistory();
});

it("should render form page again with error if no option is selected", async function () {
const req = { ...testReq, body: { journey: undefined } };
// Arrange
const req = createRequest();
const res = createResponse();

// Act
await checkFormRadioButtonSelected(req, res, next);

// Assert
expect(res.render).to.have.been.calledWith(
`ipv/page/${req.session.currentPage}.njk`,
{
Expand All @@ -47,9 +45,14 @@ describe("checkFormRadioButtonSelected middleware", () => {
});

it("should pass to next if an option is selected", async function () {
const req = { ...testReq, body: { journey: "someJourney" } };
// Arrange
const req = createRequest({ body: { journey: "someJourney" } });
const res = createResponse();

// Act
await checkFormRadioButtonSelected(req, res, next);

// Assert
expect(res.render).to.not.have.been.called;
expect(next).to.have.been.calledOnce;
});
Expand Down
137 changes: 74 additions & 63 deletions src/app/ipv/tests/formHandleCoiAndUpdateDetailsCheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
import { NextFunction } from "express";
import { expect } from "chai";
import sinon from "sinon";
import proxyquire from "proxyquire";
import {
specifyCreateRequest,
specifyCreateResponse,
} from "../../../test-utils/mock-express";

describe("handle update details/COI form checkbox", () => {
let next: NextFunction;

const testReq = {
body: {},
params: { pageId: "update-details" },
csrfToken: sinon.fake(),
session: {
currentPage: "update-details",
save: sinon.fake.yields(null),
},
log: { error: sinon.fake() },
} as any;

const res = {
status: sinon.fake(),
redirect: sinon.fake(),
send: sinon.fake(),
render: sinon.fake(),
log: { info: sinon.fake(), error: sinon.fake() },
locals: { contactUsUrl: "contactUrl", deleteAccountUrl: "deleteAccount" },
} as any;
// Mock handler parameters
const createResponse = specifyCreateResponse();
const next: any = sinon.fake();

// Setup stubs
const coreBackServiceStub = {
getProvenIdentityUserDetails: sinon.spy(),
getProvenIdentityUserDetails: sinon.fake.resolves({}),
};

const middleware: typeof import("../middleware") = proxyquire(
"../middleware",
{
Expand All @@ -38,11 +23,11 @@ describe("handle update details/COI form checkbox", () => {
);

beforeEach(() => {
next = sinon.fake() as any;
res.render = sinon.fake();
next.resetHistory();
coreBackServiceStub.getProvenIdentityUserDetails.resetHistory();
});

const testCases = [
const updateDetailsPageTestCases = [
{
detailsToUpdate: "address",
detailsCorrect: "no",
Expand Down Expand Up @@ -120,33 +105,49 @@ describe("handle update details/COI form checkbox", () => {
},
];

context("On the update-details page", () => {
testCases.forEach(
describe("On the update-details page", () => {
const createRequest = specifyCreateRequest({
params: { pageId: "update-details" },
session: {
save: sinon.fake.yields(null),
currentPage: "update-details",
},
});

updateDetailsPageTestCases.forEach(
({ detailsToUpdate, detailsCorrect, expectedJourney }) => {
it(`should set the journey to ${expectedJourney} if detailsCorrect is ${detailsCorrect} and detailsToUpdate is ${detailsToUpdate}`, async () => {
const req = { ...testReq, body: { detailsToUpdate, detailsCorrect } };
// Arrange
const req = createRequest({
body: {
detailsToUpdate,
detailsCorrect,
},
});
const res = createResponse();

// Act
await middleware.formHandleUpdateDetailsCheckBox(req, res, next);

// Assert
expect(next).to.have.been.calledOnce;
expect(req.body.journey).to.equal(expectedJourney);
});
},
);
});

context("On the confirm-your-details page", () => {
const coiTestReq = {
...testReq,
describe("On the confirm-your-details page", () => {
const createRequest = specifyCreateRequest({
params: { pageId: "confirm-your-details" },
session: {
...testReq.session,
context: "coi",
save: sinon.fake.yields(null),
currentPage: "confirm-your-details",
},
};
});

const coiDetailsCombosTestData = [
...testCases,
[
...updateDetailsPageTestCases,
{
detailsToUpdate: [],
detailsCorrect: "yes",
Expand All @@ -157,9 +158,7 @@ describe("handle update details/COI form checkbox", () => {
detailsCorrect: "yes",
expectedJourney: "next",
},
];

coiDetailsCombosTestData.forEach(
].forEach(
({
detailsToUpdate,
detailsCorrect,
Expand All @@ -170,27 +169,35 @@ describe("handle update details/COI form checkbox", () => {
expectedJourney: string;
}) => {
it(`should set the journey to ${expectedJourney} if detailsCorrect is ${detailsCorrect} and detailsToUpdate is ${detailsToUpdate}`, async () => {
const req = {
...coiTestReq,
// Arrange
const req = createRequest({
body: { detailsToUpdate, detailsCorrect },
};
session: { context: "coi", currentPage: "confirm-your-details" },
});
const res = createResponse();

// Act
await middleware.formHandleCoiDetailsCheck(req, res, next);

// Assert
expect(next).to.have.been.calledOnce;
expect(req.body.journey).to.equal(expectedJourney);
});
},
);

it("should set the correct error state if detailsCorrect is empty and detailsToUpdate is empty", async function () {
coreBackServiceStub.getProvenIdentityUserDetails = sinon.fake.returns({});

const req = {
...coiTestReq,
// Arrange
const req = createRequest({
body: { detailsToUpdate: "", detailsCorrect: "" },
};
session: { context: "coi", currentPage: "confirm-your-details" },
});
const res = createResponse();

// Act
await middleware.formHandleCoiDetailsCheck(req, res, next);

// Assert
expect(coreBackServiceStub.getProvenIdentityUserDetails).to.have.been
.called;
expect(next).to.not.have.been.called;
Expand All @@ -207,16 +214,17 @@ describe("handle update details/COI form checkbox", () => {
});

it("should set the correct error state if detailsCorrect is no and detailsToUpdate is empty", async function () {
coreBackServiceStub.getProvenIdentityUserDetails = sinon.fake.resolves(
{},
);

const req = {
...coiTestReq,
// Arrange
const req = createRequest({
body: { detailsToUpdate: "", detailsCorrect: "no" },
};
session: { context: "coi", currentPage: "confirm-your-details" },
});
const res = createResponse();

// Act
await middleware.formHandleCoiDetailsCheck(req, res, next);

// Assert
expect(coreBackServiceStub.getProvenIdentityUserDetails).to.have.been
.called;
expect(next).to.not.have.been.called;
Expand All @@ -234,18 +242,21 @@ describe("handle update details/COI form checkbox", () => {
});

it("should not get user details if the page does not require it", async function () {
coreBackServiceStub.getProvenIdentityUserDetails = sinon.fake.returns({});

const req = {
...testReq,
// Arrange
const req = specifyCreateRequest()({
params: { pageId: "update-details" },
session: {
...testReq.session,
currentPage: "check-name-date-birth",
context: undefined,
currentPage: "check-name-date-birth",
save: sinon.fake.yields(null),
},
};
});
const res = createResponse();

// Act
await middleware.formHandleCoiDetailsCheck(req, res, next);

// Assert
expect(coreBackServiceStub.getProvenIdentityUserDetails).to.not.have.been
.called;
expect(next).to.not.have.been.called;
Expand Down
Loading

0 comments on commit 840756e

Please sign in to comment.