| 1 |
/* eslint-disable-next-line n/no-missing-require */ |
| 2 |
const { EditorView } = require( 'ext.CodeMirror.v6.lib' ); |
| 3 |
const CodeMirror = require( '../../resources/codemirror.js' ); |
| 4 |
const $textarea = $( '<textarea>' ), |
| 5 |
cm = new CodeMirror( $textarea ); |
| 6 |
|
| 7 |
describe( 'initialize', () => { |
| 8 |
const initializeWithForm = () => { |
| 9 |
const form = document.createElement( 'form' ); |
| 10 |
form.append( cm.$textarea[ 0 ] ); |
| 11 |
cm.$textarea[ 0 ].form.addEventListener = jest.fn(); |
| 12 |
cm.initialize(); |
| 13 |
}; |
| 14 |
|
| 15 |
it( 'should create the EditorState with the value of the textarea', () => { |
| 16 |
cm.$textarea.val( 'foobar' ); |
| 17 |
cm.$textarea.textSelection = jest.fn().mockReturnValue( 'foobar' ); |
| 18 |
cm.initialize(); |
| 19 |
expect( cm.view.state.doc.toString() ).toStrictEqual( 'foobar' ); |
| 20 |
} ); |
| 21 |
|
| 22 |
it( 'should set the height to the same as the textarea', () => { |
| 23 |
$textarea.css( 'height', '100px' ); |
| 24 |
cm.initialize(); |
| 25 |
// 100 + 6 (padding/border) = 106 |
| 26 |
expect( $( cm.view.dom ).height() ).toStrictEqual( 106 ); |
| 27 |
} ); |
| 28 |
|
| 29 |
it( 'should instantiate an EditorView and add .cm-editor to the DOM', () => { |
| 30 |
initializeWithForm(); |
| 31 |
expect( cm.view ).toBeInstanceOf( EditorView ); |
| 32 |
expect( cm.view.dom ).toBeInstanceOf( HTMLDivElement ); |
| 33 |
expect( cm.$textarea[ 0 ].nextSibling ).toStrictEqual( cm.view.dom ); |
| 34 |
} ); |
| 35 |
|
| 36 |
it( 'should hide the native textarea', () => { |
| 37 |
cm.initialize(); |
| 38 |
expect( cm.$textarea[ 0 ].style.display ).toStrictEqual( 'none' ); |
| 39 |
} ); |
| 40 |
|
| 41 |
it( 'should add a listener for form submission', () => { |
| 42 |
initializeWithForm(); |
| 43 |
expect( cm.$textarea[ 0 ].form.addEventListener ).toHaveBeenCalledTimes( 1 ); |
| 44 |
} ); |
| 45 |
|
| 46 |
it( 'should retain the mw-editfont- class present on the textarea', () => { |
| 47 |
cm.$textarea.addClass( 'mw-editfont-monospace' ); |
| 48 |
cm.initialize(); |
| 49 |
expect( cm.view.dom.querySelector( '.cm-content' ).classList ).toContain( 'mw-editfont-monospace' ); |
| 50 |
} ); |
| 51 |
|
| 52 |
it( "should copy the 'dir' and 'lang' attributes of the textarea to .cm-editor", () => { |
| 53 |
cm.$textarea.prop( 'dir', 'rtl' ) |
| 54 |
.prop( 'lang', 'ar' ); |
| 55 |
cm.initialize(); |
| 56 |
expect( cm.view.dom.getAttribute( 'dir' ) ).toStrictEqual( 'rtl' ); |
| 57 |
expect( cm.view.dom.getAttribute( 'lang' ) ).toStrictEqual( 'ar' ); |
| 58 |
} ); |
| 59 |
} ); |
| 60 |
|
| 61 |
describe( 'logUsage', () => { |
| 62 |
it( 'should track usage of CodeMirror with the correct data', () => { |
| 63 |
CodeMirror.logUsage( { |
| 64 |
editor: 'wikitext', |
| 65 |
enabled: true, |
| 66 |
toggled: false |
| 67 |
} ); |
| 68 |
expect( mw.track ).toBeCalledWith( 'event.CodeMirrorUsage', { |
| 69 |
editor: 'wikitext', |
| 70 |
enabled: true, |
| 71 |
// eslint-disable-next-line camelcase |
| 72 |
session_token: 'abc', |
| 73 |
toggled: false, |
| 74 |
// eslint-disable-next-line camelcase |
| 75 |
user_edit_count_bucket: '1000+ edits', |
| 76 |
// eslint-disable-next-line camelcase |
| 77 |
user_id: 123 |
| 78 |
} ); |
| 79 |
} ); |
| 80 |
} ); |
| 81 |
|
| 82 |
describe( 'setCodeMirrorPreference', () => { |
| 83 |
beforeEach( () => { |
| 84 |
cm.initialize(); |
| 85 |
} ); |
| 86 |
|
| 87 |
it( 'should save using the API with the correct value', () => { |
| 88 |
CodeMirror.setCodeMirrorPreference( true ); |
| 89 |
expect( mw.Api.prototype.saveOption ).toHaveBeenCalledWith( 'usecodemirror', 1 ); |
| 90 |
expect( mw.user.options.set ).toHaveBeenCalledWith( 'usecodemirror', 1 ); |
| 91 |
} ); |
| 92 |
|
| 93 |
it( 'should not save preferences if the user is not named', () => { |
| 94 |
mw.user.isNamed = jest.fn().mockReturnValue( false ); |
| 95 |
CodeMirror.setCodeMirrorPreference( true ); |
| 96 |
expect( mw.Api.prototype.saveOption ).toHaveBeenCalledTimes( 0 ); |
| 97 |
expect( mw.user.options.set ).toHaveBeenCalledTimes( 0 ); |
| 98 |
} ); |
| 99 |
} ); |