mediawiki-extensions-CodeMi.../tests/jest/setup.js
bhsd f8a89ccf32 CodeMirror: fix toggle-related issues
The hook handlers and event listeners associated with `CodeMirror.prototype.initialize()` are now removed when CM is toggled off.

For 2017 Wikitext Editor, the focus is always set on the VE surface view.

Bug: T380840
Bug: T380983
Bug: T381358
Change-Id: Ib83f3d49c3d0496cb570f62e75f3fdc0d700be47
2024-12-04 13:23:48 +08:00

48 lines
1.7 KiB
JavaScript

const mockBundle = require( '../../resources/lib/codemirror6.bundle.dist.js' );
jest.mock( 'ext.CodeMirror.v6.lib', () => mockBundle, { virtual: true } );
const mockCodeMirror = require( '../../resources/codemirror.js' );
jest.mock( 'ext.CodeMirror.v6', () => mockCodeMirror, { virtual: true } );
jest.mock( '../../resources/ext.CodeMirror.data.js', () => jest.fn(), { virtual: true } );
global.mw = require( '@wikimedia/mw-node-qunit/src/mockMediaWiki.js' )();
mw.user = Object.assign( mw.user, {
options: {
get: jest.fn().mockImplementation( ( key ) => {
if ( key === 'codemirror-preferences' ) {
return '{"bracketMatching":1,"lineWrapping":1,"activeLine":0,"specialChars":1,"bidiIsolation":1}';
}
// Only called for 'usecodemirror' option.
return '1';
} ),
set: jest.fn()
},
sessionId: jest.fn().mockReturnValue( 'abc' ),
getId: jest.fn().mockReturnValue( 123 ),
isNamed: jest.fn().mockReturnValue( true )
} );
mw.config.get = jest.fn().mockReturnValue( '1000+ edits' );
mw.track = jest.fn();
mw.Api.prototype.saveOption = jest.fn();
mw.hook = jest.fn( ( name ) => ( {
fire: jest.fn( ( ...args ) => {
if ( mw.hook.mockHooks[ name ] ) {
mw.hook.mockHooks[ name ].forEach( ( callback ) => callback( ...args ) );
}
} ),
add: jest.fn( ( callback ) => {
if ( !mw.hook.mockHooks[ name ] ) {
mw.hook.mockHooks[ name ] = [];
}
mw.hook.mockHooks[ name ].push( callback );
} ),
remove: jest.fn( ( callback ) => {
if ( mw.hook.mockHooks[ name ] ) {
mw.hook.mockHooks[ name ] = mw.hook.mockHooks[ name ]
.filter( ( cb ) => cb !== callback );
}
} )
} ) );
mw.hook.mockHooks = {};
global.$ = require( 'jquery' );
$.fn.textSelection = () => {};
window.matchMedia = jest.fn().mockReturnValue( { matches: false } );