Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Stop the composer being re-initialised when customSuggestionPatterns change #1027

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion platforms/web/lib/useComposerModel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { RefObject } from 'react';
import { act, RefObject } from 'react';
import { renderHook, waitFor } from '@testing-library/react';

import * as mockRustModel from '../generated/wysiwyg';
Expand Down Expand Up @@ -116,4 +116,45 @@ describe('useComposerModel', () => {
).toHaveBeenCalledTimes(1);
expect(mockRustModel.new_composer_model).toHaveBeenCalledTimes(1);
});

it("Doesn't double intialize the model if customSuggestionPatterns are set", async () => {
const useProps: {
editorRef: RefObject<HTMLElement | null>;
initialContent?: string;
customSuggestionPatterns?: Array<string>;
} = {
editorRef: mockComposerRef,
initialContent: '',
customSuggestionPatterns: undefined,
};

const { result, rerender } = renderHook(
(props: {
editorRef: RefObject<HTMLElement | null>;
initialContent?: string;
customSuggestionPatterns?: Array<string>;
}) =>
useComposerModel(
props.editorRef,
props.initialContent,
props.customSuggestionPatterns,
),
{ initialProps: useProps },
);

// wait for the composerModel to be created
await waitFor(() => {
expect(result.current.composerModel).not.toBeNull();
});

await act(() => {
useProps.customSuggestionPatterns = ['test'];
rerender(useProps);
});

expect(mockRustModel.new_composer_model).toHaveBeenCalledTimes(1);
expect(
mockRustModel.new_composer_model_from_html,
).not.toHaveBeenCalled();
});
});
15 changes: 9 additions & 6 deletions platforms/web/lib/useComposerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@ export function useComposerModel(
} else {
contentModel = new_composer_model();
}
if (customSuggestionPatterns) {
contentModel.set_custom_suggestion_patterns(
customSuggestionPatterns,
);
}
setComposerModel(contentModel);
},
[setComposerModel, editorRef, customSuggestionPatterns],
[setComposerModel, editorRef],
);

useEffect(() => {
if (composerModel && customSuggestionPatterns) {
composerModel.set_custom_suggestion_patterns(
customSuggestionPatterns,
);
}
}, [composerModel, customSuggestionPatterns]);

useEffect(() => {
if (editorRef.current) {
initModel(initialContent);
Expand Down
Loading