TeamMemberRow.test.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { shallow } from 'enzyme';
  2. import React from 'react';
  3. import { SelectableValue } from '@grafana/data';
  4. import { TeamMember, TeamPermissionLevel } from '../../types';
  5. import { TeamMemberRow, Props } from './TeamMemberRow';
  6. import { getMockTeamMember } from './__mocks__/teamMocks';
  7. const setup = (propOverrides?: object) => {
  8. const props: Props = {
  9. member: getMockTeamMember(),
  10. syncEnabled: false,
  11. editorsCanAdmin: false,
  12. signedInUserIsTeamAdmin: false,
  13. updateTeamMember: jest.fn(),
  14. removeTeamMember: jest.fn(),
  15. };
  16. Object.assign(props, propOverrides);
  17. const wrapper = shallow(<TeamMemberRow {...props} />);
  18. const instance = wrapper.instance() as TeamMemberRow;
  19. return {
  20. wrapper,
  21. instance,
  22. };
  23. };
  24. describe('Render', () => {
  25. it('should render team members when sync enabled', () => {
  26. const member = getMockTeamMember();
  27. member.labels = ['LDAP'];
  28. const { wrapper } = setup({ member, syncEnabled: true });
  29. expect(wrapper).toMatchSnapshot();
  30. });
  31. describe('when feature toggle editorsCanAdmin is turned on', () => {
  32. it('should render permissions select if user is team admin', () => {
  33. const { wrapper } = setup({ editorsCanAdmin: true, signedInUserIsTeamAdmin: true });
  34. expect(wrapper).toMatchSnapshot();
  35. });
  36. it('should render span and disable buttons if user is team member', () => {
  37. const { wrapper } = setup({ editorsCanAdmin: true, signedInUserIsTeamAdmin: false });
  38. expect(wrapper).toMatchSnapshot();
  39. });
  40. });
  41. describe('when feature toggle editorsCanAdmin is turned off', () => {
  42. it('should not render permissions', () => {
  43. const { wrapper } = setup({ editorsCanAdmin: false, signedInUserIsTeamAdmin: true });
  44. expect(wrapper).toMatchSnapshot();
  45. });
  46. });
  47. });
  48. describe('Functions', () => {
  49. describe('on remove member', () => {
  50. const member = getMockTeamMember();
  51. const { instance } = setup({ member });
  52. instance.onRemoveMember(member);
  53. expect(instance.props.removeTeamMember).toHaveBeenCalledWith(1);
  54. });
  55. describe('on update permision for user in team', () => {
  56. const member: TeamMember = {
  57. userId: 3,
  58. teamId: 2,
  59. avatarUrl: '',
  60. email: 'user@user.org',
  61. login: 'member',
  62. name: 'member',
  63. labels: [],
  64. permission: TeamPermissionLevel.Member,
  65. };
  66. const { instance } = setup({ member });
  67. const permission = TeamPermissionLevel.Admin;
  68. const item: SelectableValue<TeamPermissionLevel> = { value: permission };
  69. const expectedTeamMemeber = { ...member, permission };
  70. instance.onPermissionChange(item, member);
  71. expect(instance.props.updateTeamMember).toHaveBeenCalledWith(expectedTeamMemeber);
  72. });
  73. });