grafana/public/app/features/teams/TeamList.test.tsx

101 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-05-25 05:01:40 -05:00
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { TestProvider } from 'test/helpers/TestProvider';
import { contextSrv, User } from 'app/core/services/context_srv';
import { OrgRole, Team } from '../../types';
import { Props, TeamList } from './TeamList';
import { getMockTeam, getMultipleMockTeams } from './__mocks__/teamMocks';
DataSources: refactor datasource pages to be reusable (#51874) * refactor: move utility functions out of the redux actions * refactor: move password handlers to the feature root * refactor: move API related functions to an api.ts * refactor: move components under a /components folder * refactor: move page containers under a /pages folder and extract components * refactor: update mocks to be easier to reuse * refactor: move tests into a state/tests/ subfolder * refactor: expose 'initialState' for plugins * refactor: move generic types to the root folder of the feature * refactor: import path fixe * refactor: update import paths for app routes * chore: update betterer * refactor: fix type errors due to changed mock functions * chore: fix mocking context_srv in tests * refactor: udpate imports to be more concise * fix: update failing test because of mocks * refactor: use the new `navId` prop where we can * fix: use UID instead ID in datasource edit links * fix:clean up Redux state when unmounting the edit page * refactor: use `uid` instead of `id` * refactor: always fetch the plugin details when editing a datasource The deleted lines could provide performance benefits, although they also make the implementation more prone to errors. (Mostly because we are storing the information about the currently loaded plugin in a single field, and it was not validating if it is for the latest one). We are planning to introduce some kind of caching, but first we would like to clean up the underlying state a bit (plugins & datasources. * fix: add missing dispatch() wrapper for update datasource callback * refactor: prefer using absolute import paths Co-authored-by: Ryan McKinley <ryantxu@gmail.com> * fix: ESLINT import order issue * refactor: put test files next to their files * refactor: use implicit return types for components * fix: remove caching from datasource fetching I have introduced a cache to only fetch data-sources once, however as we are missing a good logic for updating the instances in the Redux store when they change (create, update, delete), this approach is not keeping the UI in sync. Due to this reason I have removed the caching for now, and will reintroduce it once we have a more robust client-side state logic. Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2022-07-20 02:25:09 -05:00
jest.mock('app/core/config', () => ({
...jest.requireActual('app/core/config'),
featureToggles: { accesscontrol: false },
}));
const setup = (propOverrides?: object) => {
const props: Props = {
teams: [] as Team[],
noTeams: false,
loadTeams: jest.fn(),
deleteTeam: jest.fn(),
changePage: jest.fn(),
changeQuery: jest.fn(),
query: '',
page: 1,
totalPages: 0,
2018-10-11 04:49:34 -05:00
hasFetched: false,
editorsCanAdmin: false,
signedInUser: {
id: 1,
orgRole: OrgRole.Viewer,
} as User,
};
Object.assign(props, propOverrides);
contextSrv.user = props.signedInUser;
render(
<TestProvider>
<TeamList {...props} />
</TestProvider>
);
};
2022-05-25 05:01:40 -05:00
describe('TeamList', () => {
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
});
describe('when feature toggle editorsCanAdmin is turned on', () => {
2022-05-25 05:01:40 -05:00
describe('and signed in user is not viewer', () => {
it('should enable the new team button', () => {
2022-05-25 05:01:40 -05:00
setup({
teams: getMultipleMockTeams(1),
totalCount: 1,
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');
});
});
2022-05-25 05:01:40 -05:00
describe('and signed in user is a viewer', () => {
it('should disable the new team button', () => {
2022-05-25 05:01:40 -05:00
setup({
teams: getMultipleMockTeams(1),
totalCount: 1,
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');
});
});
});
});
2022-05-25 05:01:40 -05:00
it('should call delete team', async () => {
const mockDelete = jest.fn();
const mockTeam = getMockTeam();
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);
});
});