Skip to content

Commit b8dc719

Browse files
add validation on submit
1 parent 7b1f57d commit b8dc719

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

redisinsight/ui/src/components/inline-item-editor/InlineItemEditor.spec.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,42 @@ describe('InlineItemEditor', () => {
100100
fireEvent.click(screen.getByTestId(/save-btn/))
101101
expect(onApplyMock).toBeCalled()
102102
})
103+
104+
it('should not call onApply if form is invalid', () => {
105+
const onApplyMock = jest.fn().mockReturnValue(false)
106+
render(
107+
<InlineItemEditor
108+
{...instance(mockedProps)}
109+
isLoading
110+
onApply={onApplyMock}
111+
onDecline={jest.fn()}
112+
/>
113+
)
114+
115+
expect(screen.getByTestId('apply-btn')).not.toBeDisabled()
116+
117+
fireEvent.submit(screen.getByTestId(INLINE_ITEM_EDITOR))
118+
119+
expect(onApplyMock).not.toBeCalled()
120+
})
121+
122+
it('should call onApply if form is valid', () => {
123+
const onApplyMock = jest.fn().mockReturnValue(false)
124+
render(
125+
<InlineItemEditor
126+
{...instance(mockedProps)}
127+
onApply={onApplyMock}
128+
onDecline={jest.fn()}
129+
/>
130+
)
131+
132+
fireEvent.change(screen.getByTestId(INLINE_ITEM_EDITOR), { target: { value: 'val123' } })
133+
134+
expect(screen.getByTestId('apply-btn')).not.toBeDisabled()
135+
136+
fireEvent.submit(screen.getByTestId(INLINE_ITEM_EDITOR))
137+
138+
expect(onApplyMock).toBeCalled()
139+
})
103140
})
104141
})

redisinsight/ui/src/components/inline-item-editor/InlineItemEditor.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ const InlineItemEditor = (props: Props) => {
158158
const handleFormSubmit = (event: React.MouseEvent<HTMLElement>): void => {
159159
event.preventDefault()
160160
event.stopPropagation()
161-
onApply(value, event)
161+
if (!isDisabledApply()) {
162+
onApply(value, event)
163+
}
162164
}
163165

164166
const isDisabledApply = (): boolean =>

0 commit comments

Comments
 (0)