Query History: Implement star, comment and delete methods (#49330)

* Implement star, comment and delete methods

* Remove redundant await
This commit is contained in:
Piotr Jamróz 2022-05-23 15:07:37 +02:00 committed by GitHub
parent 0a509b97ef
commit 8f4c3e94b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 3 deletions

View File

@ -18,10 +18,16 @@ dsMock.init(
); );
const fetchMock = jest.fn(); const fetchMock = jest.fn();
const postMock = jest.fn();
const deleteMock = jest.fn();
const patchMock = jest.fn();
jest.mock('@grafana/runtime', () => ({ jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'), ...jest.requireActual('@grafana/runtime'),
getBackendSrv: () => ({ getBackendSrv: () => ({
fetch: fetchMock, fetch: fetchMock,
post: postMock,
delete: deleteMock,
patch: patchMock,
}), }),
getDataSourceSrv: () => dsMock, getDataSourceSrv: () => dsMock,
})); }));
@ -41,6 +47,9 @@ describe('RichHistoryRemoteStorage', () => {
beforeEach(() => { beforeEach(() => {
fetchMock.mockReset(); fetchMock.mockReset();
postMock.mockReset();
deleteMock.mockReset();
patchMock.mockReset();
storage = new RichHistoryRemoteStorage(); storage = new RichHistoryRemoteStorage();
}); });
@ -173,4 +182,41 @@ describe('RichHistoryRemoteStorage', () => {
showSuccessAlert: false, showSuccessAlert: false,
}); });
}); });
it('stars query history items', async () => {
const { richHistoryQuery, dto } = setup();
postMock.mockResolvedValue({
result: dto,
});
const query = await storage.updateStarred('test', true);
expect(postMock).toBeCalledWith('/api/query-history/star/test');
expect(query).toMatchObject(richHistoryQuery);
});
it('unstars query history items', async () => {
const { richHistoryQuery, dto } = setup();
deleteMock.mockResolvedValue({
result: dto,
});
const query = await storage.updateStarred('test', false);
expect(deleteMock).toBeCalledWith('/api/query-history/star/test');
expect(query).toMatchObject(richHistoryQuery);
});
it('updates query history comments', async () => {
const { richHistoryQuery, dto } = setup();
patchMock.mockResolvedValue({
result: dto,
});
const query = await storage.updateComment('test', 'just a comment');
expect(patchMock).toBeCalledWith('/api/query-history/test', {
comment: 'just a comment',
});
expect(query).toMatchObject(richHistoryQuery);
});
it('deletes query history items', async () => {
await storage.deleteRichHistory('test');
expect(deleteMock).toBeCalledWith('/api/query-history/test');
});
}); });

View File

@ -38,6 +38,10 @@ type RichHistoryRemoteStorageResultsPayloadDTO = {
}; };
}; };
type RichHistoryRemoteStorageUpdatePayloadDTO = {
result: RichHistoryRemoteStorageDTO;
};
export default class RichHistoryRemoteStorage implements RichHistoryStorage { export default class RichHistoryRemoteStorage implements RichHistoryStorage {
private readonly preferenceService: PreferencesService; private readonly preferenceService: PreferencesService;
@ -62,7 +66,7 @@ export default class RichHistoryRemoteStorage implements RichHistoryStorage {
} }
async deleteRichHistory(id: string): Promise<void> { async deleteRichHistory(id: string): Promise<void> {
throw new Error('not supported yet'); getBackendSrv().delete(`/api/query-history/${id}`);
} }
async getRichHistory(filters: RichHistorySearchFilters) { async getRichHistory(filters: RichHistorySearchFilters) {
@ -95,7 +99,10 @@ export default class RichHistoryRemoteStorage implements RichHistoryStorage {
} }
async updateComment(id: string, comment: string | undefined): Promise<RichHistoryQuery> { async updateComment(id: string, comment: string | undefined): Promise<RichHistoryQuery> {
throw new Error('not supported yet'); const dto: RichHistoryRemoteStorageUpdatePayloadDTO = await getBackendSrv().patch(`/api/query-history/${id}`, {
comment: comment,
});
return fromDTO(dto.result);
} }
updateSettings(settings: RichHistorySettings): Promise<void> { updateSettings(settings: RichHistorySettings): Promise<void> {
@ -107,7 +114,13 @@ export default class RichHistoryRemoteStorage implements RichHistoryStorage {
} }
async updateStarred(id: string, starred: boolean): Promise<RichHistoryQuery> { async updateStarred(id: string, starred: boolean): Promise<RichHistoryQuery> {
throw new Error('not supported yet'); let dto: RichHistoryRemoteStorageUpdatePayloadDTO;
if (starred) {
dto = await getBackendSrv().post(`/api/query-history/star/${id}`);
} else {
dto = await getBackendSrv().delete(`/api/query-history/star/${id}`);
}
return fromDTO(dto.result);
} }
/** /**