current-plan-widget.test.tsx 7.2 KB

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