-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alun Turner
committed
Jun 22, 2023
1 parent
977f03b
commit 870e207
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { RefObject } from 'react'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
|
||
import * as mockRustModel from '../generated/wysiwyg'; | ||
import { useComposerModel } from './useComposerModel'; | ||
|
||
describe('useComposerModel', () => { | ||
const mockComposer = document.createElement('div'); | ||
const mockNullRef = { current: null } as RefObject<null>; | ||
const mockComposerRef = { current: mockComposer } as RefObject<HTMLElement>; | ||
|
||
beforeEach(() => { | ||
vi.spyOn(mockRustModel, 'new_composer_model'); | ||
vi.spyOn(mockRustModel, 'new_composer_model_from_html'); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('Does not create a composerModel without a ref', () => { | ||
const { result } = renderHook(() => useComposerModel(mockNullRef)); | ||
|
||
expect(result.current.composerModel).toBeNull(); | ||
}); | ||
|
||
it('Only calls `new_composer_model` if ref exists but no initial content exists', async () => { | ||
const { result } = renderHook(() => useComposerModel(mockComposerRef)); | ||
|
||
// wait for the composerModel to be created | ||
await waitFor(() => { | ||
expect(result.current.composerModel).not.toBeNull(); | ||
}); | ||
|
||
// check only new_composer_model has been called | ||
expect(mockRustModel.new_composer_model).toHaveBeenCalledTimes(1); | ||
expect( | ||
mockRustModel.new_composer_model_from_html, | ||
).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Calls `new_composer_model_from_html` if ref and initial content exists', async () => { | ||
const { result } = renderHook(() => | ||
useComposerModel(mockComposerRef, 'some content'), | ||
); | ||
|
||
// wait for the composerModel to be created | ||
await waitFor(() => { | ||
expect(result.current.composerModel).not.toBeNull(); | ||
}); | ||
|
||
// check only new_composer_model_from_html has been called | ||
expect( | ||
mockRustModel.new_composer_model_from_html, | ||
).toHaveBeenCalledTimes(1); | ||
expect(mockRustModel.new_composer_model).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Sets the ref inner html when initial content is valid html', async () => { | ||
const inputContent = `<a href="this is allowed" other="disallowedattribute">test link</a>`; | ||
|
||
// the rust model will strip "bad" attributes and the hook always adds a trailing <br> | ||
const expectedComposerInnerHtml = `<a href="this is allowed">test link</a><br>`; | ||
const { result } = renderHook(() => | ||
useComposerModel(mockComposerRef, inputContent), | ||
); | ||
|
||
// wait for the composerModel to be created | ||
await waitFor(() => { | ||
expect(result.current.composerModel).not.toBeNull(); | ||
}); | ||
|
||
// check that the content of the div is the rust model output | ||
expect(mockComposer.innerHTML).toBe(expectedComposerInnerHtml); | ||
}); | ||
|
||
it('Falls back to calling `new_composer_model` if there is a parsing error', async () => { | ||
// Use badly formed initial content to cause a html parsing error | ||
const { result } = renderHook(() => | ||
useComposerModel(mockComposerRef, '<badly>formed content</>'), | ||
); | ||
|
||
// wait for the composerModel to be created | ||
await waitFor(() => { | ||
expect(result.current.composerModel).not.toBeNull(); | ||
}); | ||
|
||
// check that both functions have been called | ||
expect( | ||
mockRustModel.new_composer_model_from_html, | ||
).toHaveBeenCalledTimes(1); | ||
expect(mockRustModel.new_composer_model).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |