| 
									
										
										
										
											2025-05-09 22:35:36 +03:00
										 |  |  | import { ClassicEditor, _setModelData as setModelData } from 'ckeditor5'; | 
					
						
							| 
									
										
										
										
											2022-03-04 13:39:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-20 14:27:21 +02:00
										 |  |  | import Mermaid from '../src/mermaid.js'; | 
					
						
							|  |  |  | import MermaidUI from '../src/mermaidui.js'; | 
					
						
							| 
									
										
										
										
											2025-05-10 02:23:22 +03:00
										 |  |  | import { afterEach, beforeEach, describe, it } from 'vitest'; | 
					
						
							|  |  |  | import { expect } from 'vitest'; | 
					
						
							| 
									
										
										
										
											2022-03-04 13:39:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* global document */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe( 'MermaidUI', () => { | 
					
						
							|  |  |  | 	it( 'should be named', () => { | 
					
						
							|  |  |  | 		expect( MermaidUI.pluginName ).to.equal( 'MermaidUI' ); | 
					
						
							|  |  |  | 	} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe( 'init()', () => { | 
					
						
							|  |  |  | 		let domElement, editor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		beforeEach( async () => { | 
					
						
							|  |  |  | 			domElement = document.createElement( 'div' ); | 
					
						
							|  |  |  | 			document.body.appendChild( domElement ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			editor = await ClassicEditor.create( domElement, { | 
					
						
							| 
									
										
										
										
											2025-05-10 02:25:11 +03:00
										 |  |  | 				licenseKey: "GPL", | 
					
						
							| 
									
										
										
										
											2022-03-04 13:39:39 +01:00
										 |  |  | 				plugins: [ | 
					
						
							| 
									
										
										
										
											2022-03-10 16:19:50 +01:00
										 |  |  | 					Mermaid | 
					
						
							| 
									
										
										
										
											2022-03-04 13:39:39 +01:00
										 |  |  | 				] | 
					
						
							|  |  |  | 			} ); | 
					
						
							|  |  |  | 		} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		afterEach( () => { | 
					
						
							|  |  |  | 			domElement.remove(); | 
					
						
							|  |  |  | 			return editor.destroy(); | 
					
						
							|  |  |  | 		} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it( 'should register the UI item', () => { | 
					
						
							|  |  |  | 			expect( editor.ui.componentFactory.has( 'mermaid' ) ).to.equal( true ); | 
					
						
							|  |  |  | 		} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it( 'has the base properties', () => { | 
					
						
							|  |  |  | 			const button = editor.ui.componentFactory.create( 'mermaid' ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-08 12:04:13 +01:00
										 |  |  | 			expect( button ).to.have.property( 'label', 'Insert Mermaid diagram' ); | 
					
						
							| 
									
										
										
										
											2022-03-04 13:39:39 +01:00
										 |  |  | 			expect( button ).to.have.property( 'icon' ); | 
					
						
							|  |  |  | 			expect( button ).to.have.property( 'tooltip', true ); | 
					
						
							|  |  |  | 		} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		describe( 'UI components', () => { | 
					
						
							|  |  |  | 			for ( const buttonName of [ | 
					
						
							|  |  |  | 				'mermaidPreview', | 
					
						
							|  |  |  | 				'mermaidSourceView', | 
					
						
							|  |  |  | 				'mermaidSplitView', | 
					
						
							|  |  |  | 				'mermaidInfo' | 
					
						
							|  |  |  | 			] ) { | 
					
						
							|  |  |  | 				it( `should register the ${ buttonName } button`, () => { | 
					
						
							|  |  |  | 					expect( editor.ui.componentFactory.has( buttonName ) ).to.equal( true ); | 
					
						
							|  |  |  | 				} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				it( `should add the base properties for ${ buttonName } button`, () => { | 
					
						
							|  |  |  | 					const button = editor.ui.componentFactory.create( buttonName ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					expect( button ).to.have.property( 'label' ); | 
					
						
							|  |  |  | 					expect( button ).to.have.property( 'icon' ); | 
					
						
							|  |  |  | 					expect( button ).to.have.property( 'tooltip', true ); | 
					
						
							|  |  |  | 				} ); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} ); | 
					
						
							| 
									
										
										
										
											2022-03-10 16:15:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it( 'should set focus inside textarea of a newly created mermaid', () => { | 
					
						
							|  |  |  | 			const button = editor.ui.componentFactory.create( 'mermaid' ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			button.fire( 'execute' ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expect( document.activeElement.tagName ).to.equal( 'TEXTAREA' ); | 
					
						
							|  |  |  | 		} ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it( 'should not crash if the button is fired inside model.change()', () => { | 
					
						
							|  |  |  | 			const button = editor.ui.componentFactory.create( 'mermaid' ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:19:50 +01:00
										 |  |  | 			setModelData( editor.model, '[]' ); | 
					
						
							| 
									
										
										
										
											2022-03-10 16:15:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			editor.model.change( () => { | 
					
						
							|  |  |  | 				button.fire( 'execute' ); | 
					
						
							|  |  |  | 			} ); | 
					
						
							|  |  |  | 			// As the conversion is to be executed after the model.change(), we don't have access to the fully prepared view and
 | 
					
						
							|  |  |  | 			// despite that, we should still successfully add mermaid widget to the editor, not requiring the selection change
 | 
					
						
							|  |  |  | 			// to the inside of the nonexisting textarea element.
 | 
					
						
							|  |  |  | 		} ); | 
					
						
							| 
									
										
										
										
											2022-03-04 13:39:39 +01:00
										 |  |  | 	} ); | 
					
						
							|  |  |  | } ); | 
					
						
							|  |  |  | 
 |