Skip to content

Commit e45fdb1

Browse files
author
samuel.gomez
committed
test(field): better field control cover
1 parent 1e74005 commit e45fdb1

File tree

8 files changed

+143
-45
lines changed

8 files changed

+143
-45
lines changed

package-lock.json

Lines changed: 1 addition & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
"@types/downloadjs": "1.4.6",
5757
"@types/jest-axe": "3.5.9",
5858
"@types/lodash": "4.14.202",
59+
"@types/node": "20.11.5",
5960
"@types/react": "18.2.48",
6061
"@types/react-dom": "18.2.18",
6162
"@typescript-eslint/eslint-plugin": "6.19.0",
6263
"@typescript-eslint/parser": "6.19.0",
6364
"@vitejs/plugin-react": "4.2.1",
64-
"@vitest/coverage-istanbul": "^1.2.0",
6565
"@vitest/coverage-v8": "1.2.0",
6666
"cz-conventional-changelog": "3.3.0",
6767
"eslint": "8.56.0",

src/shared/components/form/FieldSelectInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Controller } from 'react-hook-form';
21
import { SelectInput } from '@axa-fr/react-toolkit-all';
32
import { MessageTypes } from '@axa-fr/react-toolkit-form-core';
43
import { ComponentPropsWithoutRef } from 'react';
4+
import { Controller } from 'react-hook-form';
55
import { DEFAULT_OPTION_LABEL } from 'shared/constants';
66
import { onChangeValue } from './form.helper';
77

@@ -38,7 +38,7 @@ const FieldSelectInput = ({
3838
label={label}
3939
forceDisplayMessage={forceDisplayMessage}
4040
messageType={messageType}
41-
message={fieldState.error?.message ?? null}
41+
message={fieldState.error?.message}
4242
placeholder={placeholder}
4343
forceDisplayPlaceholder={forceDisplayPlaceholder}
4444
{...otherSelectInputProps}

src/shared/components/form/FieldTextInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Controller } from 'react-hook-form';
21
import { TextInput } from '@axa-fr/react-toolkit-all';
32
import { MessageTypes } from '@axa-fr/react-toolkit-form-core';
43
import { ComponentPropsWithoutRef } from 'react';
4+
import { Controller } from 'react-hook-form';
55
import { onChangeValue } from './form.helper';
66

77
type TTextInput = ComponentPropsWithoutRef<typeof TextInput>;
@@ -34,7 +34,7 @@ const FieldTextInput = ({
3434
label={label}
3535
forceDisplayMessage={forceDisplayMessage}
3636
messageType={messageType}
37-
message={fieldState.error?.message ?? null}
37+
message={fieldState.error?.message}
3838
{...otherTextInputProps}
3939
/>
4040
)}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { render, screen, waitFor } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import { act } from 'react-dom/test-utils';
4+
import FieldSelectInput from '../FieldSelectInput';
5+
import { ERROR_MESSAGE, FieldInputWithControl, LABEL_SUBMIT } from './FieldWrapper.mock';
6+
7+
export const LABEL_CIVILITY = 'Civilité';
8+
export const CIVILITY = 'civility';
9+
export const OPTIONS_CIVILITY = [
10+
{ label: 'M.', value: 'M', id: 'civility-m' },
11+
{ label: 'Mme', value: 'MME', id: 'civility-mme' },
12+
{ label: 'Other', value: 'OTHER', id: 'civility-other' },
13+
];
14+
15+
const rules = {
16+
[CIVILITY]: {
17+
required: ERROR_MESSAGE,
18+
},
19+
};
20+
21+
describe('<FieldSelectInput/>', () => {
22+
it('Should render FieldSelectInput', async () => {
23+
render(
24+
<FieldInputWithControl
25+
renderField={control => (
26+
<FieldSelectInput control={control} rules={rules[CIVILITY]} name={CIVILITY} label={LABEL_CIVILITY} options={OPTIONS_CIVILITY} />
27+
)}
28+
/>,
29+
);
30+
const selectInput = screen.getByRole('combobox', { name: RegExp(LABEL_CIVILITY) });
31+
const type = 'Mme';
32+
33+
expect(selectInput).toBeInTheDocument();
34+
await act(async () => {
35+
await waitFor(() => userEvent.selectOptions(selectInput, type));
36+
});
37+
await waitFor(() => expect(screen.getByDisplayValue(RegExp(type))).toBeInTheDocument());
38+
});
39+
40+
it('Should render FieldSelectInput with error', async () => {
41+
render(
42+
<FieldInputWithControl
43+
renderField={control => (
44+
<FieldSelectInput control={control} rules={rules[CIVILITY]} name={CIVILITY} label={LABEL_CIVILITY} options={OPTIONS_CIVILITY} />
45+
)}
46+
/>,
47+
);
48+
const selectInput = screen.getByRole('combobox', { name: RegExp(LABEL_CIVILITY) });
49+
expect(selectInput).toBeInTheDocument();
50+
51+
await act(async () => {
52+
await waitFor(() => userEvent.click(screen.getByText(LABEL_SUBMIT)));
53+
});
54+
await screen.findByText(RegExp(ERROR_MESSAGE));
55+
});
56+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { render, screen, waitFor } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import { act } from 'react-dom/test-utils';
4+
import FieldTextInput from '../FieldTextInput';
5+
import { ERROR_MESSAGE, FieldInputWithControl, LABEL_SUBMIT } from './FieldWrapper.mock';
6+
7+
export const LABEL_FIRSTNAME = 'Prénom';
8+
export const FIRSTNAME = 'firstname';
9+
10+
const rules = {
11+
[FIRSTNAME]: {
12+
required: ERROR_MESSAGE,
13+
},
14+
};
15+
16+
describe('<FieldTextInput/>', () => {
17+
it('Should render FieldTextInput', async () => {
18+
render(
19+
<FieldInputWithControl
20+
renderField={control => <FieldTextInput control={control} rules={rules[FIRSTNAME]} name={FIRSTNAME} label={LABEL_FIRSTNAME} />}
21+
/>,
22+
);
23+
24+
const textInput = screen.getByRole('textbox', { name: RegExp(LABEL_FIRSTNAME) });
25+
const value = 'Samuel';
26+
27+
expect(textInput).toBeInTheDocument();
28+
const user = userEvent.setup();
29+
await act(async () => {
30+
await waitFor(() => user.type(textInput, value));
31+
});
32+
33+
await waitFor(() => expect(textInput).toHaveValue(value));
34+
});
35+
36+
it('Should render FieldSelectInput with error', async () => {
37+
render(
38+
<FieldInputWithControl
39+
renderField={control => <FieldTextInput control={control} rules={rules[FIRSTNAME]} name={FIRSTNAME} label={LABEL_FIRSTNAME} />}
40+
/>,
41+
);
42+
const textInput = screen.getByRole('textbox', { name: RegExp(LABEL_FIRSTNAME) });
43+
expect(textInput).toBeInTheDocument();
44+
45+
await act(async () => {
46+
await waitFor(() => userEvent.click(screen.getByText(LABEL_SUBMIT)));
47+
});
48+
await screen.findByText(RegExp(ERROR_MESSAGE));
49+
});
50+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Button } from '@axa-fr/react-toolkit-all';
2+
import { ReactElement } from 'react';
3+
import { Control, FieldValues, SubmitHandler, useForm } from 'react-hook-form';
4+
5+
export const LABEL_SUBMIT = 'Valider';
6+
export const FORM_ADD_MEMBERS = 'form-add-members';
7+
export const ERROR_MESSAGE = 'Champ obligatoire';
8+
9+
export const FieldInputWithControl = ({ renderField }: { renderField: (control: Control<FieldValues, unknown>) => ReactElement }) => {
10+
const { handleSubmit, control } = useForm();
11+
const onValid: SubmitHandler<FieldValues> = dataSubmitted => dataSubmitted;
12+
13+
return (
14+
<form onSubmit={handleSubmit(onValid)} id={FORM_ADD_MEMBERS}>
15+
{renderField(control)}
16+
17+
<Button type="submit" classModifier="hasiconRight">
18+
<span className="af-btn__text">{LABEL_SUBMIT}</span>
19+
</Button>
20+
</form>
21+
);
22+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { onChangeValue } from '../form.helper';
2+
3+
describe('onChangeValue', () => {
4+
test('Should call onChange with target value', () => {
5+
const onChange = vi.fn();
6+
onChangeValue(onChange)({ value: 'test' });
7+
expect(onChange).toHaveBeenCalledWith('test');
8+
});
9+
});

0 commit comments

Comments
 (0)