role.spec.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {UserFixture} from 'sentry-fixture/user';
  3. import {act, render, screen} from 'sentry-test/reactTestingLibrary';
  4. import {Role} from 'sentry/components/acl/role';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import OrganizationStore from 'sentry/stores/organizationStore';
  7. describe('Role', function () {
  8. const organization = OrganizationFixture({
  9. orgRole: 'admin',
  10. orgRoleList: [
  11. {
  12. id: 'member',
  13. name: 'Member',
  14. desc: '...',
  15. minimumTeamRole: 'contributor',
  16. isTeamRolesAllowed: true,
  17. },
  18. {
  19. id: 'admin',
  20. name: 'Admin',
  21. desc: '...',
  22. minimumTeamRole: 'admin',
  23. isTeamRolesAllowed: true,
  24. },
  25. {
  26. id: 'manager',
  27. name: 'Manager',
  28. desc: '...',
  29. minimumTeamRole: 'admin',
  30. isTeamRolesAllowed: true,
  31. },
  32. {
  33. id: 'owner',
  34. name: 'Owner',
  35. desc: '...',
  36. minimumTeamRole: 'admin',
  37. isTeamRolesAllowed: true,
  38. },
  39. ],
  40. });
  41. describe('as render prop', function () {
  42. const childrenMock = jest.fn().mockReturnValue(null);
  43. beforeEach(function () {
  44. OrganizationStore.init();
  45. childrenMock.mockClear();
  46. });
  47. it('has a sufficient role', function () {
  48. render(<Role role="admin">{childrenMock}</Role>, {
  49. organization,
  50. });
  51. expect(childrenMock).toHaveBeenCalledWith({
  52. hasRole: true,
  53. });
  54. });
  55. it('has an insufficient role', function () {
  56. render(<Role role="manager">{childrenMock}</Role>, {
  57. organization,
  58. });
  59. expect(childrenMock).toHaveBeenCalledWith({
  60. hasRole: false,
  61. });
  62. });
  63. it('gives access to a superuser with insufficient role', function () {
  64. organization.access = ['org:superuser'];
  65. OrganizationStore.onUpdate(organization, {replace: true});
  66. render(<Role role="owner">{childrenMock}</Role>, {
  67. organization,
  68. });
  69. expect(childrenMock).toHaveBeenCalledWith({
  70. hasRole: true,
  71. });
  72. });
  73. it('does not give access to a made up role', function () {
  74. render(<Role role="abcdefg">{childrenMock}</Role>, {
  75. organization,
  76. });
  77. expect(childrenMock).toHaveBeenCalledWith({
  78. hasRole: false,
  79. });
  80. });
  81. it('handles no user', function () {
  82. const user = {...ConfigStore.get('user')};
  83. ConfigStore.set('user', undefined as any);
  84. render(<Role role="member">{childrenMock}</Role>, {
  85. organization,
  86. });
  87. expect(childrenMock).toHaveBeenCalledWith({
  88. hasRole: false,
  89. });
  90. act(() => ConfigStore.set('user', user));
  91. });
  92. it('updates if user changes', function () {
  93. ConfigStore.set('user', undefined as any);
  94. const {rerender} = render(<Role role="member">{childrenMock}</Role>, {
  95. organization,
  96. });
  97. expect(childrenMock).toHaveBeenCalledWith({
  98. hasRole: false,
  99. });
  100. act(() => ConfigStore.set('user', UserFixture()));
  101. rerender(<Role role="member">{childrenMock}</Role>);
  102. expect(childrenMock).toHaveBeenCalledWith({
  103. hasRole: true,
  104. });
  105. });
  106. it('handles no organization.orgRoleList', function () {
  107. render(
  108. <Role role="member" organization={{...organization, orgRoleList: []}}>
  109. {childrenMock}
  110. </Role>,
  111. {organization}
  112. );
  113. expect(childrenMock).toHaveBeenCalledWith({
  114. hasRole: false,
  115. });
  116. });
  117. });
  118. describe('as React node', function () {
  119. it('has a sufficient role', function () {
  120. render(
  121. <Role role="member">
  122. <div>The Child</div>
  123. </Role>,
  124. {organization}
  125. );
  126. expect(screen.getByText('The Child')).toBeInTheDocument();
  127. });
  128. it('has an insufficient role', function () {
  129. render(
  130. <Role role="owner">
  131. <div>The Child</div>
  132. </Role>,
  133. {organization}
  134. );
  135. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  136. });
  137. });
  138. });