Skip to content

Commit

Permalink
Merge branch 'main' into feat/confiramtion-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-vl committed Jun 21, 2024
2 parents 77bb43a + 14ad92a commit 386b2f9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
### Features / Enhancements

- Updated name to Business Forms Panel (#361)
- Add support frontend data sources (#361)
- Added support frontend data sources (#361)
- Prepare for Grafana 11 (#399)
- Updated Reset button handler (#422)

## 3.8.0 (2024-05-30)

Expand Down
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 @@ -524,6 +524,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 386b2f9

Please sign in to comment.