managed-group-subscriptions.test.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. name: 'ol-splitTestVariants',
  94. value: { 'group-audit-logs': 'active' },
  95. },
  96. ],
  97. })
  98. const elements = screen.getAllByText('You are a', {
  99. exact: false,
  100. })
  101. expect(elements.length).to.equal(4)
  102. expect(elements[0].textContent).to.equal(
  103. 'You are a manager and member of the Professional group subscription GAS administered by you@example.com.'
  104. )
  105. expect(elements[1].textContent).to.equal(
  106. 'You are a manager of the Collaborator group subscription GASWPLC administered by someone@example.com.'
  107. )
  108. expect(elements[2].textContent).to.equal(
  109. 'You are a manager and member of the Collaborator group subscription Testing administered by you (admin@example.com).'
  110. )
  111. expect(elements[3].textContent).to.equal(
  112. 'You are a manager of the Collaborator group subscription Testing Another administered by you (admin@example.com).'
  113. )
  114. const links = screen.getAllByRole('link')
  115. expect(links[1].getAttribute('href')).to.equal(
  116. '/manage/groups/bcd567/members'
  117. )
  118. expect(links[2].getAttribute('href')).to.equal(
  119. '/manage/groups/bcd567/managers'
  120. )
  121. expect(links[3].getAttribute('href')).to.equal(
  122. '/manage/groups/bcd567/audit-logs'
  123. )
  124. expect(links[4].getAttribute('href')).to.equal('/metrics/groups/bcd567')
  125. expect(links[6].getAttribute('href')).to.equal(
  126. '/manage/groups/def456/members'
  127. )
  128. expect(links[7].getAttribute('href')).to.equal(
  129. '/manage/groups/def456/managers'
  130. )
  131. expect(links[9].getAttribute('href')).to.equal('/metrics/groups/def456')
  132. expect(links[11].getAttribute('href')).to.equal(
  133. '/manage/groups/group2abc/members'
  134. )
  135. expect(links[12].getAttribute('href')).to.equal(
  136. '/manage/groups/group2abc/managers'
  137. )
  138. expect(links[14].getAttribute('href')).to.equal('/metrics/groups/group2abc')
  139. expect(links[16].getAttribute('href')).to.equal(
  140. '/manage/groups/group123abc/members'
  141. )
  142. expect(links[17].getAttribute('href')).to.equal(
  143. '/manage/groups/group123abc/managers'
  144. )
  145. expect(links[19].getAttribute('href')).to.equal(
  146. '/metrics/groups/group123abc'
  147. )
  148. })
  149. it('renders nothing when there are no group memberships', function () {
  150. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />)
  151. const elements = screen.queryAllByText('You are a', {
  152. exact: false,
  153. })
  154. expect(elements.length).to.equal(0)
  155. })
  156. it('does not render the Manage group settings row when the user is not the group admin', function () {
  157. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  158. metaTags: [
  159. {
  160. name: 'ol-managedGroupSubscriptions',
  161. value: managedGroupSubscriptions2,
  162. },
  163. {
  164. name: 'ol-groupSettingsEnabledFor',
  165. value: [],
  166. },
  167. ],
  168. })
  169. expect(screen.queryByText('Manage group settings')).to.be.null
  170. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  171. .be.null
  172. })
  173. it('renders Managed Group / Group SSO settings row when both features are turned on', async function () {
  174. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  175. metaTags: [
  176. {
  177. name: 'ol-managedGroupSubscriptions',
  178. value: managedGroupSubscriptions2,
  179. },
  180. {
  181. name: 'ol-groupSettingsEnabledFor',
  182. value: [managedGroupSubscriptions2[0]._id],
  183. },
  184. ],
  185. })
  186. await screen.findAllByText('Group settings')
  187. await screen.findAllByText('Configure and manage SSO and Managed Users')
  188. })
  189. it('does not render Group SSO settings when the feature is turned off', async function () {
  190. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  191. metaTags: [
  192. {
  193. name: 'ol-managedGroupSubscriptions',
  194. value: managedGroupSubscriptions4,
  195. },
  196. {
  197. name: 'ol-groupSettingsEnabledFor',
  198. value: [managedGroupSubscriptions4[0]._id],
  199. },
  200. ],
  201. })
  202. await screen.findAllByText('Group settings')
  203. await screen.findAllByText('Turn on Managed Users')
  204. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  205. .not.exist
  206. expect(screen.queryByText('Configure and manage SSO')).to.not.exist
  207. })
  208. it('does not render MAnaged Group settings when the feature is turned off', async function () {
  209. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  210. metaTags: [
  211. {
  212. name: 'ol-managedGroupSubscriptions',
  213. value: managedGroupSubscriptions3,
  214. },
  215. {
  216. name: 'ol-groupSettingsEnabledFor',
  217. value: [managedGroupSubscriptions3[0]._id],
  218. },
  219. ],
  220. })
  221. await screen.findAllByText('Group settings')
  222. await screen.findAllByText('Configure and manage SSO')
  223. expect(screen.queryByText('Turn on Managed Users')).to.not.exist
  224. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  225. .not.exist
  226. })
  227. })