-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from ckeditor/ck/552
Fix: Call `onChangeInitializedEditors` on startup of `CKEditorContext` if there are ready editors.
- Loading branch information
Showing
11 changed files
with
211 additions
and
42 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
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
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
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
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
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 |
---|---|---|
|
@@ -63,6 +63,8 @@ | |
"react-dom": "^18.0.0", | ||
"react16": "npm:react@^16.0.0", | ||
"react16-dom": "npm:react-dom@^16.0.0", | ||
"react17": "npm:react@^17.0.0", | ||
"react17-dom": "npm:react-dom@^17.0.0", | ||
"react18": "npm:react@^18.0.0", | ||
"react18-dom": "npm:react-dom@^18.0.0", | ||
"react19": "npm:[email protected]", | ||
|
@@ -83,8 +85,9 @@ | |
"node": ">=18.0.0" | ||
}, | ||
"scripts": { | ||
"dev": "echo \"Use 'dev:16', 'dev:18', or 'dev:19' depending on the version of React you want to test\"", | ||
"dev": "echo \"Use 'dev:16', 'dev:17', 'dev:18', or 'dev:19' depending on the version of React you want to test\"", | ||
"dev:16": "REACT_VERSION=16 vite", | ||
"dev:17": "REACT_VERSION=17 vite", | ||
"dev:18": "REACT_VERSION=18 vite", | ||
"dev:19": "REACT_VERSION=19 vite", | ||
"build": "vite build && tsc --emitDeclarationOnly", | ||
|
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
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
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,157 @@ | ||
/** | ||
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import { describe, it, expect, vi, afterEach } from 'vitest'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { Collection } from 'ckeditor5'; | ||
|
||
import { useInitializedCKEditorsMap } from '../../src/context/useInitializedCKEditorsMap.js'; | ||
|
||
import type { ContextWatchdogValue } from '../../src/context/ckeditorcontext.js'; | ||
import type { CKEditorConfigContextMetadata } from '../../src/context/setCKEditorReactContextMetadata.js'; | ||
|
||
import MockEditor from '../_utils/editor.js'; | ||
|
||
describe( 'useInitializedCKEditorsMap', () => { | ||
afterEach( () => { | ||
vi.clearAllMocks(); | ||
} ); | ||
|
||
it( 'should not call onChangeInitializedEditors when context is not initialized', () => { | ||
const onChangeInitializedEditors = vi.fn(); | ||
const mockWatchdog = { | ||
status: 'initializing' as const, | ||
watchdog: null | ||
}; | ||
|
||
renderHook( () => useInitializedCKEditorsMap( { | ||
currentContextWatchdog: mockWatchdog, | ||
onChangeInitializedEditors | ||
} ) ); | ||
|
||
expect( onChangeInitializedEditors ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should not track editors that are not ready', () => { | ||
const editors = new Collection(); | ||
const onChangeInitializedEditors = vi.fn(); | ||
const notReadyEditor = createMockEditor( 'initializing', { name: '1' } ); | ||
editors.add( notReadyEditor ); | ||
|
||
renderHook( () => useInitializedCKEditorsMap( { | ||
currentContextWatchdog: createMockContextWatchdog( editors ), | ||
onChangeInitializedEditors | ||
} ) ); | ||
|
||
expect( onChangeInitializedEditors ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should track ready editors', () => { | ||
const editors = new Collection(); | ||
const onChangeInitializedEditors = vi.fn(); | ||
const readyEditor = createMockEditor( 'ready', { name: '1' } ); | ||
editors.add( readyEditor ); | ||
|
||
renderHook( () => useInitializedCKEditorsMap( { | ||
currentContextWatchdog: createMockContextWatchdog( editors ), | ||
onChangeInitializedEditors | ||
} ) ); | ||
|
||
expect( onChangeInitializedEditors ).toHaveBeenCalledWith( | ||
expect.objectContaining( { | ||
'1': { | ||
instance: readyEditor, | ||
metadata: { | ||
name: '1' | ||
} | ||
} | ||
} ), | ||
expect.anything() | ||
); | ||
} ); | ||
|
||
it( 'should handle adding new editors to collection', () => { | ||
const editors = new Collection(); | ||
const onChangeInitializedEditors = vi.fn(); | ||
|
||
renderHook( () => useInitializedCKEditorsMap( { | ||
currentContextWatchdog: createMockContextWatchdog( editors ), | ||
onChangeInitializedEditors | ||
} ) ); | ||
|
||
const newEditor = createMockEditor( 'ready', { name: '2' } ); | ||
editors.add( newEditor ); | ||
|
||
// Simulate 'ready' event | ||
const readyCallback = newEditor.once.mock.calls.find( ( call: any ) => call[ 0 ] === 'ready' )![ 1 ]; | ||
readyCallback(); | ||
|
||
expect( onChangeInitializedEditors ).toHaveBeenLastCalledWith( | ||
expect.objectContaining( { | ||
'2': { | ||
instance: newEditor, | ||
metadata: { | ||
name: '2' | ||
} | ||
} | ||
} ), | ||
expect.anything() | ||
); | ||
} ); | ||
|
||
it( 'should handle editor destruction', () => { | ||
const editors = new Collection(); | ||
const onChangeInitializedEditors = vi.fn(); | ||
const editor = createMockEditor( 'ready', { name: '1' } ); | ||
editors.add( editor ); | ||
|
||
renderHook( () => useInitializedCKEditorsMap( { | ||
currentContextWatchdog: createMockContextWatchdog( editors ), | ||
onChangeInitializedEditors | ||
} ) ); | ||
|
||
// Simulate 'destroy' event | ||
const destroyCallback = editor.once.mock.calls.find( ( call: any ) => call[ 0 ] === 'destroy' )![ 1 ]; | ||
editors.remove( editor ); | ||
destroyCallback(); | ||
|
||
expect( onChangeInitializedEditors ).toHaveBeenLastCalledWith( | ||
expect.objectContaining( {} ), | ||
expect.anything() | ||
); | ||
} ); | ||
} ); | ||
|
||
function createMockEditor( state = 'ready', contextMetadata: CKEditorConfigContextMetadata, config = {} ) { | ||
const editor = new MockEditor( document.createElement( 'div' ), { | ||
get( name: string ) { | ||
if ( name === '$__CKEditorReactContextMetadata' ) { | ||
return contextMetadata; | ||
} | ||
|
||
if ( Object.prototype.hasOwnProperty.call( config, name ) ) { | ||
return config[ name ]; | ||
} | ||
|
||
return undefined; | ||
} | ||
} ); | ||
|
||
editor.state = state; | ||
editor.once = vi.fn(); | ||
|
||
return editor; | ||
} | ||
|
||
function createMockContextWatchdog( editors = new Collection() ) { | ||
return ( { | ||
status: 'initialized' as const, | ||
watchdog: { | ||
context: { | ||
editors | ||
} | ||
} | ||
} ) as unknown as ContextWatchdogValue<any>; | ||
} |
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
Oops, something went wrong.