dictionary-modal-content.test.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { screen, fireEvent, waitFor } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import DictionaryModal from '../../../../../frontend/js/features/dictionary/components/dictionary-modal'
  5. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  6. function setLearnedWords(words) {
  7. window.metaAttributesCache.set('ol-learnedWords', words)
  8. window.dispatchEvent(new CustomEvent('learnedWords:doreset'))
  9. }
  10. describe('<DictionaryModalContent />', function () {
  11. beforeEach(function () {
  12. window.metaAttributesCache = window.metaAttributesCache || new Map()
  13. })
  14. afterEach(function () {
  15. window.metaAttributesCache = new Map()
  16. fetchMock.reset()
  17. window.dispatchEvent(new CustomEvent('learnedWords:doreset'))
  18. })
  19. it('list words', async function () {
  20. setLearnedWords(['foo', 'bar'])
  21. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  22. screen.getByText('foo')
  23. screen.getByText('bar')
  24. })
  25. it('shows message when empty', async function () {
  26. setLearnedWords([])
  27. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  28. screen.getByText('Your custom dictionary is empty.')
  29. })
  30. it('removes words', async function () {
  31. fetchMock.post('/spelling/unlearn', 200)
  32. setLearnedWords(['Foo', 'bar'])
  33. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  34. screen.getByText('Foo')
  35. screen.getByText('bar')
  36. const [firstButton] = screen.getAllByRole('button', {
  37. name: 'Remove from dictionary',
  38. })
  39. fireEvent.click(firstButton)
  40. await fetchMock.flush()
  41. await waitFor(() => expect(screen.queryByText('bar')).to.not.exist)
  42. screen.getByText('Foo')
  43. })
  44. it('handles errors', async function () {
  45. fetchMock.post('/spelling/unlearn', 500)
  46. setLearnedWords(['foo'])
  47. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  48. screen.getByText('foo')
  49. const [firstButton] = screen.getAllByRole('button', {
  50. name: 'Remove from dictionary',
  51. })
  52. fireEvent.click(firstButton)
  53. await fetchMock.flush()
  54. screen.getByText('Sorry, something went wrong')
  55. screen.getByText('foo')
  56. })
  57. })