Skip to content

Commit

Permalink
fix reset action issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vitPinchuk committed Jun 18, 2024
1 parent 651868e commit 97235c8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/components/FormPanel/FormPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/components/FormPanel/FormPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ export const FormPanel: React.FC<Props> = ({
*/
await executeCustomCode({ code: options.resetAction.code, initial: initialRef.current });
setLoading(LoadingMode.NONE);

return;
}

/**
Expand Down

0 comments on commit 97235c8

Please sign in to comment.