Skip to content

Commit

Permalink
perf: JSONForm infinite function when setting internal error state wh…
Browse files Browse the repository at this point in the history
…en a field has error
  • Loading branch information
ashit-rath committed Aug 30, 2024
1 parent 67de8c1 commit 1d5dec6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,56 @@ describe("useRegisterFieldInvalid - setMetaInternalFieldState", () => {
expect(mockSetError).not.toBeCalled();
});

it("calls setError when isValid is false and error is not present", () => {
const mocksetMetaInternalFieldState = jest.fn();
const mockClearErrors = jest.fn();
const mockSetError = jest.fn();

function Wrapper({ children }: { children: React.ReactNode }) {
const methods = useForm();

return (
<FormContextProvider
executeAction={jest.fn}
renderMode="CANVAS"
setMetaInternalFieldState={mocksetMetaInternalFieldState}
updateFormData={jest.fn}
updateWidgetMetaProperty={jest.fn}
updateWidgetProperty={jest.fn}
>
<FormProvider
{...methods}
clearErrors={mockClearErrors}
setError={mockSetError}
>
{children}
</FormProvider>
</FormContextProvider>
);
}

const fieldName = "testField";

act(() => {
renderHook(
() =>
useRegisterFieldValidity({
isValid: false,
fieldName,
fieldType: FieldType.TEXT_INPUT,
}),
{
wrapper: Wrapper,
},
);
});

jest.runAllTimers();

expect(mockSetError).toBeCalledTimes(1);
expect(mockClearErrors).not.toBeCalledWith(fieldName);
});

it("updates fieldState and error state with the updated isValid value", () => {
const mocksetMetaInternalFieldState = jest.fn();
// TODO: Fix this the next time the file is edited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ function useRegisterFieldValidity({
});
}
} else {
startAndEndSpanForFn("JSONFormWidget.setError", {}, () => {
setError(fieldName, {
type: fieldType,
message: "Invalid field",
if (!error) {
startAndEndSpanForFn("JSONFormWidget.setError", {}, () => {
setError(fieldName, {
type: fieldType,
message: "Invalid field",
});
});
});
}
}
} catch (e) {
Sentry.captureException(e);
Expand Down

0 comments on commit 1d5dec6

Please sign in to comment.