DataSourcePermissions.test.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { NavModel } from '@grafana/data';
  4. import { DataSourcePermission } from '../types';
  5. import { DataSourcePermissions, Props } from './DataSourcePermissions';
  6. jest.mock('app/core/core', () => {
  7. return {
  8. contextSrv: {
  9. hasPermission: () => true,
  10. },
  11. };
  12. });
  13. jest.mock('app/features/plugins/datasource_srv', () => {
  14. return {
  15. getDatasourceSrv: () => {
  16. return {};
  17. },
  18. };
  19. });
  20. jest.mock('@grafana/runtime', () => ({
  21. getBackendSrv: () => {
  22. return {
  23. get: () => {
  24. return Promise.resolve([]);
  25. },
  26. };
  27. },
  28. config: {
  29. buildInfo: {},
  30. licenseInfo: {},
  31. getConfig: () => {},
  32. config: {},
  33. featureToggles: {},
  34. },
  35. }));
  36. jest.mock('@grafana/runtime/src/config', () => ({
  37. config: {
  38. buildInfo: {},
  39. licenseInfo: {},
  40. getConfig: () => {},
  41. config: {},
  42. featureToggles: {},
  43. },
  44. }));
  45. beforeEach(() => {
  46. jest.clearAllMocks();
  47. });
  48. const setup = (propOverrides?: object) => {
  49. const props: Props = {
  50. permissions: [] as DataSourcePermission[],
  51. enabled: true,
  52. isDefault: false,
  53. uid: '1',
  54. navModel: { node: {}, main: {} } as NavModel,
  55. addDataSourcePermission: jest.fn(),
  56. enableDataSourcePermissions: jest.fn(),
  57. disableDataSourcePermissions: jest.fn(),
  58. loadDataSourcePermissions: jest.fn(),
  59. loadDataSource: async () => jest.fn((uid: string) => ({ id: 1 } as any)) as any,
  60. loadDataSourceMeta: jest.fn(),
  61. removeDataSourcePermission: jest.fn(),
  62. };
  63. Object.assign(props, propOverrides);
  64. render(<DataSourcePermissions {...props} />);
  65. };
  66. describe('Render', () => {
  67. it('should render component', () => {
  68. setup({ enabled: false });
  69. expect(screen.getByText('Enable')).toBeInTheDocument();
  70. });
  71. it('should render permissions enabled', async () => {
  72. setup({
  73. dataSourcePermission: {
  74. enabled: true,
  75. uid: '1',
  76. permissions: [] as DataSourcePermission[],
  77. },
  78. });
  79. expect(await screen.findByRole('button', { name: /Add a permission/i })).toBeInTheDocument();
  80. expect(screen.getByRole('button', { name: /Disable permissions/i })).toBeInTheDocument();
  81. });
  82. });