dictionary-modal-content.test.jsx 2.1 KB

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