diff --git a/.betterer.results b/.betterer.results index 0766c17123e..6e5f4a6438b 100644 --- a/.betterer.results +++ b/.betterer.results @@ -197,9 +197,6 @@ exports[`no enzyme tests`] = { "public/app/features/org/OrgProfile.test.tsx:623809345": [ [0, 19, 13, "RegExp match", "2409514259"] ], - "public/app/features/teams/CreateTeam.test.tsx:1750035593": [ - [0, 19, 13, "RegExp match", "2409514259"] - ], "public/app/features/teams/TeamGroupSync.test.tsx:2526985933": [ [0, 19, 13, "RegExp match", "2409514259"] ], diff --git a/public/app/features/teams/CreateTeam.test.tsx b/public/app/features/teams/CreateTeam.test.tsx index 77af350c6aa..5c8c2aa5f27 100644 --- a/public/app/features/teams/CreateTeam.test.tsx +++ b/public/app/features/teams/CreateTeam.test.tsx @@ -1,15 +1,62 @@ -import { shallow } from 'enzyme'; +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import React from 'react'; +import { NavModel } from '@grafana/data'; + import { CreateTeam, Props } from './CreateTeam'; -describe('Render', () => { - it('should render component', () => { - const props: Props = { - navModel: {} as any, - }; - const wrapper = shallow(); +beforeEach(() => { + jest.clearAllMocks(); +}); - expect(wrapper).toMatchSnapshot(); +const mockPost = jest.fn(() => { + return Promise.resolve({}); +}); + +jest.mock('@grafana/runtime', () => ({ + getBackendSrv: () => { + return { + post: mockPost, + }; + }, + config: { + buildInfo: {}, + licenseInfo: {}, + }, +})); + +const setup = () => { + const props: Props = { + navModel: { node: {}, main: {} } as NavModel, + }; + return render(); +}; + +describe('Create team', () => { + it('should render component', () => { + setup(); + expect(screen.getByText(/new team/i)).toBeInTheDocument(); + }); + + it('should send correct data to the server', async () => { + setup(); + await userEvent.type(screen.getByRole('textbox', { name: /name/i }), 'Test team'); + await userEvent.type(screen.getByLabelText(/email/i), 'team@test.com'); + await userEvent.click(screen.getByRole('button', { name: /create/i })); + await waitFor(() => { + expect(mockPost).toHaveBeenCalledWith(expect.anything(), { name: 'Test team', email: 'team@test.com' }); + }); + }); + + it('should validate required fields', async () => { + setup(); + await userEvent.type(screen.getByLabelText(/email/i), 'team@test.com'); + await userEvent.click(screen.getByRole('button', { name: /create/i })); + await waitFor(() => { + expect(mockPost).not.toBeCalled(); + }); + expect(screen.getAllByRole('alert')).toHaveLength(1); + expect(screen.getByText(/team name is required/i)).toBeInTheDocument(); }); }); diff --git a/public/app/features/teams/CreateTeam.tsx b/public/app/features/teams/CreateTeam.tsx index 7685e43c190..1c35b134e4b 100644 --- a/public/app/features/teams/CreateTeam.tsx +++ b/public/app/features/teams/CreateTeam.tsx @@ -3,7 +3,7 @@ import { connect } from 'react-redux'; import { NavModel } from '@grafana/data'; import { getBackendSrv, locationService } from '@grafana/runtime'; -import { Button, Form, Field, Input, FieldSet, Label, Tooltip, Icon } from '@grafana/ui'; +import { Button, Form, Field, Input, FieldSet } from '@grafana/ui'; import Page from 'app/core/components/Page/Page'; import { contextSrv } from 'app/core/core'; import { getNavModel } from 'app/core/selectors/navModel'; @@ -33,22 +33,16 @@ export class CreateTeam extends PureComponent {
- {({ register }) => ( + {({ register, errors }) => (
- + - Email - - - - - } + label={'Email'} + description={'This is optional and is primarily used for allowing custom team avatars.'} > - +