mediawiki-extensions-CodeMi.../tests/jest/codemirror.preferences.test.js

78 lines
2.9 KiB
JavaScript
Raw Normal View History

CodeMirrorPreferences: add panel to tweak prefs with the editor open This is toggled by pressing Mod-Shift-, (or Command-Shift-, on MacOS), which then puts focus on the preferences panel. It can be closed with the Escape key, just like other CM panels. The CodeMirror class comes with these extension which can be toggled in preferences: * Bracket matching * Line numbering * Line wrapping * Highlight the active line * Show special characters Only bracket matching, line numbering, and line wrapping are available in the 2017 editor. The bidi isolation and template folding extensions are registered in CodeMirrorModeMediaWiki as they are MW-specific. CodeMirrorPreferences' new registerExtension() method allows any consumer of CodeMirror to add any arbitrary extensions to the preferences panel. This is expected to be called *after* CodeMirror has finished initializing. The 'ext.CodeMirror.ready' hook now passes the CodeMirror instance to accommodate this. The preferences are stored as a single user option in the database, called 'codemirror-preferences'. The defaults can be configured with the $wgCodeMirrorDefaultPreferences configuration setting. The sysadmin-facing values are the familiar boolean, but since CodeMirror is widely used, we make extra efforts to reduce the storage footprint (see T54777). This includes only storing preferences that differ from the defaults, and using binary representation instead of boolean values, since the user option is stored as a string. For now, all preferences are ignored in the 2017 editor. In a future patch, we may add some as toggleable Tools in the VE toolbar. Other changes: * Refactor CSS to use a .darkmode() mixin * Add a method to create a CSS-only fieldset in CodeMirrorPanel * Fix Jest tests now that there are more calls to mw.user.options.get() * Adjust Selenium tests to always use CM6 * Adjust Selenium tests to delete test pages (useful for local dev) * Remove unused code Bug: T359498 Change-Id: I70dcf2f49418cea632c452c1266440effad634f3
2024-08-16 01:52:13 +00:00
/* eslint-disable-next-line n/no-missing-require */
const { Compartment, EditorView } = require( 'ext.CodeMirror.v6.lib' );
const CodeMirrorPreferences = require( '../../resources/codemirror.preferences.js' );
describe( 'CodeMirrorPreferences', () => {
let mockDefaultPreferences, mockUserPreferences, getCodeMirrorPreferences;
beforeEach( () => {
mockDefaultPreferences = ( config = { fooExtension: false, barExtension: true } ) => {
mw.config.get = jest.fn().mockReturnValue( {
defaultPreferences: config
} );
};
mockUserPreferences = ( preferences = {} ) => {
mw.user.options.get = jest.fn().mockReturnValue( preferences );
};
/* eslint-disable-next-line arrow-body-style */
getCodeMirrorPreferences = () => {
return new CodeMirrorPreferences( {
fooExtension: EditorView.theme(),
barExtension: EditorView.theme()
} );
};
} );
it( 'defaultPreferences', () => {
mockDefaultPreferences();
const preferences = getCodeMirrorPreferences();
expect( preferences.defaultPreferences ).toStrictEqual( {
fooExtension: false,
barExtension: true
} );
} );
it( 'fetchPreferences', () => {
mockDefaultPreferences();
mockUserPreferences( '{"fooExtension":1}' );
mw.user.isNamed = jest.fn().mockReturnValue( true );
const preferences = getCodeMirrorPreferences();
expect( preferences.fetchPreferences() ).toStrictEqual( {
fooExtension: true,
barExtension: true
} );
} );
it( 'setPreference', () => {
mockDefaultPreferences();
mw.user.isNamed = jest.fn().mockReturnValue( true );
const preferences = getCodeMirrorPreferences();
preferences.setPreference( 'fooExtension', true );
expect( preferences.preferences.fooExtension ).toStrictEqual( true );
expect( mw.user.options.set ).toHaveBeenCalledWith( 'codemirror-preferences', '{"fooExtension":1}' );
expect( mw.Api.prototype.saveOption ).toHaveBeenCalledWith( 'codemirror-preferences', '{"fooExtension":1}' );
} );
it( 'getPreference', () => {
mockDefaultPreferences();
mockUserPreferences( '{"barExtension":0}' );
const preferences = getCodeMirrorPreferences();
expect( preferences.getPreference( 'fooExtension' ) ).toStrictEqual( false );
expect( preferences.getPreference( 'barExtension' ) ).toStrictEqual( false );
} );
it( 'registerExtension', () => {
mockDefaultPreferences( { fooExtension: false, barExtension: false } );
mockUserPreferences( '{"fooExtension":0,"barExtension":1}' );
const fooExtension = EditorView.theme();
const barExtension = EditorView.theme();
const preferences = getCodeMirrorPreferences( { fooExtension, barExtension } );
const view = new EditorView();
preferences.registerExtension( 'barExtension', barExtension, view );
expect( preferences.extensionRegistry.barExtension ).toStrictEqual( barExtension );
expect( preferences.compartmentRegistry.barExtension ).toBeInstanceOf( Compartment );
expect( preferences.compartmentRegistry.barExtension.get( view.state ).length )
.toStrictEqual( 2 );
} );
} );