grammarly-warning.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import sinon from 'sinon'
  2. import fetchMock from 'fetch-mock'
  3. import { expect } from 'chai'
  4. import { fireEvent, screen, waitFor } from '@testing-library/react'
  5. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  6. import GrammarlyWarning from '../../../../../frontend/js/features/source-editor/components/grammarly-warning'
  7. import * as grammarlyModule from '../../../../../frontend/js/shared/utils/grammarly'
  8. import localStorage from '../../../../../frontend/js/infrastructure/local-storage'
  9. describe('<GrammarlyWarning />', function () {
  10. let grammarlyStub
  11. before(function () {
  12. window.metaAttributesCache = new Map()
  13. })
  14. beforeEach(function () {
  15. grammarlyStub = sinon.stub(grammarlyModule, 'default')
  16. })
  17. afterEach(function () {
  18. window.metaAttributesCache = new Map()
  19. grammarlyStub.restore()
  20. fetchMock.reset()
  21. localStorage.clear()
  22. })
  23. it('shows warning when grammarly is available', async function () {
  24. grammarlyStub.returns(true)
  25. window.metaAttributesCache.set('ol-showNewSourceEditorOption', true)
  26. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  27. scope: {
  28. editor: {
  29. newSourceEditor: true,
  30. },
  31. },
  32. })
  33. await screen.findByText(
  34. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  35. )
  36. await screen.findByRole('button', { name: 'Close' })
  37. await screen.findByRole('link', { name: 'Find out how to avoid this' })
  38. })
  39. it('does not show warning when grammarly is not available', async function () {
  40. grammarlyStub.returns(false)
  41. window.metaAttributesCache.set('ol-showNewSourceEditorOption', true)
  42. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  43. scope: {
  44. editor: {
  45. newSourceEditor: true,
  46. },
  47. },
  48. })
  49. await waitFor(() => {
  50. expect(
  51. screen.queryByText(
  52. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  53. )
  54. ).to.not.exist
  55. })
  56. })
  57. it('does not show warning when user has dismissed the warning', async function () {
  58. grammarlyStub.returns(true)
  59. localStorage.setItem('editor.has_dismissed_grammarly_warning', true)
  60. window.metaAttributesCache.set('ol-showNewSourceEditorOption', true)
  61. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  62. scope: {
  63. editor: {
  64. newSourceEditor: true,
  65. },
  66. },
  67. })
  68. await waitFor(() => {
  69. expect(
  70. screen.queryByText(
  71. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  72. )
  73. ).to.not.exist
  74. })
  75. })
  76. it('does not show warning when user does not have CM6', async function () {
  77. grammarlyStub.returns(true)
  78. window.metaAttributesCache.set('ol-showNewSourceEditorOption', false)
  79. renderWithEditorContext(<GrammarlyWarning delay={100} />)
  80. await waitFor(() => {
  81. expect(
  82. screen.queryByText(
  83. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  84. )
  85. ).to.not.exist
  86. })
  87. })
  88. it('does not show warning when user have ace as their preference', async function () {
  89. grammarlyStub.returns(true)
  90. window.metaAttributesCache.set('ol-showNewSourceEditorOption', true)
  91. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  92. scope: {
  93. editor: {
  94. newSourceEditor: false,
  95. },
  96. },
  97. })
  98. await waitFor(() => {
  99. expect(
  100. screen.queryByText(
  101. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  102. )
  103. ).to.not.exist
  104. })
  105. })
  106. it('does not show warning when user have rich text as their preference', async function () {
  107. grammarlyStub.returns(true)
  108. window.metaAttributesCache.set('ol-showNewSourceEditorOption', true)
  109. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  110. scope: {
  111. editor: {
  112. newSourceEditor: true,
  113. showRichText: true,
  114. },
  115. },
  116. })
  117. await waitFor(() => {
  118. expect(
  119. screen.queryByText(
  120. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  121. )
  122. ).to.not.exist
  123. })
  124. })
  125. it('hides warning if close button is pressed', async function () {
  126. grammarlyStub.returns(true)
  127. window.metaAttributesCache.set('ol-showNewSourceEditorOption', true)
  128. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  129. scope: {
  130. editor: {
  131. newSourceEditor: true,
  132. },
  133. },
  134. })
  135. const warningText =
  136. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  137. await screen.findByText(warningText)
  138. const hasDismissedGrammarlyWarning = localStorage.getItem(
  139. 'editor.has_dismissed_grammarly_warning'
  140. )
  141. expect(hasDismissedGrammarlyWarning).to.equal(null)
  142. const closeButton = screen.getByRole('button', { name: 'Close' })
  143. fireEvent.click(closeButton)
  144. expect(screen.queryByText(warningText)).to.not.exist
  145. await waitFor(() => {
  146. const hasDismissedGrammarlyWarning = localStorage.getItem(
  147. 'editor.has_dismissed_grammarly_warning'
  148. )
  149. expect(hasDismissedGrammarlyWarning).to.equal(true)
  150. })
  151. })
  152. })