CreateTeam.test.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { NavModel } from '@grafana/data';
  5. import { CreateTeam, Props } from './CreateTeam';
  6. beforeEach(() => {
  7. jest.clearAllMocks();
  8. });
  9. const mockPost = jest.fn(() => {
  10. return Promise.resolve({});
  11. });
  12. jest.mock('@grafana/runtime', () => ({
  13. getBackendSrv: () => {
  14. return {
  15. post: mockPost,
  16. };
  17. },
  18. config: {
  19. buildInfo: {},
  20. licenseInfo: {},
  21. },
  22. }));
  23. const setup = () => {
  24. const props: Props = {
  25. navModel: { node: {}, main: {} } as NavModel,
  26. };
  27. return render(<CreateTeam {...props} />);
  28. };
  29. describe('Create team', () => {
  30. it('should render component', () => {
  31. setup();
  32. expect(screen.getByText(/new team/i)).toBeInTheDocument();
  33. });
  34. it('should send correct data to the server', async () => {
  35. setup();
  36. await userEvent.type(screen.getByRole('textbox', { name: /name/i }), 'Test team');
  37. await userEvent.type(screen.getByLabelText(/email/i), 'team@test.com');
  38. await userEvent.click(screen.getByRole('button', { name: /create/i }));
  39. await waitFor(() => {
  40. expect(mockPost).toHaveBeenCalledWith(expect.anything(), { name: 'Test team', email: 'team@test.com' });
  41. });
  42. });
  43. it('should validate required fields', async () => {
  44. setup();
  45. await userEvent.type(screen.getByLabelText(/email/i), 'team@test.com');
  46. await userEvent.click(screen.getByRole('button', { name: /create/i }));
  47. await waitFor(() => {
  48. expect(mockPost).not.toBeCalled();
  49. });
  50. expect(screen.getAllByRole('alert')).toHaveLength(1);
  51. expect(screen.getByText(/team name is required/i)).toBeInTheDocument();
  52. });
  53. });