Skip to content

Commit 14ae393

Browse files
KaiSpencerTomdango
andauthored
bug/Checkbox declaration file inconsistency (#133)
* Add "strictNullChecks", fix two cases where typing needs updating in line with checks. * Revert to .defaultProps * Throw error on default HeadingLevel.tsx case * Warn on invalid prop, test to cover * Better dev warning for heading level --------- Co-authored-by: Thomas Judd-Cooper <[email protected]>
1 parent 4993b4e commit 14ae393

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

src/components/do-dont-list/__tests__/__snapshots__/DoDontList.test.tsx.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ exports[`DoDontList matches snapshot: DoDontList-Do 1`] = `
1313
>
1414
<HeadingLevel
1515
className="nhsuk-do-dont-list__label"
16-
headingLevel="h3"
1716
>
1817
Do
1918
</HeadingLevel>
@@ -33,7 +32,6 @@ exports[`DoDontList matches snapshot: DoDontList-Dont 1`] = `
3332
>
3433
<HeadingLevel
3534
className="nhsuk-do-dont-list__label"
36-
headingLevel="h3"
3735
>
3836
Don't
3937
</HeadingLevel>

src/components/table/TableHelpers.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ export const isTableCell = (child: ReactNode): child is ReactElement => {
66
};
77

88
export const getHeadingsFromChildren = (children: ReactNode): string[] => {
9-
return React.Children
10-
.map(children, child => {
11-
if (isTableCell(child)) {
12-
return child.props.children.toString();
13-
}
14-
return null;
15-
})
16-
.filter(Boolean);
9+
const headings: string[] = [];
10+
React.Children.map(children, (child) => {
11+
if (isTableCell(child)) {
12+
headings.push(child.props.children.toString());
13+
}
14+
});
15+
return headings;
1716
};

src/components/warning-callout/__tests__/__snapshots__/WarningCallout.test.tsx.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ exports[`WarningCallout matches snapshot 1`] = `
1010
>
1111
<HeadingLevel
1212
className="nhsuk-warning-callout__label"
13-
headingLevel="h3"
1413
>
1514
<h3
1615
className="nhsuk-warning-callout__label"

src/util/HeadingLevel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type HeadingLevelType =
1818
| 'H5'
1919
| 'H6';
2020

21-
const HeadingLevel: React.FC<HeadingLevelProps> = ({ headingLevel, ...rest }) => {
21+
const HeadingLevel: React.FC<HeadingLevelProps> = ({ headingLevel='h3', ...rest }) => {
2222
switch (headingLevel.toLowerCase()) {
2323
case 'h1':
2424
return <h1 {...rest} />;
@@ -32,10 +32,10 @@ const HeadingLevel: React.FC<HeadingLevelProps> = ({ headingLevel, ...rest }) =>
3232
return <h5 {...rest} />;
3333
case 'h6':
3434
return <h6 {...rest} />;
35+
default:
36+
console.error(`HeadingLevel: Invalid headingLevel prop: ${headingLevel}`);
37+
return <h3 {...rest} />;
3538
}
3639
};
37-
HeadingLevel.defaultProps = {
38-
headingLevel: 'h3',
39-
};
4040

4141
export default HeadingLevel;

src/util/__tests__/HeadingLevel.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,15 @@ describe('HeadingLevel', () => {
4343
h6Element.unmount();
4444
H6Element.unmount();
4545
});
46+
47+
it("console.warn when headingLevel is invalid", () => {
48+
// eslint-disable-next-line @typescript-eslint/no-empty-function
49+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
50+
// @ts-expect-error - testing invalid prop
51+
shallow(<HeadingLevel headingLevel="h7" />);
52+
expect(consoleSpy).toHaveBeenCalledTimes(1);
53+
expect(consoleSpy).toHaveBeenCalledWith(
54+
'HeadingLevel: Invalid headingLevel prop: h7');
55+
consoleSpy.mockRestore();
56+
});
4657
});

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"types": ["react", "jest", "node"],
1313
"allowSyntheticDefaultImports": true,
1414
"esModuleInterop": true,
15-
"skipLibCheck": true
15+
"skipLibCheck": true,
16+
"strictNullChecks": true
1617
},
1718
"include": ["src/**/*"],
1819
"exclude": [

0 commit comments

Comments
 (0)