current-plan-widget.test.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, render, screen } from '@testing-library/react'
  4. import {
  5. CommonsPlanSubscription,
  6. GroupPlanSubscription,
  7. IndividualPlanSubscription,
  8. } from '../../../../../types/project/dashboard/subscription'
  9. import { DeepReadonly } from '../../../../../types/utils'
  10. import * as eventTracking from '../../../../../frontend/js/infrastructure/event-tracking'
  11. import CurrentPlanWidget from '../../../../../frontend/js/features/project-list/components/current-plan-widget/current-plan-widget'
  12. describe('<CurrentPlanWidget />', function () {
  13. const freePlanTooltipMessage =
  14. /click to find out how you could benefit from overleaf premium features/i
  15. const paidPlanTooltipMessage =
  16. /click to find out how to make the most of your overleaf premium features/i
  17. beforeEach(function () {
  18. window.metaAttributesCache = window.metaAttributesCache || new Map()
  19. })
  20. describe('free plan', function () {
  21. let sendMBSpy: sinon.SinonSpy
  22. beforeEach(function () {
  23. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  24. window.metaAttributesCache.set('ol-usersBestSubscription', {
  25. type: 'free',
  26. })
  27. render(<CurrentPlanWidget />)
  28. })
  29. afterEach(function () {
  30. sendMBSpy.restore()
  31. })
  32. it('shows text and tooltip on mouseover', function () {
  33. const link = screen.getByRole('link', {
  34. name: /you’re on the free plan/i,
  35. })
  36. fireEvent.mouseOver(link)
  37. screen.getByRole('tooltip', { name: freePlanTooltipMessage })
  38. })
  39. it('clicks on upgrade button', function () {
  40. const upgradeLink = screen.getByRole('link', { name: /upgrade/i })
  41. fireEvent.click(upgradeLink)
  42. expect(sendMBSpy).to.be.calledOnce
  43. expect(sendMBSpy).calledWith('upgrade-button-click', {
  44. source: 'dashboard-top',
  45. })
  46. })
  47. })
  48. describe('paid plan', function () {
  49. describe('trial', function () {
  50. const subscription = {
  51. type: 'individual',
  52. plan: {
  53. name: 'Abc',
  54. },
  55. subscription: {
  56. name: 'Example Name',
  57. },
  58. remainingTrialDays: -1,
  59. } as DeepReadonly<IndividualPlanSubscription>
  60. beforeEach(function () {
  61. window.metaAttributesCache.set('ol-usersBestSubscription', {
  62. ...subscription,
  63. })
  64. })
  65. it('shows remaining days', function () {
  66. const newSubscription: IndividualPlanSubscription = {
  67. ...subscription,
  68. remainingTrialDays: 5,
  69. }
  70. window.metaAttributesCache.set(
  71. 'ol-usersBestSubscription',
  72. newSubscription
  73. )
  74. render(<CurrentPlanWidget />)
  75. screen.getByRole('link', {
  76. name: new RegExp(
  77. `${newSubscription.remainingTrialDays} more days on your overleaf premium trial`,
  78. 'i'
  79. ),
  80. })
  81. })
  82. it('shows last day message', function () {
  83. window.metaAttributesCache.set('ol-usersBestSubscription', {
  84. ...subscription,
  85. remainingTrialDays: 1,
  86. })
  87. render(<CurrentPlanWidget />)
  88. screen.getByRole('link', {
  89. name: /this is the last day of your overleaf premium trial/i,
  90. })
  91. })
  92. })
  93. describe('individual', function () {
  94. const subscription = {
  95. type: 'individual',
  96. plan: {
  97. name: 'Abc',
  98. },
  99. subscription: {
  100. teamName: 'Example Team',
  101. name: 'Example Name',
  102. },
  103. remainingTrialDays: -1,
  104. } as DeepReadonly<IndividualPlanSubscription>
  105. beforeEach(function () {
  106. window.metaAttributesCache.set('ol-usersBestSubscription', {
  107. ...subscription,
  108. })
  109. })
  110. it('shows text and tooltip on mouseover', function () {
  111. render(<CurrentPlanWidget />)
  112. const link = screen.getByRole('link', {
  113. name: /you’re using overleaf premium/i,
  114. })
  115. fireEvent.mouseOver(link)
  116. screen.getByRole('tooltip', {
  117. name: new RegExp(`on the ${subscription.plan.name}`, 'i'),
  118. })
  119. screen.getByRole('tooltip', { name: paidPlanTooltipMessage })
  120. })
  121. })
  122. describe('group', function () {
  123. const subscription = {
  124. type: 'group',
  125. plan: {
  126. name: 'Abc',
  127. },
  128. subscription: {
  129. name: 'Example Name',
  130. },
  131. remainingTrialDays: -1,
  132. } as DeepReadonly<GroupPlanSubscription>
  133. beforeEach(function () {
  134. window.metaAttributesCache.set('ol-usersBestSubscription', {
  135. ...subscription,
  136. })
  137. })
  138. it('shows text and tooltip on mouseover (without subscription team name)', function () {
  139. render(<CurrentPlanWidget />)
  140. const link = screen.getByRole('link', {
  141. name: /you’re using overleaf premium/i,
  142. })
  143. fireEvent.mouseOver(link)
  144. expect(subscription.subscription.teamName).to.be.undefined
  145. screen.getByRole('tooltip', {
  146. name: new RegExp(
  147. `on the ${subscription.plan.name} plan as a member of a group subscription`,
  148. 'i'
  149. ),
  150. })
  151. screen.getByRole('tooltip', { name: paidPlanTooltipMessage })
  152. })
  153. it('shows text and tooltip on mouseover (with subscription team name)', function () {
  154. const newSubscription = {
  155. ...subscription,
  156. subscription: {
  157. teamName: 'Example Team',
  158. },
  159. }
  160. window.metaAttributesCache.set(
  161. 'ol-usersBestSubscription',
  162. newSubscription
  163. )
  164. render(<CurrentPlanWidget />)
  165. const link = screen.getByRole('link', {
  166. name: /you’re using overleaf premium/i,
  167. })
  168. fireEvent.mouseOver(link)
  169. screen.getByRole('tooltip', {
  170. name: new RegExp(
  171. `on the ${newSubscription.plan.name} plan as a member of a group subscription, ${newSubscription.subscription.teamName}`,
  172. 'i'
  173. ),
  174. })
  175. screen.getByRole('tooltip', { name: paidPlanTooltipMessage })
  176. })
  177. })
  178. describe('commons', function () {
  179. it('shows text and tooltip on mouseover', function () {
  180. const subscription = {
  181. type: 'commons',
  182. plan: {
  183. name: 'Abc',
  184. },
  185. subscription: {
  186. name: 'Example Name',
  187. },
  188. } as DeepReadonly<CommonsPlanSubscription>
  189. window.metaAttributesCache.set('ol-usersBestSubscription', {
  190. ...subscription,
  191. })
  192. render(<CurrentPlanWidget />)
  193. const link = screen.getByRole('link', {
  194. name: /you’re using overleaf premium/i,
  195. })
  196. fireEvent.mouseOver(link)
  197. screen.getByRole('tooltip', {
  198. name: new RegExp(
  199. `on the ${subscription.plan.name} plan because of your affiliation with ${subscription.subscription.name}`,
  200. 'i'
  201. ),
  202. })
  203. screen.getByRole('tooltip', { name: paidPlanTooltipMessage })
  204. })
  205. })
  206. })
  207. })