Skip to content

Commit 7932d3a

Browse files
Resolve various Sonar issues
1 parent cefb631 commit 7932d3a

File tree

11 files changed

+38
-50
lines changed

11 files changed

+38
-50
lines changed

src/components/content-presentation/icons/__tests__/Icons.test.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import * as Icons from '../';
44

55
describe('Icons', () => {
66
it('all icons match snapshots', () => {
7-
Object.entries(Icons).forEach((icon) => {
8-
const [name, Icon] = icon;
9-
7+
for (const [name, Icon] of Object.entries(Icons)) {
108
const { container } = render(<Icon />);
11-
129
expect(container).toMatchSnapshot(name);
13-
});
10+
}
1411
});
1512
});

src/components/form-elements/checkboxes/__tests__/Checkboxes.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ describe('Checkboxes', () => {
106106
{ moduleName: 'nhsuk-checkboxes' },
107107
);
108108

109-
expect(container.querySelector('#none')?.getAttribute('data-checkbox-exclusive')).toBe('true');
109+
const inputEl = container.querySelector<HTMLInputElement>('#none');
110+
111+
expect(inputEl?.dataset).toHaveProperty('checkboxExclusive', 'true');
110112
});
111113
});

src/components/form-elements/date-input/components/IndividualDateInputs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ const IndividualDateInput = forwardRef<HTMLInputElement, IndividualDateInputProp
4646

4747
const inputID = id || `${ctxId}-${inputType}`;
4848
const inputName = name || `${ctxName}-${inputType}`;
49-
const inputValue = value !== undefined ? value : ctxValue?.[inputType];
49+
const inputValue = value === undefined ? ctxValue?.[inputType] : value;
5050
const inputDefaultValue =
51-
defaultValue !== undefined ? defaultValue : ctxDefaultValue?.[inputType];
51+
defaultValue === undefined ? ctxDefaultValue?.[inputType] : defaultValue;
5252

5353
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
5454
e.persist();

src/components/form-elements/error-summary/ErrorSummary.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const ListItem = forwardRef<HTMLAnchorElement, ListItemProps>((props, forwardedR
4747

4848
return (
4949
<li>
50-
{props.asElement ?? props.href ? (
50+
{(props.asElement ?? props.href) ? (
5151
<Element ref={forwardedRef} {...rest}>
5252
{children}
5353
</Element>
@@ -80,16 +80,14 @@ const ErrorSummaryComponent = forwardRef<HTMLDivElement, ErrorSummaryProps>(
8080
}, [moduleRef, instance]);
8181

8282
const items = Children.toArray(children);
83-
const [title] = items.filter((child) => childIsOfComponentType(child, Title));
83+
const title = items.find((child) => childIsOfComponentType(child, Title));
8484
const bodyItems = items.filter((child) => !childIsOfComponentType(child, Title));
8585

8686
return (
8787
<div
8888
className={classNames('nhsuk-error-summary', className)}
8989
data-module="nhsuk-error-summary"
90-
data-disable-auto-focus={
91-
typeof disableAutoFocus !== 'undefined' ? disableAutoFocus : undefined
92-
}
90+
data-disable-auto-focus={disableAutoFocus}
9391
ref={moduleRef}
9492
{...rest}
9593
>

src/components/navigation/header/Header.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ const HeaderComponent = forwardRef<HTMLElement, HeaderProps>((props, forwardedRe
107107
}, [logoProps, serviceProps, organisationProps]);
108108

109109
const items = Children.toArray(children);
110-
const [childLogo] = items.filter((child) => childIsOfComponentType(child, Logo));
111-
const [childSearch] = items.filter((child) => childIsOfComponentType(child, Search));
112-
const [childNavigation] = items.filter((child) => childIsOfComponentType(child, Navigation));
113-
const [childAccount] = items.filter((child) => childIsOfComponentType(child, Account));
110+
const childLogo = items.find((child) => childIsOfComponentType(child, Logo));
111+
const childSearch = items.find((child) => childIsOfComponentType(child, Search));
112+
const childNavigation = items.find((child) => childIsOfComponentType(child, Navigation));
113+
const childAccount = items.find((child) => childIsOfComponentType(child, Account));
114114

115115
return (
116116
<header

src/components/utils/__tests__/FormGroup.test.tsx

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ describe('FormGroup', () => {
5454
{ className: 'nhsuk-form-group' },
5555
);
5656

57-
expect(renderProps).not.toBe(null);
5857
expect(renderProps?.id).toHaveLength(11);
5958
expect(renderProps?.id).toContain('input');
6059
});
@@ -70,7 +69,6 @@ describe('FormGroup', () => {
7069
{ className: 'nhsuk-form-group' },
7170
);
7271

73-
expect(renderProps).not.toBe(null);
7472
expect(renderProps?.id).toBe('TestID2ElectricBoogaloo');
7573
});
7674

@@ -85,16 +83,14 @@ describe('FormGroup', () => {
8583
{ className: 'nhsuk-form-group' },
8684
);
8785

88-
expect(renderProps).not.toBe(null);
8986
expect(renderProps?.id).toHaveLength(11);
9087
expect(renderProps?.id).toContain('input');
9188

92-
expect(container.querySelector('input')?.getAttribute('aria-describedby')).toBe(
93-
`${renderProps?.id}--hint`,
94-
);
95-
expect(container.querySelector('.nhsuk-hint')?.getAttribute('id')).toBe(
96-
`${renderProps?.id}--hint`,
97-
);
89+
const hintEl = container.querySelector('.nhsuk-hint');
90+
const inputEl = container.querySelector('input');
91+
92+
expect(hintEl).toHaveProperty('id', `${renderProps?.id}--hint`);
93+
expect(inputEl?.getAttribute('aria-describedby')).toBe(`${renderProps?.id}--hint`);
9894
});
9995

10096
it('passes correct props for hint (custom id)', async () => {
@@ -108,11 +104,13 @@ describe('FormGroup', () => {
108104
{ className: 'nhsuk-form-group' },
109105
);
110106

111-
expect(renderProps).not.toBe(null);
112107
expect(renderProps?.id).toBe('testID');
113108

114-
expect(container.querySelector('input')?.getAttribute('aria-describedby')).toBe('testID--hint');
115-
expect(container.querySelector('.nhsuk-hint')?.getAttribute('id')).toBe('testID--hint');
109+
const hintEl = container.querySelector('.nhsuk-hint');
110+
const inputEl = container.querySelector('input');
111+
112+
expect(hintEl).toHaveProperty('id', 'testID--hint');
113+
expect(inputEl?.getAttribute('aria-describedby')).toBe('testID--hint');
116114
});
117115

118116
it('passes correct props for label (generated id)', async () => {
@@ -126,7 +124,6 @@ describe('FormGroup', () => {
126124
{ className: 'nhsuk-form-group' },
127125
);
128126

129-
expect(renderProps).not.toBe(null);
130127
expect(renderProps?.id).toHaveLength(11);
131128
expect(renderProps?.id).toContain('input');
132129

@@ -152,7 +149,6 @@ describe('FormGroup', () => {
152149
{ className: 'nhsuk-form-group' },
153150
);
154151

155-
expect(renderProps).not.toBe(null);
156152
expect(renderProps?.id).toBe('testID');
157153

158154
expect(container.querySelector('.nhsuk-label')?.getAttribute('id')).toBe('testID--label');
@@ -176,7 +172,6 @@ describe('FormGroup', () => {
176172
{ className: 'nhsuk-form-group' },
177173
);
178174

179-
expect(renderProps).not.toBe(null);
180175
expect(renderProps?.id).toHaveLength(11);
181176
expect(renderProps?.id).toContain('input');
182177
expect(renderProps!['aria-describedby']).toBe(`${renderProps?.id}--error-message`);
@@ -208,7 +203,6 @@ describe('FormGroup', () => {
208203
{ className: 'nhsuk-form-group' },
209204
);
210205

211-
expect(renderProps).not.toBe(null);
212206
expect(renderProps?.id).toBe('testID');
213207
expect(renderProps!['aria-describedby']).toBe(`testID--error-message`);
214208

@@ -277,9 +271,8 @@ describe('FormGroup', () => {
277271
{ className: 'nhsuk-form-group' },
278272
);
279273

280-
const inputElement = container.querySelector('input');
281-
expect(inputElement).not.toBeNull();
282-
expect(inputElement?.getAttribute('aria-describedby')).toBe(
274+
const inputEl = container.querySelector('input');
275+
expect(inputEl?.getAttribute('aria-describedby')).toBe(
283276
'error-and-hint--hint error-and-hint--error-message',
284277
);
285278
});
@@ -293,9 +286,7 @@ describe('FormGroup', () => {
293286
{ className: 'nhsuk-form-group' },
294287
);
295288

296-
const inputElement = container.querySelector('input');
297-
expect(inputElement).not.toBeNull();
298-
299-
expect(inputElement?.getAttribute('aria-describedby')).toBe(null);
289+
const inputEl = container.querySelector('input');
290+
expect(inputEl?.getAttribute('aria-describedby')).toBe(null);
300291
});
301292
});

src/setupTests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { outdent } from 'outdent';
77
*
88
* @see {@link https://github.com/jsdom/jsdom/issues/1695}
99
*/
10-
Object.defineProperties(window, {
10+
Object.defineProperties(globalThis, {
1111
TextEncoder: {
1212
value: TextEncoder,
1313
configurable: true,
@@ -32,9 +32,9 @@ Object.defineProperties(Element.prototype, {
3232
});
3333

3434
/**
35-
* Polyfill `window.matchMedia()` for NHS.UK frontend tabs
35+
* Polyfill `matchMedia()` for NHS.UK frontend tabs
3636
*/
37-
Object.defineProperty(window, 'matchMedia', {
37+
Object.defineProperty(globalThis, 'matchMedia', {
3838
writable: true,
3939
value: jest.fn().mockImplementation((query) => ({
4040
matches: true,

stories/Form Elements/Checkboxes.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useRef, useState, useEffect, SyntheticEvent } from 'react';
2-
import { Checkboxes, TextInput, Button } from '../../src';
2+
import { Checkboxes, TextInput } from '../../src';
33
import { Meta, StoryObj } from '@storybook/react';
44

55
/**

stories/Form Elements/Radios.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState, MouseEvent } from 'react';
2-
import { Radios, Button, TextInput, Checkboxes } from '../../src';
1+
import React, { useState } from 'react';
2+
import { Radios, TextInput, Checkboxes } from '../../src';
33
import { Meta, StoryObj } from '@storybook/react';
44

55
const meta: Meta<typeof Radios> = {

stories/Form Elements/Select.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState, MouseEvent } from 'react';
2-
import { Select, Button, TextInput } from '../../src';
1+
import React, { useState } from 'react';
2+
import { Select, TextInput } from '../../src';
33
import { Meta, StoryObj } from '@storybook/react';
44

55
const meta: Meta<typeof Select> = {

0 commit comments

Comments
 (0)