managed-group-subscriptions.test.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { expect } from 'chai'
  2. import { screen } from '@testing-library/react'
  3. import ManagedGroupSubscriptions from '../../../../../../frontend/js/features/subscription/components/dashboard/managed-group-subscriptions'
  4. import { ManagedGroupSubscription } from '../../../../../../types/subscription/dashboard/subscription'
  5. import {
  6. cleanUpContext,
  7. renderWithSubscriptionDashContext,
  8. } from '../../helpers/render-with-subscription-dash-context'
  9. function getManagedGroupSubscriptions(
  10. groupSSO: boolean | null,
  11. managedUsers: boolean | null
  12. ): ManagedGroupSubscription[] {
  13. const subscriptionOne = {
  14. _id: 'bcd567',
  15. userIsGroupMember: true,
  16. planLevelName: 'Professional',
  17. admin_id: {
  18. email: 'you@example.com',
  19. },
  20. features: {
  21. groupSSO,
  22. managedUsers,
  23. },
  24. teamName: 'GAS',
  25. }
  26. const subscriptionTwo = {
  27. _id: 'def456',
  28. userIsGroupMember: false,
  29. planLevelName: 'Collaborator',
  30. admin_id: {
  31. email: 'someone@example.com',
  32. },
  33. features: {
  34. groupSSO,
  35. managedUsers,
  36. },
  37. teamName: 'GASWPLC',
  38. }
  39. const subscriptionMemberAndAdmin = {
  40. _id: 'group2abc',
  41. userIsGroupMember: true,
  42. planLevelName: 'Collaborator',
  43. admin_id: {
  44. email: 'admin@example.com',
  45. },
  46. features: {
  47. groupSSO,
  48. managedUsers,
  49. },
  50. teamName: 'Testing',
  51. }
  52. const subscriptionAdmin = {
  53. _id: 'group123abc',
  54. userIsGroupMember: false,
  55. planLevelName: 'Collaborator',
  56. admin_id: {
  57. email: 'admin@example.com',
  58. },
  59. features: {
  60. groupSSO,
  61. managedUsers,
  62. },
  63. teamName: 'Testing Another',
  64. }
  65. return [
  66. subscriptionOne,
  67. subscriptionTwo,
  68. subscriptionMemberAndAdmin,
  69. subscriptionAdmin,
  70. ]
  71. }
  72. const managedGroupSubscriptions: ManagedGroupSubscription[] =
  73. getManagedGroupSubscriptions(false, false)
  74. const managedGroupSubscriptions2: ManagedGroupSubscription[] =
  75. getManagedGroupSubscriptions(true, true)
  76. const managedGroupSubscriptions3: ManagedGroupSubscription[] =
  77. getManagedGroupSubscriptions(true, false)
  78. const managedGroupSubscriptions4: ManagedGroupSubscription[] =
  79. getManagedGroupSubscriptions(false, true)
  80. describe('<ManagedGroupSubscriptions />', function () {
  81. afterEach(function () {
  82. cleanUpContext()
  83. })
  84. it('renders all managed group subscriptions', async function () {
  85. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  86. metaTags: [
  87. {
  88. name: 'ol-managedGroupSubscriptions',
  89. value: managedGroupSubscriptions,
  90. },
  91. { name: 'ol-usersEmail', value: 'admin@example.com' },
  92. ],
  93. })
  94. const elements = screen.getAllByText('You are a', {
  95. exact: false,
  96. })
  97. expect(elements.length).to.equal(4)
  98. expect(elements[0].textContent).to.equal(
  99. 'You are a manager and member of the Professional group subscription GAS administered by you@example.com.'
  100. )
  101. expect(elements[1].textContent).to.equal(
  102. 'You are a manager of the Collaborator group subscription GASWPLC administered by someone@example.com.'
  103. )
  104. expect(elements[2].textContent).to.equal(
  105. 'You are a manager and member of the Collaborator group subscription Testing administered by you (admin@example.com).'
  106. )
  107. expect(elements[3].textContent).to.equal(
  108. 'You are a manager of the Collaborator group subscription Testing Another administered by you (admin@example.com).'
  109. )
  110. const links = screen.getAllByRole('link')
  111. expect(links[1].getAttribute('href')).to.equal(
  112. '/manage/groups/bcd567/members'
  113. )
  114. expect(links[2].getAttribute('href')).to.equal(
  115. '/manage/groups/bcd567/managers'
  116. )
  117. expect(links[3].getAttribute('href')).to.equal(
  118. '/manage/groups/bcd567/audit-logs'
  119. )
  120. expect(links[4].getAttribute('href')).to.equal('/metrics/groups/bcd567')
  121. expect(links[6].getAttribute('href')).to.equal(
  122. '/manage/groups/def456/members'
  123. )
  124. expect(links[7].getAttribute('href')).to.equal(
  125. '/manage/groups/def456/managers'
  126. )
  127. expect(links[9].getAttribute('href')).to.equal('/metrics/groups/def456')
  128. expect(links[11].getAttribute('href')).to.equal(
  129. '/manage/groups/group2abc/members'
  130. )
  131. expect(links[12].getAttribute('href')).to.equal(
  132. '/manage/groups/group2abc/managers'
  133. )
  134. expect(links[14].getAttribute('href')).to.equal('/metrics/groups/group2abc')
  135. expect(links[16].getAttribute('href')).to.equal(
  136. '/manage/groups/group123abc/members'
  137. )
  138. expect(links[17].getAttribute('href')).to.equal(
  139. '/manage/groups/group123abc/managers'
  140. )
  141. expect(links[19].getAttribute('href')).to.equal(
  142. '/metrics/groups/group123abc'
  143. )
  144. })
  145. it('renders nothing when there are no group memberships', function () {
  146. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />)
  147. const elements = screen.queryAllByText('You are a', {
  148. exact: false,
  149. })
  150. expect(elements.length).to.equal(0)
  151. })
  152. it('does not render the Manage group settings row when the user is not the group admin', function () {
  153. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  154. metaTags: [
  155. {
  156. name: 'ol-managedGroupSubscriptions',
  157. value: managedGroupSubscriptions2,
  158. },
  159. {
  160. name: 'ol-groupSettingsEnabledFor',
  161. value: [],
  162. },
  163. ],
  164. })
  165. expect(screen.queryByText('Manage group settings')).to.be.null
  166. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  167. .be.null
  168. })
  169. it('renders Managed Group / Group SSO settings row when both features are turned on', async function () {
  170. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  171. metaTags: [
  172. {
  173. name: 'ol-managedGroupSubscriptions',
  174. value: managedGroupSubscriptions2,
  175. },
  176. {
  177. name: 'ol-groupSettingsEnabledFor',
  178. value: [managedGroupSubscriptions2[0]._id],
  179. },
  180. ],
  181. })
  182. await screen.findAllByText('Group settings')
  183. await screen.findAllByText('Configure and manage SSO and Managed Users')
  184. })
  185. it('does not render Group SSO settings when the feature is turned off', async function () {
  186. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  187. metaTags: [
  188. {
  189. name: 'ol-managedGroupSubscriptions',
  190. value: managedGroupSubscriptions4,
  191. },
  192. {
  193. name: 'ol-groupSettingsEnabledFor',
  194. value: [managedGroupSubscriptions4[0]._id],
  195. },
  196. ],
  197. })
  198. await screen.findAllByText('Group settings')
  199. await screen.findAllByText('Turn on Managed Users')
  200. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  201. .not.exist
  202. expect(screen.queryByText('Configure and manage SSO')).to.not.exist
  203. })
  204. it('does not render MAnaged Group settings when the feature is turned off', async function () {
  205. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  206. metaTags: [
  207. {
  208. name: 'ol-managedGroupSubscriptions',
  209. value: managedGroupSubscriptions3,
  210. },
  211. {
  212. name: 'ol-groupSettingsEnabledFor',
  213. value: [managedGroupSubscriptions3[0]._id],
  214. },
  215. ],
  216. })
  217. await screen.findAllByText('Group settings')
  218. await screen.findAllByText('Configure and manage SSO')
  219. expect(screen.queryByText('Turn on Managed Users')).to.not.exist
  220. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  221. .not.exist
  222. })
  223. })