Skip to content

Commit

Permalink
chore: More type tightening (#173)
Browse files Browse the repository at this point in the history
## What does this PR do?
- Follow on from #78
- Removes all `implicitAny`s apart from those in
`src/export/digitalPlanning/model.ts`

### Next steps
- Write `Passport.get()` method to replace the placeholder
`Passport.generic()` (see
#78 (comment))
 - Test TS compilation on Pizza
- Rebase / merge / cherry-pick
#68 to move things
forward there
  • Loading branch information
DafyddLlyr authored Oct 25, 2023
1 parent 3d8465f commit 69c797c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
},
],
},
testPathIgnorePatterns: ["dist/*", "tests/*"],
testPathIgnorePatterns: ["dist/*"],
collectCoverage: true,
coverageReporters: ["html", "lcov", "text-summary"],
coverageDirectory: "./coverage",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"types": "./dist/index.d.ts",
"scripts": {
"build": "rimraf ./types ./dist && tsc --project ./tsconfig.types.json && tsc && pnpm copy-json-schema",
"build": "rimraf ./types ./dist && tsc --project ./tsconfig.types.json && tsc --project ./tsconfig.build.json && pnpm copy-json-schema",
"examples": "rimraf ./examples && ts-node ./src/templates/generateExamples.ts",
"lint": "eslint 'src/**/*.{js,ts}' && prettier -c src/**/*",
"lint:fix": "eslint --fix 'src/**/*.{js,ts}' && prettier -w src/**/*",
Expand Down
27 changes: 21 additions & 6 deletions src/export/bops/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
Response,
ResponseMetaData,
SiteAddress,
Value,
} from "../../types";
import {
BOPSFullPayload,
Expand All @@ -27,6 +28,7 @@ import {
GOV_PAY_PASSPORT_KEY,
USER_ROLES,
} from "../../types";
import { DataObject } from "./../../types/data";

const bopsDictionary = {
// applicant or agent details can be provided via TextInput(plural) or ContactInput component
Expand Down Expand Up @@ -90,6 +92,10 @@ function exhaustiveCheck(type: never): never {
throw new Error(`Unhandled type ${type}`);
}

function isDataObject(value: Value): value is DataObject {
return typeof value === "object" && value !== null;
}

export function formatProposalDetails({
flow,
breadcrumbs,
Expand Down Expand Up @@ -153,6 +159,13 @@ export function formatProposalDetails({
try {
const addressObject = Object.values(crumb.data!)[0];

if (!addressObject)
throw Error("Missing AddressInput component type");
if (!isDataObject(addressObject))
throw Error(
"Invalid addressObject produced by AddressInput component",
);

// `Object.values(addressObject)` won't guarantee key order, so explicitly create a new array
const orderedAddressKeys = [
"line1",
Expand All @@ -162,10 +175,10 @@ export function formatProposalDetails({
"postcode",
"country",
];
const orderedAddressItems: string[] = [];
const orderedAddressItems: Array<Value | undefined> = [];
orderedAddressKeys.forEach((key) => {
if (addressObject?.[key]) {
orderedAddressItems.push(addressObject?.[key]);
if (addressObject[key]) {
orderedAddressItems.push(addressObject[key]);
}
});
return [orderedAddressItems.filter(Boolean).join(", ")];
Expand Down Expand Up @@ -360,9 +373,11 @@ export function computeBOPSParams({
]) as Array<EnhancedGISResponse>;
const constraints: BOPSFullPayload["constraints"] = {};
passportConstraints.forEach((response: EnhancedGISResponse) => {
Object.entries(response.constraints).forEach(([key, constraint]) => {
constraints[key] = constraint.value;
});
Object.entries(response.constraints || []).forEach(
([key, constraint]) => {
constraints[key] = constraint.value;
},
);
});
data.constraints = constraints;

Expand Down
12 changes: 8 additions & 4 deletions src/export/bops/tests/addressInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ describe("AddressInput details are set correctly based on the breadcrumbs", () =
(detail) => detail.question === "What's your address",
)?.[0];

expect(addressInputItem?.responses[0]?.["value"]).toEqual(
"10 Main Street, Apt 1, Southwark, London, SE5 0HU, UK",
expect(addressInputItem?.responses[0]).toEqual(
expect.objectContaining({
value: "10 Main Street, Apt 1, Southwark, London, SE5 0HU, UK",
}),
);
});

Expand Down Expand Up @@ -84,8 +86,10 @@ describe("AddressInput details are set correctly based on the breadcrumbs", () =
(detail) => detail.question === "What's your address",
)?.[0];

expect(addressInputItem?.responses[0]?.["value"]).toEqual(
"10 Main Street, Southwark, SE5 0HU",
expect(addressInputItem?.responses[0]).toEqual(
expect.objectContaining({
value: "10 Main Street, Southwark, SE5 0HU",
}),
);
});
});
12 changes: 8 additions & 4 deletions src/export/bops/tests/contactInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ describe("ContactInput details are set correctly based on the breadcrumbs", () =
(detail) => detail.question === "Your contact details",
)?.[0];

expect(contactInputItem?.responses[0]?.["value"]).toEqual(
"Ms Jane Doe Open Systems Lab 0123456789 [email protected]",
expect(contactInputItem?.responses[0]).toEqual(
expect.objectContaining({
value: "Ms Jane Doe Open Systems Lab 0123456789 [email protected]",
}),
);
});

Expand Down Expand Up @@ -104,8 +106,10 @@ describe("ContactInput details are set correctly based on the breadcrumbs", () =
(detail) => detail.question === "Your contact details",
)?.[0];

expect(contactInputItem?.responses[0]?.["value"]).toEqual(
"Jane Doe 0123456789 [email protected]",
expect(contactInputItem?.responses[0]).toEqual(
expect.objectContaining({
value: "Jane Doe 0123456789 [email protected]",
}),
);
});
});
21 changes: 21 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"allowJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": ["DOM", "ESNext"],
"target": "es2022",
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"jsx": "react",
"strict": true,
"rootDir": "./src",
"declaration": true,
"outDir": "dist"
},
"include": ["./src/*", "./src/**/*"],
"exclude": ["./src/**/*.test.ts", "./src/**/mocks"]
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
"outDir": "dist"
},
"include": ["./src/*", "./src/**/*"],
"exclude": ["./src/**/*.test.ts", "./src/**/mocks"]
}

0 comments on commit 69c797c

Please sign in to comment.