diff --git a/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx b/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx index cd0af3cd77..4f86611eff 100644 --- a/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx +++ b/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx @@ -1,4 +1,4 @@ -import { useCallback } from 'react' +import { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import { Alert, Button, Modal } from 'react-bootstrap' import Icon from '../../../shared/components/icon' @@ -18,19 +18,24 @@ export default function DictionaryModalContent({ handleHide, }: DictionaryModalContentProps) { const { t } = useTranslation() + const [learnedWords, setLearnedWords] = useState(ignoredWords.learnedWords) const { isError, runAsync } = useAsync() const handleRemove = useCallback( word => { - ignoredWords.remove(word) runAsync( postJSON('/spelling/unlearn', { body: { word, }, }) - ).catch(debugConsole.error) + ) + .then(() => { + ignoredWords.remove(word) + setLearnedWords(new Set(ignoredWords.learnedWords)) + }) + .catch(debugConsole.error) }, [runAsync] ) @@ -46,31 +51,29 @@ export default function DictionaryModalContent({ {t('generic_something_went_wrong')} ) : null} - {ignoredWords.learnedWords?.size > 0 ? ( + {learnedWords?.size > 0 ? ( ) : (

diff --git a/services/web/test/frontend/features/dictionary/components/dictionary-modal-content.test.jsx b/services/web/test/frontend/features/dictionary/components/dictionary-modal-content.test.jsx index 072357dd83..6f1c637bba 100644 --- a/services/web/test/frontend/features/dictionary/components/dictionary-modal-content.test.jsx +++ b/services/web/test/frontend/features/dictionary/components/dictionary-modal-content.test.jsx @@ -1,5 +1,8 @@ -import { screen, fireEvent } from '@testing-library/react' -import { expect } from 'chai' +import { + screen, + fireEvent, + waitForElementToBeRemoved, +} from '@testing-library/react' import fetchMock from 'fetch-mock' import DictionaryModal from '../../../../../frontend/js/features/dictionary/components/dictionary-modal' import { renderWithEditorContext } from '../../../helpers/render-with-context' @@ -16,6 +19,7 @@ describe('', function () { afterEach(function () { window.metaAttributesCache = new Map() fetchMock.reset() + window.dispatchEvent(new CustomEvent('learnedWords:doreset')) }) it('list words', async function () { @@ -35,12 +39,13 @@ describe('', function () { fetchMock.post('/spelling/unlearn', 200) setLearnedWords(['Foo', 'bar']) renderWithEditorContext( {}} />) + 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 waitForElementToBeRemoved(() => screen.getByText('bar')) screen.getByText('Foo') }) @@ -48,12 +53,13 @@ describe('', function () { fetchMock.post('/spelling/unlearn', 500) setLearnedWords(['foo']) renderWithEditorContext( {}} />) + 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') }) })