group-subscription-memberships.test.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { expect } from 'chai'
  2. import {
  3. fireEvent,
  4. render,
  5. screen,
  6. waitFor,
  7. within,
  8. } from '@testing-library/react'
  9. import sinon from 'sinon'
  10. import GroupSubscriptionMemberships from '../../../../../../frontend/js/features/subscription/components/dashboard/group-subscription-memberships'
  11. import { SubscriptionDashboardProvider } from '../../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
  12. import fetchMock from 'fetch-mock'
  13. import { MemberGroupSubscription } from '../../../../../../types/subscription/dashboard/subscription'
  14. import {
  15. groupActiveSubscription,
  16. groupActiveSubscriptionWithPendingLicenseChange,
  17. } from '../../fixtures/subscriptions'
  18. import { location } from '@/shared/components/location'
  19. import { UserId } from '../../../../../../types/user'
  20. import { SplitTestProvider } from '@/shared/context/split-test-context'
  21. const userId = 'fff999fff999'
  22. const memberGroupSubscriptions: MemberGroupSubscription[] = [
  23. {
  24. ...groupActiveSubscription,
  25. userIsGroupManager: false,
  26. planLevelName: 'Pro',
  27. admin_id: {
  28. id: 'abc123abc123' as UserId,
  29. email: 'you@example.com',
  30. },
  31. },
  32. {
  33. ...groupActiveSubscriptionWithPendingLicenseChange,
  34. userIsGroupManager: true,
  35. planLevelName: 'Standard',
  36. admin_id: {
  37. id: 'bcd456bcd456' as UserId,
  38. email: 'someone@example.com',
  39. },
  40. },
  41. ] as MemberGroupSubscription[]
  42. describe('<GroupSubscriptionMemberships />', function () {
  43. beforeEach(function () {
  44. window.metaAttributesCache.set(
  45. 'ol-memberGroupSubscriptions',
  46. memberGroupSubscriptions
  47. )
  48. window.metaAttributesCache.set('ol-user_id', userId)
  49. })
  50. afterEach(function () {
  51. fetchMock.removeRoutes().clearHistory()
  52. })
  53. it('renders all group subscriptions not managed', function () {
  54. render(
  55. <SplitTestProvider>
  56. <SubscriptionDashboardProvider>
  57. <GroupSubscriptionMemberships />
  58. </SubscriptionDashboardProvider>
  59. </SplitTestProvider>
  60. )
  61. const elements = screen.getAllByText('You are on our', {
  62. exact: false,
  63. })
  64. expect(elements.length).to.equal(1)
  65. expect(elements[0].textContent).to.equal(
  66. 'You are on our Pro plan as a member of the group subscription GAS administered by you@example.com'
  67. )
  68. })
  69. describe('opens leave group modal when button is clicked', function () {
  70. beforeEach(function () {
  71. this.locationWrapperSandbox = sinon.createSandbox()
  72. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  73. render(
  74. <SplitTestProvider>
  75. <SubscriptionDashboardProvider>
  76. <GroupSubscriptionMemberships />
  77. </SubscriptionDashboardProvider>
  78. </SplitTestProvider>
  79. )
  80. const leaveGroupButton = screen.getByRole('button', {
  81. name: 'Leave group',
  82. })
  83. fireEvent.click(leaveGroupButton)
  84. this.confirmModal = screen.getByRole('dialog')
  85. within(this.confirmModal).getByText(
  86. 'Are you sure you want to leave this group?'
  87. )
  88. this.cancelButton = within(this.confirmModal).getByRole('button', {
  89. name: 'Cancel',
  90. })
  91. this.leaveNowButton = within(this.confirmModal).getByRole('button', {
  92. name: 'Leave now',
  93. })
  94. })
  95. afterEach(function () {
  96. this.locationWrapperSandbox.restore()
  97. })
  98. it('close the modal', function () {
  99. fireEvent.click(this.cancelButton)
  100. expect(screen.queryByRole('dialog')).to.not.exist
  101. })
  102. it('leave the group', async function () {
  103. const leaveGroupApiMock = fetchMock.delete(
  104. `/subscription/group/user?subscriptionId=bcd567`,
  105. {
  106. status: 204,
  107. }
  108. )
  109. fireEvent.click(this.leaveNowButton)
  110. expect(leaveGroupApiMock.callHistory.called()).to.be.true
  111. const reloadStub = this.locationWrapperStub.reload
  112. await waitFor(() => {
  113. expect(reloadStub).to.have.been.called
  114. })
  115. })
  116. })
  117. it('renders nothing when there are no group subscriptions', function () {
  118. window.metaAttributesCache.set('ol-memberGroupSubscriptions', undefined)
  119. render(
  120. <SplitTestProvider>
  121. <SubscriptionDashboardProvider>
  122. <GroupSubscriptionMemberships />
  123. </SubscriptionDashboardProvider>
  124. </SplitTestProvider>
  125. )
  126. const elements = screen.queryAllByText('You are on our', {
  127. exact: false,
  128. })
  129. expect(elements.length).to.equal(0)
  130. })
  131. })