grammarly-warning.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  26. scope: {
  27. editor: {
  28. newSourceEditor: true,
  29. },
  30. },
  31. })
  32. await screen.findByText(
  33. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  34. )
  35. await screen.findByRole('button', { name: 'Close' })
  36. await screen.findByRole('link', { name: 'Find out how to avoid this' })
  37. })
  38. it('does not show warning when grammarly is not available', async function () {
  39. grammarlyStub.returns(false)
  40. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  41. scope: {
  42. editor: {
  43. newSourceEditor: true,
  44. },
  45. },
  46. })
  47. await waitFor(() => {
  48. expect(
  49. screen.queryByText(
  50. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  51. )
  52. ).to.not.exist
  53. })
  54. })
  55. it('does not show warning when user has dismissed the warning', async function () {
  56. grammarlyStub.returns(true)
  57. localStorage.setItem('editor.has_dismissed_grammarly_warning', true)
  58. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  59. scope: {
  60. editor: {
  61. newSourceEditor: true,
  62. },
  63. },
  64. })
  65. await waitFor(() => {
  66. expect(
  67. screen.queryByText(
  68. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  69. )
  70. ).to.not.exist
  71. })
  72. })
  73. it('does not show warning when user have ace as their preference', async function () {
  74. grammarlyStub.returns(true)
  75. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  76. scope: {
  77. editor: {
  78. newSourceEditor: false,
  79. },
  80. },
  81. })
  82. await waitFor(() => {
  83. expect(
  84. screen.queryByText(
  85. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  86. )
  87. ).to.not.exist
  88. })
  89. })
  90. it('hides warning if close button is pressed', async function () {
  91. grammarlyStub.returns(true)
  92. renderWithEditorContext(<GrammarlyWarning delay={100} />, {
  93. scope: {
  94. editor: {
  95. newSourceEditor: true,
  96. },
  97. },
  98. })
  99. const warningText =
  100. 'A browser extension, for example Grammarly, may be slowing down Overleaf.'
  101. await screen.findByText(warningText)
  102. const hasDismissedGrammarlyWarning = localStorage.getItem(
  103. 'editor.has_dismissed_grammarly_warning'
  104. )
  105. expect(hasDismissedGrammarlyWarning).to.equal(null)
  106. const closeButton = screen.getByRole('button', { name: 'Close' })
  107. fireEvent.click(closeButton)
  108. expect(screen.queryByText(warningText)).to.not.exist
  109. await waitFor(() => {
  110. const hasDismissedGrammarlyWarning = localStorage.getItem(
  111. 'editor.has_dismissed_grammarly_warning'
  112. )
  113. expect(hasDismissedGrammarlyWarning).to.equal(true)
  114. })
  115. })
  116. })