mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Add TeamRolePicker to CreateTeam and TeamSettings pages * Align tests to the changes * Change TeamRolePicker * Add useRoleOptions hook * Clean up * Requested changes by reviewers * Fixes * Fixes
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { render, screen, waitFor } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import React from 'react';
|
|
|
|
import { BackendSrv, setBackendSrv } from '@grafana/runtime';
|
|
|
|
import { CreateTeam } from './CreateTeam';
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
jest.mock('app/core/core', () => ({
|
|
contextSrv: {
|
|
licensedAccessControlEnabled: () => false,
|
|
hasPermission: () => true,
|
|
hasPermissionInMetadata: () => true,
|
|
user: { orgId: 1 },
|
|
},
|
|
}));
|
|
|
|
jest.mock('app/core/components/RolePicker/hooks', () => ({
|
|
useRoleOptions: jest.fn().mockReturnValue([{ roleOptions: [] }, jest.fn()]),
|
|
}));
|
|
|
|
const mockPost = jest.fn(() => {
|
|
return Promise.resolve({});
|
|
});
|
|
|
|
setBackendSrv({
|
|
post: mockPost,
|
|
} as any as BackendSrv);
|
|
|
|
const setup = () => {
|
|
return render(<CreateTeam />);
|
|
};
|
|
|
|
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();
|
|
});
|
|
});
|