From 97235c855d1a8cfb8a1299f246ac7cd9322524a9 Mon Sep 17 00:00:00 2001 From: vitPinchuk Date: Tue, 18 Jun 2024 10:30:50 +0300 Subject: [PATCH] fix reset action issue --- src/components/FormPanel/FormPanel.test.tsx | 90 +++++++++++++++++++++ src/components/FormPanel/FormPanel.tsx | 2 + 2 files changed, 92 insertions(+) diff --git a/src/components/FormPanel/FormPanel.test.tsx b/src/components/FormPanel/FormPanel.test.tsx index 0a130312..ff5b7285 100644 --- a/src/components/FormPanel/FormPanel.test.tsx +++ b/src/components/FormPanel/FormPanel.test.tsx @@ -1727,6 +1727,7 @@ describe('Panel', () => { }); expect(replaceVariables).toHaveBeenCalledWith(defaultOptions.resetAction.code); + expect(replaceVariables).toHaveBeenCalledTimes(1); expect(appEventsMock.publish).toHaveBeenCalledWith({ type: AppEvents.alertSuccess.name, payload: 'success', @@ -1835,6 +1836,95 @@ describe('Panel', () => { expect(replaceVariables).toHaveBeenCalledTimes(1); }); + it('Should not run reset datasource request if datasource not specified', async () => { + /** + * Render + */ + jest.mocked(fetch).mockImplementationOnce( + () => + Promise.resolve({ + ok: true, + json: jest.fn(() => + Promise.resolve({ + test: '123', + number: 123, + }) + ), + }) as any + ); + + const datasourceRequestMock = jest.fn(() => + Promise.resolve({ + data: [], + state: LoadingState.Done, + }) + ) as any; + jest.mocked(useDatasourceRequest).mockImplementation(() => datasourceRequestMock); + + const { rerender } = await act(async () => + render( + getComponent({ + options: { + elements: [ + { ...FORM_ELEMENT_DEFAULT, id: 'test', value: '123' }, + { type: FormElementType.NUMBER, id: 'number', value: 123 }, + ], + }, + }) + ) + ); + /** + * Trigger element updates + */ + await act(async () => + rerender( + getComponent({ + options: { + elements: [ + { ...FORM_ELEMENT_DEFAULT, id: 'test', value: '123' }, + { type: FormElementType.NUMBER, id: 'number', value: 111 }, + { type: FormElementType.DISABLED, id: 'disabled', value: '222' }, + ], + resetAction: { + datasource: '', + mode: ResetActionMode.DATASOURCE, + payloadMode: PayloadMode.CUSTOM, + getPayload: `return { key1: 'value' }`, + payload: { + sql: 'select *;', + }, + }, + }, + }) + ) + ); + + /** + * Check if Reset can be run + */ + expect(selectors.buttonReset()).toBeInTheDocument(); + expect(selectors.buttonReset()).not.toBeDisabled(); + + /** + * Reset replaceVariables calls count + */ + replaceVariables.mockClear(); + + /** + * Run reset request + */ + await act(async () => { + fireEvent.click(selectors.buttonReset()); + }); + + expect(datasourceRequestMock).not.toHaveBeenCalled(); + + /** + * Check if replace variables called for get payload function + */ + expect(replaceVariables).toHaveBeenCalledTimes(0); + }); + it('Should show reset datasource request error', async () => { /** * Render diff --git a/src/components/FormPanel/FormPanel.tsx b/src/components/FormPanel/FormPanel.tsx index a209d489..05859f79 100644 --- a/src/components/FormPanel/FormPanel.tsx +++ b/src/components/FormPanel/FormPanel.tsx @@ -523,6 +523,8 @@ export const FormPanel: React.FC = ({ */ await executeCustomCode({ code: options.resetAction.code, initial: initialRef.current }); setLoading(LoadingMode.NONE); + + return; } /**