| 1 |
'use strict'; |
| 2 |
|
| 3 |
const assert = require( 'assert' ), |
| 4 |
EditPage = require( '../pageobjects/edit.page' ), |
| 5 |
FixtureContent = require( '../fixturecontent' ), |
| 6 |
LoginPage = require( 'wdio-mediawiki/LoginPage' ), |
| 7 |
UserPreferences = require( '../userpreferences' ), |
| 8 |
Util = require( 'wdio-mediawiki/Util' ); |
| 9 |
|
| 10 |
describe( 'CodeMirror textSelection for the wikitext 2010 editor', () => { |
| 11 |
let title; |
| 12 |
|
| 13 |
before( async () => { |
| 14 |
title = Util.getTestString( 'CodeMirror-fixture1-' ); |
| 15 |
await LoginPage.loginAdmin(); |
| 16 |
await FixtureContent.createFixturePage( title ); |
| 17 |
await UserPreferences.enableWikitext2010EditorWithCodeMirror(); |
| 18 |
await EditPage.openForEditing( title, true ); |
| 19 |
await EditPage.wikiEditorToolbar.waitForDisplayed(); |
| 20 |
await EditPage.clickText(); |
| 21 |
} ); |
| 22 |
|
| 23 |
// Content is "[]{{template}}" |
| 24 |
it( 'sets and gets the correct text when using setContents and getContents', async () => { |
| 25 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'setContents', 'foobar' ) ); |
| 26 |
assert.strictEqual( |
| 27 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ), |
| 28 |
'foobar' |
| 29 |
); |
| 30 |
} ); |
| 31 |
|
| 32 |
// Content is now "foobar" |
| 33 |
it( 'sets and gets the correct selection when using setSelection and getSelection', async () => { |
| 34 |
await browser.execute( () => { |
| 35 |
$( '.cm-editor' ).textSelection( 'setSelection', { start: 3, end: 6 } ); |
| 36 |
} ); |
| 37 |
assert.strictEqual( |
| 38 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getSelection' ) ), |
| 39 |
'bar' |
| 40 |
); |
| 41 |
} ); |
| 42 |
|
| 43 |
it( 'correctly replaces the selected text when using replaceSelection', async () => { |
| 44 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'replaceSelection', 'baz' ) ); |
| 45 |
assert.strictEqual( |
| 46 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ), |
| 47 |
'foobaz' |
| 48 |
); |
| 49 |
} ); |
| 50 |
|
| 51 |
// Content is now "foobaz" |
| 52 |
it( 'returns the correct values for getCaretPosition', async () => { |
| 53 |
await browser.execute( () => { |
| 54 |
$( '.cm-editor' ).textSelection( 'setSelection', { start: 3, end: 6 } ); |
| 55 |
} ); |
| 56 |
assert.strictEqual( |
| 57 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getCaretPosition' ) ), |
| 58 |
6 |
| 59 |
); |
| 60 |
assert.deepStrictEqual( |
| 61 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getCaretPosition', { startAndEnd: true } ) ), |
| 62 |
[ 3, 6 ] |
| 63 |
); |
| 64 |
} ); |
| 65 |
|
| 66 |
it( 'correctly wraps the selected text when using encapsulateSelection', async () => { |
| 67 |
await browser.execute( () => { |
| 68 |
$( '.cm-editor' ).textSelection( 'setContents', 'foobaz' ) |
| 69 |
.textSelection( 'encapsulateSelection', { |
| 70 |
selectionStart: 0, |
| 71 |
selectionEnd: 6, |
| 72 |
pre: '<div>', |
| 73 |
post: '</div>' |
| 74 |
} ); |
| 75 |
} ); |
| 76 |
assert.strictEqual( |
| 77 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ), |
| 78 |
'<div>foobaz</div>' |
| 79 |
); |
| 80 |
} ); |
| 81 |
|
| 82 |
it( "correctly inserts the 'peri' option when using encapsulateSelection", async () => { |
| 83 |
await browser.execute( () => { |
| 84 |
$( '.cm-editor' ).textSelection( 'setContents', 'foobaz' ) |
| 85 |
.textSelection( 'encapsulateSelection', { |
| 86 |
selectionStart: 0, |
| 87 |
selectionEnd: 6, |
| 88 |
pre: '<div>', |
| 89 |
post: '</div>', |
| 90 |
peri: 'Soundgarden', |
| 91 |
replace: true |
| 92 |
} ); |
| 93 |
} ); |
| 94 |
assert.strictEqual( |
| 95 |
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ), |
| 96 |
'<div>Soundgarden</div>' |
| 97 |
); |
| 98 |
} ); |
| 99 |
|
| 100 |
it( 'scrolls to the correct place when using scrollToCaretPosition', async () => { |
| 101 |
await browser.execute( () => { |
| 102 |
const $cmEditor = $( '.cm-editor' ); |
| 103 |
$cmEditor.textSelection( 'setContents', 'foobar\n'.repeat( 50 ) ); |
| 104 |
// Ensure caret is at the top. |
| 105 |
$cmEditor.textSelection( 'setSelection', { start: 0 } ); |
| 106 |
// Force scrolling to the bottom. |
| 107 |
$( '.cm-scroller' )[ 0 ].scrollTop = 5000; |
| 108 |
// Use textSelection to scroll back to caret. |
| 109 |
$cmEditor.textSelection( 'scrollToCaretPosition' ); |
| 110 |
} ); |
| 111 |
assert.strictEqual( |
| 112 |
await browser.execute( () => $( '.cm-scroller' ).scrollTop() ), |
| 113 |
0 |
| 114 |
); |
| 115 |
} ); |
| 116 |
|
| 117 |
// Content is now "foobar\n" repeated 50 times. |
| 118 |
it( 'retains the contents after turning CodeMirror off', async () => { |
| 119 |
await EditPage.legacyCodeMirrorButton.click(); |
| 120 |
await EditPage.legacyTextInput.waitForDisplayed(); |
| 121 |
assert.match( await EditPage.legacyTextInput.getValue(), /foobar/ ); |
| 122 |
} ); |
| 123 |
} ); |