DataSourceSettingsPage.test.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { screen, render } from '@testing-library/react';
  2. import React from 'react';
  3. import { PluginState } from '@grafana/data';
  4. import { selectors } from '@grafana/e2e-selectors';
  5. import { cleanUpAction } from 'app/core/actions/cleanUp';
  6. import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
  7. import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
  8. import { getMockDataSource } from '../__mocks__/dataSourcesMocks';
  9. import { dataSourceLoaded, setDataSourceName, setIsDefault } from '../state/reducers';
  10. import { DataSourceSettingsPage, Props } from './DataSourceSettingsPage';
  11. jest.mock('app/core/core', () => {
  12. return {
  13. contextSrv: {
  14. hasPermission: () => true,
  15. hasPermissionInMetadata: () => true,
  16. },
  17. };
  18. });
  19. const getMockNode = () => ({
  20. text: 'text',
  21. subTitle: 'subtitle',
  22. icon: 'icon',
  23. });
  24. const getProps = (): Props => ({
  25. ...getRouteComponentProps(),
  26. navModel: {
  27. node: getMockNode(),
  28. main: getMockNode(),
  29. },
  30. dataSource: getMockDataSource(),
  31. dataSourceMeta: getMockPlugin(),
  32. dataSourceId: 'x',
  33. deleteDataSource: jest.fn(),
  34. loadDataSource: jest.fn(),
  35. setDataSourceName,
  36. updateDataSource: jest.fn(),
  37. initDataSourceSettings: jest.fn(),
  38. testDataSource: jest.fn(),
  39. setIsDefault,
  40. dataSourceLoaded,
  41. cleanUpAction,
  42. page: null,
  43. plugin: null,
  44. loadError: null,
  45. loading: false,
  46. testingStatus: {},
  47. });
  48. describe('Render', () => {
  49. it('should not render loading when props are ready', () => {
  50. render(<DataSourceSettingsPage {...getProps()} />);
  51. expect(screen.queryByText('Loading ...')).not.toBeInTheDocument();
  52. });
  53. it('should render loading if datasource is not ready', () => {
  54. const mockProps = getProps();
  55. mockProps.dataSource.id = 0;
  56. mockProps.loading = true;
  57. render(<DataSourceSettingsPage {...mockProps} />);
  58. expect(screen.getByText('Loading ...')).toBeInTheDocument();
  59. });
  60. it('should render beta info text if plugin state is beta', () => {
  61. const mockProps = getProps();
  62. mockProps.dataSourceMeta.state = PluginState.beta;
  63. render(<DataSourceSettingsPage {...mockProps} />);
  64. expect(screen.getByTitle('This feature is close to complete but not fully tested')).toBeInTheDocument();
  65. });
  66. it('should render alpha info text if plugin state is alpha', () => {
  67. const mockProps = getProps();
  68. mockProps.dataSourceMeta.state = PluginState.alpha;
  69. render(<DataSourceSettingsPage {...mockProps} />);
  70. expect(
  71. screen.getByTitle('This feature is experimental and future updates might not be backward compatible')
  72. ).toBeInTheDocument();
  73. });
  74. it('should not render is ready only message is readOnly is false', () => {
  75. const mockProps = getProps();
  76. mockProps.dataSource.readOnly = false;
  77. render(<DataSourceSettingsPage {...mockProps} />);
  78. expect(screen.queryByLabelText(selectors.pages.DataSource.readOnly)).not.toBeInTheDocument();
  79. });
  80. it('should render is ready only message is readOnly is true', () => {
  81. const mockProps = getProps();
  82. mockProps.dataSource.readOnly = true;
  83. render(<DataSourceSettingsPage {...mockProps} />);
  84. expect(screen.getByLabelText(selectors.pages.DataSource.readOnly)).toBeInTheDocument();
  85. });
  86. it('should render error message with detailed message', () => {
  87. const mockProps = {
  88. ...getProps(),
  89. testingStatus: {
  90. message: 'message',
  91. status: 'error',
  92. details: { message: 'detailed message' },
  93. },
  94. };
  95. render(<DataSourceSettingsPage {...mockProps} />);
  96. expect(screen.getByText(mockProps.testingStatus.message)).toBeInTheDocument();
  97. expect(screen.getByText(mockProps.testingStatus.details.message)).toBeInTheDocument();
  98. });
  99. it('should render error message with empty details', () => {
  100. const mockProps = {
  101. ...getProps(),
  102. testingStatus: {
  103. message: 'message',
  104. status: 'error',
  105. details: {},
  106. },
  107. };
  108. render(<DataSourceSettingsPage {...mockProps} />);
  109. expect(screen.getByText(mockProps.testingStatus.message)).toBeInTheDocument();
  110. });
  111. it('should render error message without details', () => {
  112. const mockProps = {
  113. ...getProps(),
  114. testingStatus: {
  115. message: 'message',
  116. status: 'error',
  117. },
  118. };
  119. render(<DataSourceSettingsPage {...mockProps} />);
  120. expect(screen.getByText(mockProps.testingStatus.message)).toBeInTheDocument();
  121. });
  122. it('should render verbose error message with detailed verbose error message', () => {
  123. const mockProps = {
  124. ...getProps(),
  125. testingStatus: {
  126. message: 'message',
  127. status: 'error',
  128. details: { message: 'detailed message', verboseMessage: 'verbose message' },
  129. },
  130. };
  131. render(<DataSourceSettingsPage {...mockProps} />);
  132. expect(screen.getByText(mockProps.testingStatus.details.verboseMessage)).toBeInTheDocument();
  133. });
  134. });