QueryRows.test.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { fireEvent, render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { Provider } from 'react-redux';
  4. import { setDataSourceSrv } from '@grafana/runtime';
  5. import { configureStore } from 'app/store/configureStore';
  6. import { ExploreId, ExploreState } from 'app/types';
  7. import { DataQuery } from '../../../../packages/grafana-data/src';
  8. import { UserState } from '../profile/state/reducers';
  9. import { QueryRows } from './QueryRows';
  10. import { makeExplorePaneState } from './state/utils';
  11. function setup(queries: DataQuery[]) {
  12. const defaultDs = {
  13. name: 'newDs',
  14. uid: 'newDs-uid',
  15. meta: { id: 'newDs' },
  16. };
  17. const datasources: Record<string, any> = {
  18. 'newDs-uid': defaultDs,
  19. 'someDs-uid': {
  20. name: 'someDs',
  21. uid: 'someDs-uid',
  22. meta: { id: 'someDs' },
  23. components: {
  24. QueryEditor: () => 'someDs query editor',
  25. },
  26. },
  27. };
  28. setDataSourceSrv({
  29. getList() {
  30. return Object.values(datasources).map((d) => ({ name: d.name }));
  31. },
  32. getInstanceSettings(uid: string) {
  33. return datasources[uid] || defaultDs;
  34. },
  35. get(uid?: string) {
  36. return Promise.resolve(uid ? datasources[uid] || defaultDs : defaultDs);
  37. },
  38. } as any);
  39. const leftState = makeExplorePaneState();
  40. const initialState: ExploreState = {
  41. left: {
  42. ...leftState,
  43. richHistory: [],
  44. datasourceInstance: datasources['someDs-uid'],
  45. queries,
  46. },
  47. syncedTimes: false,
  48. right: undefined,
  49. richHistoryStorageFull: false,
  50. richHistoryLimitExceededWarningShown: false,
  51. richHistoryMigrationFailed: false,
  52. };
  53. const store = configureStore({ explore: initialState, user: { orgId: 1 } as UserState });
  54. return {
  55. store,
  56. datasources,
  57. };
  58. }
  59. describe('Explore QueryRows', () => {
  60. it('Should duplicate a query and generate a valid refId', async () => {
  61. const { store } = setup([{ refId: 'A' }]);
  62. render(
  63. <Provider store={store}>
  64. <QueryRows exploreId={ExploreId.left} />
  65. </Provider>
  66. );
  67. // waiting for the d&d component to fully render.
  68. await screen.findAllByText('someDs query editor');
  69. let duplicateButton = screen.getByTitle('Duplicate query');
  70. fireEvent.click(duplicateButton);
  71. // We should have another row with refId B
  72. expect(await screen.findByLabelText('Query editor row title B')).toBeInTheDocument();
  73. });
  74. });