-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathproposalActionsDecoderBooleanField.test.tsx
More file actions
149 lines (130 loc) · 6.65 KB
/
Copy pathproposalActionsDecoderBooleanField.test.tsx
File metadata and controls
149 lines (130 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as ReactHookForm from 'react-hook-form';
import { modulesCopy } from '../../../../../assets';
import {
type IProposalActionsDecoderBooleanFieldProps,
ProposalActionsDecoderBooleanField,
} from './proposalActionsDecoderBooleanField';
jest.mock('react-hook-form', () => ({
...jest.requireActual<typeof ReactHookForm>('react-hook-form'),
__esModule: true,
}));
describe('<ProposalActionsDecoderBooleanField /> component', () => {
const useControllerSpy = jest.spyOn(ReactHookForm, 'useController');
beforeEach(() => {
useControllerSpy.mockReturnValue({
fieldState: { error: undefined },
field: { value: undefined, onBlur: jest.fn(), onChange: jest.fn(), ref: jest.fn() },
} as unknown as ReactHookForm.UseControllerReturn);
});
afterEach(() => {
useControllerSpy.mockReset();
});
const createTestComponent = (props?: Partial<IProposalActionsDecoderBooleanFieldProps>) => {
const completeProps: IProposalActionsDecoderBooleanFieldProps = {
parameter: { name: 'boolParam', type: 'bool', value: undefined },
fieldName: 'testName',
...props,
};
return <ProposalActionsDecoderBooleanField {...completeProps} />;
};
it('registers the field on the form context and renders a radio group with no default selection', () => {
const fieldName = 'parameter.0.value';
render(createTestComponent({ fieldName }));
expect(useControllerSpy).toHaveBeenCalledWith({
name: fieldName,
rules: { validate: expect.any(Function) as unknown },
});
const radios = screen.getAllByRole('radio');
expect(radios).toHaveLength(2);
radios.forEach((radio) => expect(radio).not.toBeChecked());
expect(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanTrue })).toBeInTheDocument();
expect(
screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanFalse }),
).toBeInTheDocument();
});
it('prefixes the field name with the formPrefix property when set', () => {
const fieldName = 'value';
const formPrefix = 'actions.0';
render(createTestComponent({ fieldName, formPrefix }));
expect(useControllerSpy).toHaveBeenCalledWith(expect.objectContaining({ name: `${formPrefix}.${fieldName}` }));
});
it('normalises string field values to booleans to support transaction data encoding', () => {
const onChange = jest.fn();
useControllerSpy.mockReturnValue({
fieldState: { error: undefined },
field: { value: 'true', onBlur: jest.fn(), onChange, ref: jest.fn() },
} as unknown as ReactHookForm.UseControllerReturn);
render(createTestComponent());
expect(onChange).toHaveBeenCalledWith(true);
});
it('does not update the field value when it is already a boolean', () => {
const onChange = jest.fn();
useControllerSpy.mockReturnValue({
fieldState: { error: undefined },
field: { value: false, onBlur: jest.fn(), onChange, ref: jest.fn() },
} as unknown as ReactHookForm.UseControllerReturn);
render(createTestComponent());
expect(onChange).not.toHaveBeenCalled();
});
it('renders the parameter name, type and notice as field labels', () => {
const parameter = { name: 'tryExecute', notice: 'description', type: 'bool', value: undefined };
render(createTestComponent({ parameter }));
expect(screen.getByText(`(${parameter.type})`)).toBeInTheDocument();
expect(screen.getByText(parameter.notice)).toBeInTheDocument();
// eslint-disable-next-line testing-library/no-node-access
expect(screen.getByText(`(${parameter.type})`).parentElement).toHaveTextContent(
`${parameter.name} (${parameter.type})`,
);
});
it('does not render the field labels when hideLabels property is set to true', () => {
const parameter = { name: 'tryExecute', notice: 'description', type: 'bool', value: undefined };
render(createTestComponent({ parameter, hideLabels: true }));
expect(screen.queryByText(`(${parameter.type})`)).not.toBeInTheDocument();
expect(screen.queryByText(parameter.notice)).not.toBeInTheDocument();
});
it('checks the radio item matching the current field value', () => {
useControllerSpy.mockReturnValue({
fieldState: { error: undefined },
field: { value: false, onChange: jest.fn() },
} as unknown as ReactHookForm.UseControllerReturn);
render(createTestComponent());
expect(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanFalse })).toBeChecked();
expect(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanTrue })).not.toBeChecked();
});
it('triggers the onChange callback with a boolean value on radio selection', async () => {
const onChange = jest.fn();
useControllerSpy.mockReturnValue({
fieldState: { error: undefined },
field: { value: undefined, onBlur: jest.fn(), onChange, ref: jest.fn() },
} as unknown as ReactHookForm.UseControllerReturn);
render(createTestComponent());
await userEvent.click(screen.getByRole('radio', { name: modulesCopy.proposalActionsDecoder.booleanTrue }));
expect(onChange).toHaveBeenCalledWith(true);
});
it('passes form blur and ref handlers to the radio group', () => {
const onBlur = jest.fn();
const ref = jest.fn();
useControllerSpy.mockReturnValue({
fieldState: { error: undefined },
field: { value: undefined, onBlur, onChange: jest.fn(), ref },
} as unknown as ReactHookForm.UseControllerReturn);
render(createTestComponent());
const radioGroup = screen.getByRole('radiogroup');
expect(ref).toHaveBeenCalledWith(radioGroup);
radioGroup.focus();
radioGroup.blur();
expect(onBlur).toHaveBeenCalled();
});
it('renders an alert when the form field has an error', () => {
const error = { message: 'invalid field' };
useControllerSpy.mockReturnValue({
fieldState: { error },
field: { value: undefined, onBlur: jest.fn(), onChange: jest.fn(), ref: jest.fn() },
} as unknown as ReactHookForm.UseControllerReturn);
render(createTestComponent());
expect(screen.getByRole('alert')).toBeInTheDocument();
expect(screen.getByText(error.message)).toBeInTheDocument();
});
});