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

Commit

Permalink
Merge pull request #1027 from matrix-org/langleyd/web_fix_double_init
Browse files Browse the repository at this point in the history
Stop the composer being re-initialised when customSuggestionPatterns change
  • Loading branch information
langleyd authored Jul 31, 2024
2 parents 270b70e + 3667fff commit 7a76224
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
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

0 comments on commit 7a76224

Please sign in to comment.