selectors.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { User } from 'app/core/services/context_srv';
  2. import { Team, TeamGroup, TeamsState, TeamState, OrgRole } from '../../../types';
  3. import { getMockTeam, getMockTeamMembers, getMultipleMockTeams } from '../__mocks__/teamMocks';
  4. import { getTeam, getTeamMembers, getTeams, isSignedInUserTeamAdmin, Config } from './selectors';
  5. describe('Teams selectors', () => {
  6. describe('Get teams', () => {
  7. const mockTeams = getMultipleMockTeams(5);
  8. it('should return teams if no search query', () => {
  9. const mockState: TeamsState = { teams: mockTeams, searchQuery: '', searchPage: 1, hasFetched: false };
  10. const teams = getTeams(mockState);
  11. expect(teams).toEqual(mockTeams);
  12. });
  13. it('Should filter teams if search query', () => {
  14. const mockState: TeamsState = { teams: mockTeams, searchQuery: '5', searchPage: 1, hasFetched: false };
  15. const teams = getTeams(mockState);
  16. expect(teams.length).toEqual(1);
  17. });
  18. });
  19. });
  20. describe('Team selectors', () => {
  21. describe('Get team', () => {
  22. const mockTeam = getMockTeam();
  23. it('should return team if matching with location team', () => {
  24. const mockState: TeamState = {
  25. team: mockTeam,
  26. searchMemberQuery: '',
  27. members: [],
  28. groups: [],
  29. };
  30. const team = getTeam(mockState, '1');
  31. expect(team).toEqual(mockTeam);
  32. });
  33. });
  34. describe('Get members', () => {
  35. const mockTeamMembers = getMockTeamMembers(5, 5);
  36. it('should return team members', () => {
  37. const mockState: TeamState = {
  38. team: {} as Team,
  39. searchMemberQuery: '',
  40. members: mockTeamMembers,
  41. groups: [] as TeamGroup[],
  42. };
  43. const members = getTeamMembers(mockState);
  44. expect(members).toEqual(mockTeamMembers);
  45. });
  46. });
  47. });
  48. const signedInUserId = 1;
  49. const setup = (configOverrides?: Partial<Config>) => {
  50. const defaultConfig: Config = {
  51. editorsCanAdmin: false,
  52. members: getMockTeamMembers(5, 5),
  53. signedInUser: {
  54. id: signedInUserId,
  55. isGrafanaAdmin: false,
  56. orgRole: OrgRole.Viewer,
  57. } as User,
  58. };
  59. return { ...defaultConfig, ...configOverrides };
  60. };
  61. describe('isSignedInUserTeamAdmin', () => {
  62. describe('when feature toggle editorsCanAdmin is turned off', () => {
  63. it('should return true', () => {
  64. const config = setup();
  65. const result = isSignedInUserTeamAdmin(config);
  66. expect(result).toBe(true);
  67. });
  68. });
  69. describe('when feature toggle editorsCanAdmin is turned on', () => {
  70. it('should return true if signed in user is grafanaAdmin', () => {
  71. const config = setup({
  72. editorsCanAdmin: true,
  73. signedInUser: {
  74. id: signedInUserId,
  75. isGrafanaAdmin: true,
  76. orgRole: OrgRole.Viewer,
  77. } as User,
  78. });
  79. const result = isSignedInUserTeamAdmin(config);
  80. expect(result).toBe(true);
  81. });
  82. it('should return true if signed in user is org admin', () => {
  83. const config = setup({
  84. editorsCanAdmin: true,
  85. signedInUser: {
  86. id: signedInUserId,
  87. isGrafanaAdmin: false,
  88. orgRole: OrgRole.Admin,
  89. } as User,
  90. });
  91. const result = isSignedInUserTeamAdmin(config);
  92. expect(result).toBe(true);
  93. });
  94. it('should return true if signed in user is team admin', () => {
  95. const config = setup({
  96. members: getMockTeamMembers(5, signedInUserId),
  97. editorsCanAdmin: true,
  98. signedInUser: {
  99. id: signedInUserId,
  100. isGrafanaAdmin: false,
  101. orgRole: OrgRole.Viewer,
  102. } as User,
  103. });
  104. const result = isSignedInUserTeamAdmin(config);
  105. expect(result).toBe(true);
  106. });
  107. it('should return false if signed in user is not grafanaAdmin, org admin or team admin', () => {
  108. const config = setup({
  109. editorsCanAdmin: true,
  110. signedInUser: {
  111. id: signedInUserId,
  112. isGrafanaAdmin: false,
  113. orgRole: OrgRole.Viewer,
  114. } as User,
  115. });
  116. const result = isSignedInUserTeamAdmin(config);
  117. expect(result).toBe(false);
  118. });
  119. });
  120. });