managed-group-subscriptions.test.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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('/metrics/groups/bcd567')
  118. expect(links[5].getAttribute('href')).to.equal(
  119. '/manage/groups/def456/members'
  120. )
  121. expect(links[6].getAttribute('href')).to.equal(
  122. '/manage/groups/def456/managers'
  123. )
  124. expect(links[7].getAttribute('href')).to.equal('/metrics/groups/def456')
  125. expect(links[9].getAttribute('href')).to.equal(
  126. '/manage/groups/group2abc/members'
  127. )
  128. expect(links[10].getAttribute('href')).to.equal(
  129. '/manage/groups/group2abc/managers'
  130. )
  131. expect(links[11].getAttribute('href')).to.equal('/metrics/groups/group2abc')
  132. expect(links[13].getAttribute('href')).to.equal(
  133. '/manage/groups/group123abc/members'
  134. )
  135. expect(links[14].getAttribute('href')).to.equal(
  136. '/manage/groups/group123abc/managers'
  137. )
  138. expect(links[15].getAttribute('href')).to.equal(
  139. '/metrics/groups/group123abc'
  140. )
  141. })
  142. it('renders nothing when there are no group memberships', function () {
  143. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />)
  144. const elements = screen.queryAllByText('You are a', {
  145. exact: false,
  146. })
  147. expect(elements.length).to.equal(0)
  148. })
  149. it('does not render the Manage group settings row when the user is not the group admin', function () {
  150. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  151. metaTags: [
  152. {
  153. name: 'ol-managedGroupSubscriptions',
  154. value: managedGroupSubscriptions2,
  155. },
  156. {
  157. name: 'ol-groupSettingsEnabledFor',
  158. value: [],
  159. },
  160. ],
  161. })
  162. expect(screen.queryByText('Manage group settings')).to.be.null
  163. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  164. .be.null
  165. })
  166. it('renders Managed Group / Group SSO settings row when both features are turned on', async function () {
  167. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  168. metaTags: [
  169. {
  170. name: 'ol-managedGroupSubscriptions',
  171. value: managedGroupSubscriptions2,
  172. },
  173. {
  174. name: 'ol-groupSettingsEnabledFor',
  175. value: [managedGroupSubscriptions2[0]._id],
  176. },
  177. ],
  178. })
  179. await screen.findAllByText('Group settings')
  180. await screen.findAllByText('Configure and manage SSO and Managed Users')
  181. })
  182. it('does not render Group SSO settings when the feature is turned off', async function () {
  183. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  184. metaTags: [
  185. {
  186. name: 'ol-managedGroupSubscriptions',
  187. value: managedGroupSubscriptions4,
  188. },
  189. {
  190. name: 'ol-groupSettingsEnabledFor',
  191. value: [managedGroupSubscriptions4[0]._id],
  192. },
  193. ],
  194. })
  195. await screen.findAllByText('Group settings')
  196. await screen.findAllByText('Turn on Managed Users')
  197. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  198. .not.exist
  199. expect(screen.queryByText('Configure and manage SSO')).to.not.exist
  200. })
  201. it('does not render MAnaged Group settings when the feature is turned off', async function () {
  202. renderWithSubscriptionDashContext(<ManagedGroupSubscriptions />, {
  203. metaTags: [
  204. {
  205. name: 'ol-managedGroupSubscriptions',
  206. value: managedGroupSubscriptions3,
  207. },
  208. {
  209. name: 'ol-groupSettingsEnabledFor',
  210. value: [managedGroupSubscriptions3[0]._id],
  211. },
  212. ],
  213. })
  214. await screen.findAllByText('Group settings')
  215. await screen.findAllByText('Configure and manage SSO')
  216. expect(screen.queryByText('Turn on Managed Users')).to.not.exist
  217. expect(screen.queryByText('Configure and manage SSO and Managed Users')).to
  218. .not.exist
  219. })
  220. })