Debian Codegraph @

tests/jest/codemirror.wikieditor.test.js

raw
1 mw.loader = { getState: jest.fn() };
2
3 const CodeMirrorWikiEditor = require( '../../resources/codemirror.wikieditor.js' ),
4 $textarea = $( '<textarea>' )
5 .text( 'The Smashing Pumpkins' ),
6 cmWe = new CodeMirrorWikiEditor( $textarea );
7
8 beforeEach( () => {
9 // Simulate the button that enables/disables CodeMirror as WikiEditor doesn't exist here.
10 const btn = document.createElement( 'span' );
11 btn.id = 'mw-editbutton-codemirror';
12 btn.classList.add( 'tool' );
13 btn.setAttribute( 'rel', 'CodeMirror' );
14 document.body.appendChild( btn );
15
16 // Add WikiEditor context to the textarea.
17 cmWe.$textarea.data = jest.fn().mockReturnValue( {
18 modules: {
19 toolbar: {
20 $toolbar: $( btn )
21 }
22 }
23 } );
24
25 // Initialize CodeMirror.
26 cmWe.initialize();
27 } );
28
29 describe( 'addCodeMirrorToWikiEditor', () => {
30 cmWe.$textarea.wikiEditor = jest.fn();
31
32 it( 'should add the button to the toolbar', () => {
33 cmWe.addCodeMirrorToWikiEditor();
34 expect( cmWe.$textarea.wikiEditor ).toHaveBeenCalledWith(
35 'addToToolbar',
36 expect.objectContaining( {
37 groups: { codemirror: expect.any( Object ) }
38 } )
39 );
40 } );
41
42 it( 'should be readonly when the textarea is also readonly', () => {
43 const textarea = document.createElement( 'textarea' );
44 textarea.readOnly = true;
45 const cmWe2 = new CodeMirrorWikiEditor( textarea );
46 cmWe2.initialize();
47 cmWe2.addCodeMirrorToWikiEditor();
48 expect( cmWe2.readOnly ).toEqual( true );
49 expect( cmWe2.state.readOnly ).toEqual( true );
50 } );
51 } );
52
53 describe( 'updateToolbarButton', () => {
54 it( 'should update the toolbar button based on the current CodeMirror state', () => {
55 const btn = document.getElementById( 'mw-editbutton-codemirror' );
56 cmWe.setCodeMirrorPreference( false );
57 cmWe.updateToolbarButton();
58 expect( btn.classList.contains( 'mw-editbutton-codemirror-active' ) ).toBeFalsy();
59 cmWe.setCodeMirrorPreference( true );
60 cmWe.updateToolbarButton();
61 expect( btn.classList.contains( 'mw-editbutton-codemirror-active' ) ).toBeTruthy();
62 } );
63 } );