Skip to content

Commit

Permalink
AUT-3779: Extract template selection logic to function with tests
Browse files Browse the repository at this point in the history
Introduces the getAccountNotFoundTemplate function which replicates
existing template selection logic previously included in the controller
and calls getChannelSpecificTemplate.

I've opted to do this because the additional requirement to select a
template that is appropriate for the channel will introduce additional
logic. My sense is that two functions (one that selects the web template
and calls another to retrieve the mobile equivalent of that template) is
easier to understand and test.
  • Loading branch information
gtvj committed Nov 4, 2024
1 parent e2dc593 commit f28e845
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/components/account-not-found/account-not-found-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ import {
import { USER_JOURNEY_EVENTS } from "../common/state-machine/state-machine";
import xss from "xss";
import { getServiceSignInLink } from "../../config";
import { getChannelSpecificTemplate } from "../../utils/get-channel-specific-template";
import { getAccountNotFoundWebTemplate } from "./get-account-not-found-web-template";

export function accountNotFoundGet(req: Request, res: Response): void {
if (req.session.client.isOneLoginService) {
res.render("account-not-found/index-one-login.njk", {
email: req.session.user.email,
});
} else if (req.session.client.serviceType === SERVICE_TYPE.OPTIONAL) {
res.render("account-not-found/index-optional.njk");
} else {
res.render("account-not-found/index-mandatory.njk", {
email: req.session.user.email,
});
}
const webTemplate: string = getAccountNotFoundWebTemplate(
req.session.client.isOneLoginService,
req.session.client.serviceType
);

res.render(webTemplate, {
email: req.session.user.email,
});
}

export function accountNotFoundPost(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SERVICE_TYPE } from "../../app.constants";

export function getAccountNotFoundWebTemplate(
isOneLoginService: boolean,
serviceType: string
): string {
if (isOneLoginService === true) {
return "account-not-found/index-one-login.njk";
}

if (serviceType === SERVICE_TYPE.OPTIONAL) {
return "account-not-found/index-optional.njk";
}

return "account-not-found/index-mandatory.njk";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from "chai";
import { describe } from "mocha";
import { getAccountNotFoundWebTemplate } from "../get-account-not-found-web-template";
import { SERVICE_TYPE } from "../../../app.constants";

describe("getAccountNotFoundWebTemplate", () => {
describe("when isOneLoginService is true", () => {
it("should always return 'account-not-found/index-one-login.njk'", () => {
[SERVICE_TYPE.OPTIONAL, SERVICE_TYPE.MANDATORY].forEach((i) => {
expect(getAccountNotFoundWebTemplate(true, i)).to.equal(
"account-not-found/index-one-login.njk"
);
});
});
});

describe("when isOneLoginService is false", () => {
it("should return 'account-not-found/index-optional.njk' when serviceType is equal to SERVICE_TYPE.OPTIONAL", () => {
expect(
getAccountNotFoundWebTemplate(false, SERVICE_TYPE.OPTIONAL)
).to.equal("account-not-found/index-optional.njk");
});

it("should return 'account-not-found/index-mandatory.njk' when serviceType is not equal to SERVICE_TYPE.OPTIONAL", () => {
[SERVICE_TYPE.MANDATORY, ""].forEach((i) => {
expect(getAccountNotFoundWebTemplate(false, i)).to.equal(
"account-not-found/index-mandatory.njk"
);
});
});
});
});

0 comments on commit f28e845

Please sign in to comment.