Skip to content

Commit 7f0a63a

Browse files
committed
added ref feature to select plus written refhook tests for input
1 parent aa5cb77 commit 7f0a63a

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Input from '../Input';
2+
import React from 'react';
3+
import { shallow } from 'enzyme';
4+
describe('Input', () => {
5+
afterEach(() => {
6+
jest.restoreAllMocks();
7+
});
8+
const InputComp = ({onHandle}: any) => {
9+
const ref = React.useRef(null);
10+
const handleClick = () => {
11+
if(!ref.current) return;
12+
onHandle();
13+
};
14+
15+
return (
16+
<Input type='button' onClick={handleClick} inputRef={ref}></Input>
17+
)
18+
};
19+
it('should do nothing if ref does not exist', () => {
20+
const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: null });
21+
const component = shallow(<InputComp />);
22+
console.log(component.debug());
23+
component.find('Input').simulate('click');
24+
expect(useRefSpy).toBeCalledWith(null);
25+
});
26+
27+
it('should handle click where ref Exists', () => {
28+
const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: document.createElement('button') });
29+
const mock = jest.fn();
30+
const component = shallow(<InputComp onHandle={mock} />);
31+
component.find('Input').simulate('click');
32+
expect(useRefSpy).toBeCalledWith(null);
33+
expect(mock).toBeCalledTimes(1);
34+
});
35+
})

src/components/select/Select.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
import React, { HTMLProps } from 'react';
2-
import classNames from 'classnames';
1+
import React, { HTMLProps, MutableRefObject } from 'react';
2+
33
import { FormElementProps } from '../../util/types/FormTypes';
44
import FormGroup from '../../util/FormGroup';
5+
import classNames from 'classnames';
56

6-
type SelectProps = HTMLProps<HTMLSelectElement> & FormElementProps;
7+
// SelectProps = HTMLProps<HTMLSelectElement> & FormElementProps;
8+
interface ISelectProps extends HTMLProps<HTMLSelectElement>, FormElementProps {
9+
selectRef?: MutableRefObject<HTMLSelectElement | null>;
10+
}
711

8-
interface ISelect extends React.FC<SelectProps> {
12+
interface ISelect extends React.FC<ISelectProps> {
913
Option: React.FC<HTMLProps<HTMLOptionElement>>;
1014
}
1115

1216
const Select: ISelect = ({ children, ...rest }) => (
13-
<FormGroup<SelectProps> inputType="select" {...rest}>
14-
{({ className, error, ...restRenderProps }) => (
17+
<FormGroup<ISelectProps> inputType="select" {...rest}>
18+
{({ className, error, selectRef, ...restRenderProps }) => (
1519
<select
16-
className={classNames('nhsuk-select', { 'nhsuk-select--error': error }, className)}
20+
className={classNames(
21+
'nhsuk-select',
22+
{ 'nhsuk-select--error': error },
23+
className,
24+
)}
25+
ref={selectRef}
1726
{...restRenderProps}
1827
>
1928
{children}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from "react";
2+
import Select from "../Select";
3+
import { shallow } from "enzyme";
4+
5+
describe(`Select`, () => {
6+
afterEach(() => {
7+
jest.restoreAllMocks();
8+
});
9+
const SelectComp = ({onHandle}: any) => {
10+
const ref = React.useRef<HTMLSelectElement | null>(null);
11+
const handleClick = () => {
12+
if(!ref.current) return;
13+
onHandle();
14+
};
15+
16+
return (
17+
<Select onClick={handleClick} selectRef={ref} />
18+
);
19+
};
20+
it(`should do nothing if ref does not exist`, () => {
21+
const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: null });
22+
const component = shallow(<SelectComp />);
23+
console.log(component.debug());
24+
component.find('Select').simulate('click');
25+
expect(useRefSpy).toBeCalledWith(null);
26+
});
27+
})

0 commit comments

Comments
 (0)