Skip to content

Commit 9d17972

Browse files
authored
Merge pull request #86 from arupdvsa/feature/select-ref-input-tests
added ref feature to select plus written refhook tests for input
2 parents aa5cb77 + 9065d44 commit 9d17972

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

src/components/input/Input.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React, { HTMLProps } from 'react';
1+
import React, { HTMLProps, MutableRefObject } from 'react';
22
import classNames from 'classnames';
33
import FormGroup from '../../util/FormGroup';
44
import { InputWidth } from '../../util/types/NHSUKTypes';
55
import { FormElementProps } from '../../util/types/FormTypes';
66

77
interface InputProps extends HTMLProps<HTMLInputElement>, FormElementProps {
8-
inputRef?: (inputRef: HTMLInputElement | null) => any;
8+
inputRef?: MutableRefObject<HTMLInputElement | null>;
99
width?: InputWidth;
1010
disableErrorLine?: boolean;
1111
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
component.find('Input').simulate('click');
23+
expect(useRefSpy).toBeCalledWith(null);
24+
});
25+
26+
it('should handle click where ref Exists', () => {
27+
const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: document.createElement('button') });
28+
const mock = jest.fn();
29+
const component = shallow(<InputComp onHandle={mock} />);
30+
component.find('Input').simulate('click');
31+
expect(useRefSpy).toBeCalledWith(null);
32+
expect(mock).toBeCalledTimes(1);
33+
});
34+
})

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
it(`should do nothing if ref does not Exist`, () => {
22+
const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: null });
23+
const component = shallow(<SelectComp />);
24+
component.find('Select').simulate('click');
25+
expect(useRefSpy).toBeCalledWith(null);
26+
});
27+
28+
it('should handle DOM events where ref Exists', () => {
29+
const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: document.createElement('select') });
30+
const mock = jest.fn();
31+
const component = shallow(<SelectComp onHandle={mock} />);
32+
component.find('Select').simulate('click');
33+
expect(useRefSpy).toBeCalledWith(null);
34+
expect(mock).toBeCalledTimes(1);
35+
});
36+
})

0 commit comments

Comments
 (0)