survey-widget.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { expect } from 'chai'
  2. import { fireEvent, render, screen } from '@testing-library/react'
  3. import { SurveyWidgetDsNav } from '@/features/project-list/components/survey-widget-ds-nav'
  4. import { SplitTestProvider } from '@/shared/context/split-test-context'
  5. describe('<SurveyWidgetDsNav />', function () {
  6. beforeEach(function () {
  7. this.name = 'my-survey'
  8. this.title = 'To help shape the future of Overleaf'
  9. this.text = 'Click here!'
  10. this.cta = 'Let’s go!'
  11. this.url = 'https://example.com/my-survey'
  12. localStorage.clear()
  13. })
  14. describe('survey widget is visible', function () {
  15. beforeEach(function () {
  16. window.metaAttributesCache.set('ol-survey', {
  17. name: this.name,
  18. title: this.title,
  19. text: this.text,
  20. url: this.url,
  21. })
  22. render(
  23. <SplitTestProvider>
  24. <SurveyWidgetDsNav />
  25. </SplitTestProvider>
  26. )
  27. })
  28. it('shows text and link', function () {
  29. const dismissed = localStorage.getItem('dismissed-my-survey')
  30. expect(dismissed).to.equal(null)
  31. screen.getByText(this.title)
  32. screen.getByText(this.text)
  33. const link = screen.getByRole('link', {
  34. name: 'Take survey',
  35. }) as HTMLAnchorElement
  36. expect(link.href).to.equal(this.url)
  37. })
  38. it('it is dismissed on click on the dismiss button', function () {
  39. const dismissButton = screen.getByRole('button', {
  40. name: 'Close',
  41. })
  42. fireEvent.click(dismissButton)
  43. const text = screen.queryByText(this.title)
  44. expect(text).to.be.null
  45. const link = screen.queryByRole('button')
  46. expect(link).to.be.null
  47. const dismissed = localStorage.getItem('dismissed-my-survey')
  48. expect(dismissed).to.equal('true')
  49. })
  50. })
  51. describe('survey widget is visible with custom CTA', function () {
  52. beforeEach(function () {
  53. window.metaAttributesCache.set('ol-survey', {
  54. name: this.name,
  55. title: this.title,
  56. text: this.text,
  57. cta: this.cta,
  58. url: this.url,
  59. })
  60. render(
  61. <SplitTestProvider>
  62. <SurveyWidgetDsNav />
  63. </SplitTestProvider>
  64. )
  65. })
  66. it('shows text and link with custom CTA', function () {
  67. const dismissed = localStorage.getItem('dismissed-my-survey')
  68. expect(dismissed).to.equal(null)
  69. screen.getByText(this.title)
  70. screen.getByText(this.text)
  71. const link = screen.getByRole('link', {
  72. name: this.cta,
  73. }) as HTMLAnchorElement
  74. expect(link.href).to.equal(this.url)
  75. })
  76. })
  77. describe('survey widget is not shown when already dismissed', function () {
  78. beforeEach(function () {
  79. window.metaAttributesCache.set('ol-survey', {
  80. name: this.name,
  81. title: this.title,
  82. text: this.text,
  83. url: this.url,
  84. })
  85. localStorage.setItem('dismissed-my-survey', 'true')
  86. render(
  87. <SplitTestProvider>
  88. <SurveyWidgetDsNav />
  89. </SplitTestProvider>
  90. )
  91. })
  92. it('nothing is displayed', function () {
  93. const text = screen.queryByText(this.title)
  94. expect(text).to.be.null
  95. const link = screen.queryByRole('button')
  96. expect(link).to.be.null
  97. })
  98. })
  99. describe('survey widget is not shown when no survey is configured', function () {
  100. beforeEach(function () {
  101. render(
  102. <SplitTestProvider>
  103. <SurveyWidgetDsNav />
  104. </SplitTestProvider>
  105. )
  106. })
  107. it('nothing is displayed', function () {
  108. const text = screen.queryByText(this.title)
  109. expect(text).to.be.null
  110. const link = screen.queryByRole('button')
  111. expect(link).to.be.null
  112. })
  113. })
  114. })