Преглед изворни кода

Merge pull request #15982 from overleaf/jel-dictionary-unlearn

[web] Only remove dictionary word from UI if unlearn request is successful

GitOrigin-RevId: 03fdeb39d43892690055efcdfafb22cf89550383
Jessica Lawshe пре 2 година
родитељ
комит
ffd328e906

+ 5 - 2
services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx

@@ -23,14 +23,17 @@ export default function DictionaryModalContent({
 
   const handleRemove = useCallback(
     word => {
-      ignoredWords.remove(word)
       runAsync(
         postJSON('/spelling/unlearn', {
           body: {
             word,
           },
         })
-      ).catch(debugConsole.error)
+      )
+        .then(() => {
+          ignoredWords.remove(word)
+        })
+        .catch(debugConsole.error)
     },
     [runAsync]
   )

+ 8 - 3
services/web/test/frontend/features/dictionary/components/dictionary-modal-content.test.jsx

@@ -1,4 +1,4 @@
-import { screen, fireEvent } from '@testing-library/react'
+import { screen, fireEvent, waitFor } from '@testing-library/react'
 import { expect } from 'chai'
 import fetchMock from 'fetch-mock'
 import DictionaryModal from '../../../../../frontend/js/features/dictionary/components/dictionary-modal'
@@ -8,6 +8,7 @@ function setLearnedWords(words) {
   window.metaAttributesCache.set('ol-learnedWords', words)
   window.dispatchEvent(new CustomEvent('learnedWords:doreset'))
 }
+
 describe('<DictionaryModalContent />', function () {
   beforeEach(function () {
     window.metaAttributesCache = window.metaAttributesCache || new Map()
@@ -16,6 +17,7 @@ describe('<DictionaryModalContent />', function () {
   afterEach(function () {
     window.metaAttributesCache = new Map()
     fetchMock.reset()
+    window.dispatchEvent(new CustomEvent('learnedWords:doreset'))
   })
 
   it('list words', async function () {
@@ -35,12 +37,14 @@ describe('<DictionaryModalContent />', function () {
     fetchMock.post('/spelling/unlearn', 200)
     setLearnedWords(['Foo', 'bar'])
     renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
+    screen.getByText('Foo')
     screen.getByText('bar')
     const [firstButton] = screen.getAllByRole('button', {
       name: 'Remove from dictionary',
     })
     fireEvent.click(firstButton)
-    expect(screen.queryByText('bar')).to.not.exist
+    await fetchMock.flush()
+    await waitFor(() => expect(screen.queryByText('bar')).to.not.exist)
     screen.getByText('Foo')
   })
 
@@ -48,12 +52,13 @@ describe('<DictionaryModalContent />', function () {
     fetchMock.post('/spelling/unlearn', 500)
     setLearnedWords(['foo'])
     renderWithEditorContext(<DictionaryModal show handleHide={() => {}} />)
+    screen.getByText('foo')
     const [firstButton] = screen.getAllByRole('button', {
       name: 'Remove from dictionary',
     })
     fireEvent.click(firstButton)
     await fetchMock.flush()
     screen.getByText('Sorry, something went wrong')
-    screen.getByText('Your custom dictionary is empty.')
+    screen.getByText('foo')
   })
 })