premium-features-link.test.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, render, screen, within } from '@testing-library/react'
  4. import * as eventTracking from '../../../../../../frontend/js/infrastructure/event-tracking'
  5. import PremiumFeaturesLink from '../../../../../../frontend/js/features/subscription/components/dashboard/premium-features-link'
  6. import * as useLocationModule from '../../../../../../frontend/js/shared/hooks/use-location'
  7. describe('<PremiumFeaturesLink />', function () {
  8. const originalLocation = window.location
  9. let sendMBSpy: sinon.SinonSpy
  10. const variants = [
  11. { name: 'default', link: '/learn/how-to/Overleaf_premium_features' },
  12. { name: 'new', link: '/about/features-overview' },
  13. ]
  14. beforeEach(function () {
  15. window.metaAttributesCache = new Map()
  16. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  17. this.locationStub = sinon.stub(useLocationModule, 'useLocation').returns({
  18. assign: sinon.stub(),
  19. reload: sinon.stub(),
  20. })
  21. })
  22. afterEach(function () {
  23. window.metaAttributesCache = new Map()
  24. sendMBSpy.restore()
  25. this.locationStub.restore()
  26. })
  27. for (const variant of variants) {
  28. describe(`${variant.name} variant`, function () {
  29. beforeEach(function () {
  30. window.metaAttributesCache.set('ol-splitTestVariants', {
  31. 'features-page': variant.name,
  32. })
  33. })
  34. afterEach(function () {
  35. window.metaAttributesCache.delete('ol-splitTestVariants')
  36. })
  37. it('renders the premium features link and sends analytics event', function () {
  38. render(<PremiumFeaturesLink />)
  39. const premiumText = screen.getByText('Get the most out of your', {
  40. exact: false,
  41. })
  42. const link = within(premiumText).getByRole('link')
  43. fireEvent.click(link)
  44. expect(sendMBSpy).to.be.calledOnce
  45. expect(sendMBSpy).calledWith('features-page-link', {
  46. splitTest: 'features-page',
  47. splitTestVariant: variant.name,
  48. page: originalLocation.pathname,
  49. })
  50. })
  51. })
  52. }
  53. })