leavers-survey-alert.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, screen, render } from '@testing-library/react'
  4. import { UserEmailsProvider } from '../../../../../frontend/js/features/settings/context/user-email-context'
  5. import { LeaversSurveyAlert } from '../../../../../frontend/js/features/settings/components/leavers-survey-alert'
  6. import * as eventTracking from '@/infrastructure/event-tracking'
  7. import localStorage from '@/infrastructure/local-storage'
  8. import fetchMock from 'fetch-mock'
  9. function renderWithProvider() {
  10. render(<LeaversSurveyAlert />, {
  11. wrapper: ({ children }) => (
  12. <UserEmailsProvider>{children}</UserEmailsProvider>
  13. ),
  14. })
  15. }
  16. describe('<LeaversSurveyAlert/>', function () {
  17. beforeEach(function () {
  18. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  19. })
  20. afterEach(function () {
  21. fetchMock.removeRoutes().clearHistory()
  22. })
  23. it('should render before the expiration date', function () {
  24. const tomorrow = Date.now() + 1000 * 60 * 60 * 24
  25. localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
  26. localStorage.setItem('hideInstitutionalLeaversSurvey', false)
  27. renderWithProvider()
  28. screen.getByRole('alert')
  29. screen.getByText(/Provide some quick feedback/)
  30. screen.getByRole('link', { name: 'Take a short survey' })
  31. })
  32. it('should not render after the expiration date', function () {
  33. const yesterday = Date.now() - 1000 * 60 * 60 * 24
  34. localStorage.setItem('showInstitutionalLeaversSurveyUntil', yesterday)
  35. localStorage.setItem('hideInstitutionalLeaversSurvey', false)
  36. renderWithProvider()
  37. expect(screen.queryByRole('alert')).to.be.null
  38. })
  39. it('should not render if it has been hidden', function () {
  40. const tomorrow = Date.now() + 1000 * 60 * 60 * 24
  41. localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
  42. localStorage.setItem('hideInstitutionalLeaversSurvey', true)
  43. renderWithProvider()
  44. expect(screen.queryByRole('alert')).to.be.null
  45. })
  46. it('should reset the expiration date when it is closed', function () {
  47. const tomorrow = Date.now() + 1000 * 60 * 60 * 24
  48. localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
  49. localStorage.setItem('hideInstitutionalLeaversSurvey', false)
  50. renderWithProvider()
  51. screen.getByRole('alert')
  52. fireEvent.click(screen.getByRole('button'))
  53. expect(screen.queryByRole('alert')).to.be.null
  54. expect(localStorage.getItem('showInstitutionalLeaversSurveyUntil')).to.be
  55. .null
  56. })
  57. describe('event tracking', function () {
  58. let sendMBSpy: sinon.SinonSpy
  59. beforeEach(function () {
  60. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  61. const tomorrow = Date.now() + 1000 * 60 * 60 * 24
  62. localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
  63. localStorage.setItem('hideInstitutionalLeaversSurvey', false)
  64. renderWithProvider()
  65. })
  66. afterEach(function () {
  67. sendMBSpy.restore()
  68. localStorage.clear()
  69. })
  70. it('should sent a `view` event on load', function () {
  71. expect(sendMBSpy).to.be.calledOnce
  72. expect(sendMBSpy).calledWith(
  73. 'institutional-leavers-survey-notification',
  74. { type: 'view', page: '/' }
  75. )
  76. })
  77. it('should sent a `click` event when the link is clicked', function () {
  78. fireEvent.click(screen.getByRole('link'))
  79. expect(sendMBSpy).to.be.calledTwice
  80. expect(sendMBSpy).calledWith(
  81. 'institutional-leavers-survey-notification',
  82. { type: 'click', page: '/' }
  83. )
  84. })
  85. it('should sent a `close` event when it is closed', function () {
  86. fireEvent.click(screen.getByRole('button'))
  87. expect(sendMBSpy).to.be.calledTwice
  88. expect(sendMBSpy).calledWith(
  89. 'institutional-leavers-survey-notification',
  90. { type: 'close', page: '/' }
  91. )
  92. })
  93. })
  94. })