2022-05-25 05:01:40 -05:00
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-04-22 08:33:13 -05:00
|
|
|
import React from 'react';
|
2023-02-02 02:53:06 -06:00
|
|
|
import { TestProvider } from 'test/helpers/TestProvider';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { contextSrv, User } from 'app/core/services/context_srv';
|
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
import { OrgRole, Team } from '../../types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { Props, TeamList } from './TeamList';
|
2018-09-11 07:14:03 -05:00
|
|
|
import { getMockTeam, getMultipleMockTeams } from './__mocks__/teamMocks';
|
|
|
|
|
2022-07-20 02:25:09 -05:00
|
|
|
jest.mock('app/core/config', () => ({
|
|
|
|
...jest.requireActual('app/core/config'),
|
|
|
|
featureToggles: { accesscontrol: false },
|
|
|
|
}));
|
2022-01-10 11:05:53 -06:00
|
|
|
|
2018-09-11 07:14:03 -05:00
|
|
|
const setup = (propOverrides?: object) => {
|
|
|
|
const props: Props = {
|
|
|
|
teams: [] as Team[],
|
2022-11-16 08:55:10 -06:00
|
|
|
noTeams: false,
|
2018-09-11 07:14:03 -05:00
|
|
|
loadTeams: jest.fn(),
|
|
|
|
deleteTeam: jest.fn(),
|
2022-11-16 08:55:10 -06:00
|
|
|
changePage: jest.fn(),
|
|
|
|
changeQuery: jest.fn(),
|
|
|
|
query: '',
|
|
|
|
page: 1,
|
|
|
|
totalPages: 0,
|
2018-10-11 04:49:34 -05:00
|
|
|
hasFetched: false,
|
2019-03-14 02:21:53 -05:00
|
|
|
editorsCanAdmin: false,
|
|
|
|
signedInUser: {
|
|
|
|
id: 1,
|
|
|
|
orgRole: OrgRole.Viewer,
|
|
|
|
} as User,
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(props, propOverrides);
|
|
|
|
|
2022-01-10 11:05:53 -06:00
|
|
|
contextSrv.user = props.signedInUser;
|
|
|
|
|
2022-11-22 10:48:07 -06:00
|
|
|
render(
|
2023-02-02 02:53:06 -06:00
|
|
|
<TestProvider>
|
2022-11-22 10:48:07 -06:00
|
|
|
<TeamList {...props} />
|
2023-02-02 02:53:06 -06:00
|
|
|
</TestProvider>
|
2022-11-22 10:48:07 -06:00
|
|
|
);
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
|
2022-05-25 05:01:40 -05:00
|
|
|
describe('TeamList', () => {
|
2018-09-11 07:14:03 -05:00
|
|
|
it('should render teams table', () => {
|
2022-05-25 05:01:40 -05:00
|
|
|
setup({ teams: getMultipleMockTeams(5), teamsCount: 5, hasFetched: true });
|
|
|
|
expect(screen.getAllByRole('row')).toHaveLength(6); // 5 teams plus table header row
|
2018-09-11 07:14:03 -05:00
|
|
|
});
|
2019-03-14 02:21:53 -05:00
|
|
|
|
|
|
|
describe('when feature toggle editorsCanAdmin is turned on', () => {
|
2022-05-25 05:01:40 -05:00
|
|
|
describe('and signed in user is not viewer', () => {
|
2019-03-14 02:21:53 -05:00
|
|
|
it('should enable the new team button', () => {
|
2022-05-25 05:01:40 -05:00
|
|
|
setup({
|
2019-03-14 02:21:53 -05:00
|
|
|
teams: getMultipleMockTeams(1),
|
2022-11-16 08:55:10 -06:00
|
|
|
totalCount: 1,
|
2019-03-14 02:21:53 -05:00
|
|
|
hasFetched: true,
|
|
|
|
editorsCanAdmin: true,
|
|
|
|
signedInUser: {
|
|
|
|
id: 1,
|
|
|
|
orgRole: OrgRole.Editor,
|
|
|
|
} as User,
|
|
|
|
});
|
|
|
|
|
2022-05-25 05:01:40 -05:00
|
|
|
expect(screen.getByRole('link', { name: /new team/i })).not.toHaveStyle('pointer-events: none');
|
2019-03-14 02:21:53 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-25 05:01:40 -05:00
|
|
|
describe('and signed in user is a viewer', () => {
|
2019-03-14 02:21:53 -05:00
|
|
|
it('should disable the new team button', () => {
|
2022-05-25 05:01:40 -05:00
|
|
|
setup({
|
2019-03-14 02:21:53 -05:00
|
|
|
teams: getMultipleMockTeams(1),
|
2022-11-16 08:55:10 -06:00
|
|
|
totalCount: 1,
|
2019-03-14 02:21:53 -05:00
|
|
|
hasFetched: true,
|
|
|
|
editorsCanAdmin: true,
|
|
|
|
signedInUser: {
|
|
|
|
id: 1,
|
|
|
|
orgRole: OrgRole.Viewer,
|
|
|
|
} as User,
|
|
|
|
});
|
|
|
|
|
2022-05-25 05:01:40 -05:00
|
|
|
expect(screen.getByRole('link', { name: /new team/i })).toHaveStyle('pointer-events: none');
|
2019-03-14 02:21:53 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-11 07:14:03 -05:00
|
|
|
});
|
|
|
|
|
2022-05-25 05:01:40 -05:00
|
|
|
it('should call delete team', async () => {
|
|
|
|
const mockDelete = jest.fn();
|
|
|
|
const mockTeam = getMockTeam();
|
2022-11-16 08:55:10 -06:00
|
|
|
setup({ deleteTeam: mockDelete, teams: [mockTeam], totalCount: 1, hasFetched: true });
|
2022-05-25 05:01:40 -05:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: `Delete team ${mockTeam.name}` }));
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Delete' }));
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(mockDelete).toHaveBeenCalledWith(mockTeam.id);
|
2018-09-11 07:14:03 -05:00
|
|
|
});
|
|
|
|
});
|