Skip to content

Commit b9a3f17

Browse files
Fix tests that relied on classic JSX runtime
1 parent 6c22e0b commit b9a3f17

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

src/components/form-elements/select/__tests__/Select.test.tsx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { render } from '@testing-library/react';
2-
import { createRef, useRef } from 'react';
1+
import { createRef } from 'react';
32
import { Select } from '..';
43
import { renderClient, renderServer } from '#util/components';
54

@@ -8,16 +7,6 @@ describe('Select', () => {
87
jest.restoreAllMocks();
98
});
109

11-
const SelectComp = ({ onHandle }: { onHandle: () => void }) => {
12-
const ref = useRef<HTMLSelectElement | null>(null);
13-
const handleClick = () => {
14-
if (!ref.current) return;
15-
onHandle();
16-
};
17-
18-
return <Select onClick={handleClick} ref={ref} />;
19-
};
20-
2110
it('matches snapshot', async () => {
2211
const { container } = await renderClient(
2312
<Select id="test-select">
@@ -84,17 +73,22 @@ describe('Select', () => {
8473
expect(selectEl2).toHaveClass('nhsuk-select--error');
8574
});
8675

87-
it('should handle DOM events where ref Exists', () => {
88-
const useRefSpy = jest
89-
.spyOn(React, 'useRef')
90-
.mockReturnValueOnce({ current: document.createElement('select') });
76+
it('should handle DOM events where ref exists', async () => {
77+
const ref = createRef<HTMLSelectElement>();
9178
const mock = jest.fn();
92-
const { container } = render(<SelectComp onHandle={mock} />);
9379

94-
const selectEl = container.querySelector('select')!;
80+
const handleClick = () => {
81+
if (!ref.current) return;
82+
mock();
83+
};
84+
85+
const { modules } = await renderClient(<Select onClick={handleClick} ref={ref} />, {
86+
className: 'nhsuk-select',
87+
});
88+
89+
const [selectEl] = modules;
9590
selectEl.click();
9691

97-
expect(useRefSpy).toBeCalledWith(null);
9892
expect(mock).toBeCalledTimes(1);
9993
});
10094
});

src/components/form-elements/text-input/__tests__/TextInput.test.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render } from '@testing-library/react';
2-
import { createRef, useRef } from 'react';
2+
import { createRef } from 'react';
33
import { TextInput } from '..';
44
import { renderClient, renderServer } from '#util/components';
55
import { InputWidth } from '#util/types';
@@ -9,16 +9,6 @@ describe('TextInput', () => {
99
jest.restoreAllMocks();
1010
});
1111

12-
const TextInputComp = ({ onHandle }: { onHandle: () => void }) => {
13-
const ref = useRef(null);
14-
const handleClick = () => {
15-
if (!ref.current) return;
16-
onHandle();
17-
};
18-
19-
return <TextInput type="button" onClick={handleClick} ref={ref} />;
20-
};
21-
2212
it('matches snapshot', async () => {
2313
const { container } = await renderClient(
2414
<TextInput label="What is your NHS number?" labelProps={{ size: 'l' }} id="nhs-number" />,
@@ -61,17 +51,22 @@ describe('TextInput', () => {
6151
expect(fieldRef.current).toHaveClass('nhsuk-input');
6252
});
6353

64-
it('should handle click where ref Exists', () => {
65-
const useRefSpy = jest
66-
.spyOn(React, 'useRef')
67-
.mockReturnValueOnce({ current: document.createElement('button') });
54+
it('should handle DOM events where ref exists', async () => {
55+
const ref = createRef<HTMLInputElement>();
6856
const mock = jest.fn();
69-
const { container } = render(<TextInputComp onHandle={mock} />);
7057

71-
const textInputEl = container.querySelector('input')!;
58+
const handleClick = () => {
59+
if (!ref.current) return;
60+
mock();
61+
};
62+
63+
const { modules } = await renderClient(<TextInput onClick={handleClick} ref={ref} />, {
64+
className: 'nhsuk-input',
65+
});
66+
67+
const [textInputEl] = modules;
7268
textInputEl.click();
7369

74-
expect(useRefSpy).toBeCalledWith(null);
7570
expect(mock).toBeCalledTimes(1);
7671
});
7772

0 commit comments

Comments
 (0)