inr-banner.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import INRBanner from '@/features/project-list/components/notifications/ads/inr-banner'
  2. import customLocalStorage from '@/infrastructure/local-storage'
  3. import { fireEvent, render, screen } from '@testing-library/react'
  4. import { expect } from 'chai'
  5. describe('<INRBanner />', function () {
  6. beforeEach(function () {
  7. customLocalStorage.clear()
  8. })
  9. it('renders correctly', async function () {
  10. render(<INRBanner />)
  11. await screen.findByRole('dialog')
  12. await screen.findByText(
  13. '70% off all Overleaf premium plans for users in India'
  14. )
  15. await screen.findByText(
  16. 'Get document history, track changes, additional collaborators, and more at Purchasing Power Parity prices.'
  17. )
  18. await screen.findByRole('button', { name: 'Maybe later' })
  19. await screen.findByRole('button', { name: 'Get discounted plan' })
  20. })
  21. it('dismisses the modal when the "Maybe later" button is clicked', async function () {
  22. render(<INRBanner />)
  23. await screen.findByRole('dialog')
  24. fireEvent.click(screen.getByRole('button', { name: 'Maybe later' }))
  25. expect(screen.queryByRole('dialog')).to.be.null
  26. const dismissedUntil = customLocalStorage.getItem(
  27. 'has_dismissed_inr_banner_until'
  28. )
  29. expect(dismissedUntil).to.not.be.null
  30. const nowPlus2Days = new Date()
  31. nowPlus2Days.setDate(nowPlus2Days.getDate() + 2)
  32. // check if dismissal date is around 1 days after the dismissal via "Maybe later" button
  33. expect(new Date(dismissedUntil)).to.be.greaterThan(new Date())
  34. expect(new Date(dismissedUntil)).to.be.lessThan(nowPlus2Days)
  35. })
  36. it('dismisses the modal when close button is clicked', async function () {
  37. render(<INRBanner />)
  38. await screen.findByRole('dialog')
  39. fireEvent.click(screen.getByRole('button', { name: /close/i }))
  40. expect(screen.queryByRole('dialog')).to.be.null
  41. const dismissedUntil = customLocalStorage.getItem(
  42. 'has_dismissed_inr_banner_until'
  43. )
  44. expect(dismissedUntil).to.not.be.null
  45. const nowPlus29Days = new Date()
  46. nowPlus29Days.setDate(nowPlus29Days.getDate() + 29)
  47. const nowPlus31Days = new Date()
  48. nowPlus31Days.setDate(nowPlus31Days.getDate() + 31)
  49. // check if dismissal date is around 30 days after the dismissal via close button
  50. expect(new Date(dismissedUntil)).to.be.greaterThan(nowPlus29Days)
  51. expect(new Date(dismissedUntil)).to.be.lessThan(nowPlus31Days)
  52. })
  53. it('hides the modal when user visits while current date is less than local storage date', function () {
  54. const until = new Date()
  55. until.setDate(until.getDate() + 30) // 30 days
  56. customLocalStorage.setItem('has_dismissed_inr_banner_until', until)
  57. render(<INRBanner />)
  58. expect(screen.queryByRole('dialog')).to.be.null
  59. })
  60. })