grafana/public/app/features/explore/QueryRows.test.tsx
Piotr Jamróz 6bb6b13379
Query History: Refactor persistence layer (#44545)
* Extract Rich History storage into two separate implementations

Implement getting all entries and adding a new entry

* Add deleting rich history items

* Add editing rich history

* Simplify RichHistoryStorage API

* Reorganize methods

* Rename variable

* Remove feature toggle

* Fix TS errors

* Fix tests

* Clean up

* Clean up only when adding new entry

* Fix showing a warning message

* Use enum instead of a string

* Update public/app/core/history/richHistoryLocalStorage.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Update public/app/core/history/richHistoryLocalStorage.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Improve readability

* Rename files and remove inferred return types

* Use const over a var

* Remove unsed files

* Remove redundant null check

* Update public/app/core/history/richHistoryLocalStorageUtils.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Fix linting issues

Co-authored-by: Giordano Ricci <me@giordanoricci.com>
2022-02-04 12:46:27 +01:00

85 lines
2.3 KiB
TypeScript

import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { configureStore } from 'app/store/configureStore';
import { Provider } from 'react-redux';
import { QueryRows } from './QueryRows';
import { ExploreId, ExploreState } from 'app/types';
import { makeExplorePaneState } from './state/utils';
import { setDataSourceSrv } from '@grafana/runtime';
import { UserState } from '../profile/state/reducers';
import { DataQuery } from '../../../../packages/grafana-data/src';
function setup(queries: DataQuery[]) {
const defaultDs = {
name: 'newDs',
uid: 'newDs-uid',
meta: { id: 'newDs' },
};
const datasources: Record<string, any> = {
'newDs-uid': defaultDs,
'someDs-uid': {
name: 'someDs',
uid: 'someDs-uid',
meta: { id: 'someDs' },
components: {
QueryEditor: () => 'someDs query editor',
},
},
};
setDataSourceSrv({
getList() {
return Object.values(datasources).map((d) => ({ name: d.name }));
},
getInstanceSettings(uid: string) {
return datasources[uid] || defaultDs;
},
get(uid?: string) {
return Promise.resolve(uid ? datasources[uid] || defaultDs : defaultDs);
},
} as any);
const leftState = makeExplorePaneState();
const initialState: ExploreState = {
left: {
...leftState,
datasourceInstance: datasources['someDs-uid'],
queries,
},
syncedTimes: false,
right: undefined,
richHistory: [],
richHistoryStorageFull: false,
richHistoryLimitExceededWarningShown: false,
};
const store = configureStore({ explore: initialState, user: { orgId: 1 } as UserState });
return {
store,
datasources,
};
}
describe('Explore QueryRows', () => {
it('Should duplicate a query and generate a valid refId', async () => {
const { store } = setup([{ refId: 'A' }]);
render(
<Provider store={store}>
<QueryRows exploreId={ExploreId.left} />
</Provider>
);
// waiting for the d&d component to fully render.
await screen.findAllByText('someDs query editor');
let duplicateButton = screen.getByTitle('Duplicate query');
fireEvent.click(duplicateButton);
// We should have another row with refId B
expect(await screen.findByLabelText('Query editor row title B')).toBeInTheDocument();
});
});