AddDataSourcePermissions.test.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import selectEvent from 'react-select-event';
  4. import { AclTarget } from 'app/types';
  5. import { AddDataSourcePermissions, Props } from './AddDataSourcePermissions';
  6. jest.mock('app/core/core', () => {
  7. return {
  8. contextSrv: {
  9. hasPermission: () => true,
  10. },
  11. };
  12. });
  13. jest.mock('@grafana/runtime', () => ({
  14. getBackendSrv: () => {
  15. return {
  16. get: () => {
  17. return Promise.resolve([]);
  18. },
  19. };
  20. },
  21. config: {
  22. featureToggles: {},
  23. },
  24. }));
  25. beforeEach(() => {
  26. jest.clearAllMocks();
  27. });
  28. const setup = () => {
  29. const props: Props = {
  30. onAddPermission: jest.fn(),
  31. onCancel: jest.fn(),
  32. };
  33. render(<AddDataSourcePermissions {...props} />);
  34. };
  35. const selectTarget = async (target: string) => {
  36. await selectEvent.select(screen.getByLabelText('Role to add new permission to'), target, {
  37. container: document.body,
  38. });
  39. };
  40. describe('Render', () => {
  41. it('should render component', async () => {
  42. setup();
  43. expect(await screen.findByText(/add permission for/i)).toBeInTheDocument();
  44. });
  45. it('should render user picker', async () => {
  46. setup();
  47. await selectTarget(AclTarget.User);
  48. expect(await screen.findByTestId('userPicker')).toBeInTheDocument();
  49. });
  50. it('should render team picker', async () => {
  51. setup();
  52. await selectTarget(AclTarget.Team);
  53. expect(await screen.findByTestId('teamPicker')).toBeInTheDocument();
  54. });
  55. });