dictionary-modal-content.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { screen, fireEvent } 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. })
  18. it('list words', async function () {
  19. setLearnedWords(['foo', 'bar'])
  20. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  21. screen.getByText('foo')
  22. screen.getByText('bar')
  23. })
  24. it('shows message when empty', async function () {
  25. setLearnedWords([])
  26. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  27. screen.getByText('Your custom dictionary is empty.')
  28. })
  29. it('removes words', async function () {
  30. fetchMock.post('/spelling/unlearn', 200)
  31. setLearnedWords(['Foo', 'bar'])
  32. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  33. screen.getByText('bar')
  34. const [firstButton] = screen.getAllByRole('button', {
  35. name: 'Remove from dictionary',
  36. })
  37. fireEvent.click(firstButton)
  38. expect(screen.queryByText('bar')).to.not.exist
  39. screen.getByText('Foo')
  40. })
  41. it('handles errors', async function () {
  42. fetchMock.post('/spelling/unlearn', 500)
  43. setLearnedWords(['foo'])
  44. renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
  45. const [firstButton] = screen.getAllByRole('button', {
  46. name: 'Remove from dictionary',
  47. })
  48. fireEvent.click(firstButton)
  49. await fetchMock.flush()
  50. screen.getByText('Sorry, something went wrong')
  51. screen.getByText('Your custom dictionary is empty.')
  52. })
  53. })