Followup r86622: add initial QUnit test cases for jquery.textSelection module.
authorBrion Vibber <brion@users.mediawiki.org>
Sat, 23 Jul 2011 00:44:00 +0000 (00:44 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sat, 23 Jul 2011 00:44:00 +0000 (00:44 +0000)
* tests .textSelection()'s encapsulateText method mostly (also uses setSelection, getContents, and getSelection)
* examples from WikiEditor toolbar: sig, bold, h2 (ownline), ulist (ownline & splitlines)
* confirms that splitlines works more or less as expected, at least for basic single-line, single split-line, and multi-line cases

Doesn't test the WikiEditor iframe mode since that's in a separate extension; when it's possible to test those things too, that'll need to be run there.

One of the h2 tests fails in IE6, returning selected text that's offset by one character from what's expected. I'm not sure whether it's actually selecting the wrong text or whether it's returning the wrong text -- needs further investigation.

Also note that there's no setContents submethod in textSelection, despite there being some notes about one.

tests/qunit/index.html
tests/qunit/suites/resources/jquery/jquery.textSelection.js [new file with mode: 0644]

index f0db23c..a581920 100644 (file)
@@ -47,6 +47,7 @@
        <script src="../../resources/jquery/jquery.localize.js"></script>
        <script src="../../resources/jquery/jquery.tabIndex.js"></script>
        <script src="../../resources/jquery/jquery.tablesorter.js"></script>
+       <script src="../../resources/jquery/jquery.textSelection.js"></script>
        <script src="../../resources/mediawiki/mediawiki.Title.js"></script>
        <script src="../../resources/mediawiki.special/mediawiki.special.js"></script>
        <script src="../../resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
@@ -73,6 +74,7 @@
        <script src="suites/resources/jquery/jquery.localize.js"></script>
        <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
        <script src="suites/resources/jquery/jquery.tablesorter.test.js" charset="UTF-8"></script>
+       <script src="suites/resources/jquery/jquery.textSelection.js" charset="UTF-8"></script>
        <script src="suites/resources/mediawiki/mediawiki.Title.js"></script>
        <script src="suites/resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
 </head>
diff --git a/tests/qunit/suites/resources/jquery/jquery.textSelection.js b/tests/qunit/suites/resources/jquery/jquery.textSelection.js
new file mode 100644 (file)
index 0000000..d231af4
--- /dev/null
@@ -0,0 +1,216 @@
+module( 'jquery.textSelection.js' );
+
+test( '-- Initial check', function() {
+       expect(1);
+       ok( $.fn.textSelection, 'jQuery.fn.textSelection defined' );
+} );
+
+/**
+ * Test factory for $.fn.textSelection('encapsulateText')
+ *
+ * @param options {object} associative array containing:
+ *   description {string}
+ *   input {string}
+ *   output {string}
+ *   start {int} starting char for selection
+ *   end {int} ending char for selection
+ *   params {object} add'l parameters for $().textSelection('encapsulateText')
+ */
+var encapsulateTest = function( options ) {
+       var opt = $.extend({
+               description: '',
+               before: {},
+               after: {},
+               replace: {}
+       }, options);
+
+       opt.before = $.extend({
+               text: '',
+               start: 0,
+               end: 0
+       }, opt.before);
+       opt.after = $.extend({
+               text: '',
+               selected: null
+       }, opt.after);
+
+       test( opt.description, function() {
+               var tests = 1;
+               if (opt.after.selected !== null) {
+                       tests++;
+               }
+               expect(tests);
+
+               var $fixture = $('<div id="qunit-fixture"></div>');
+               var $textarea = $('<textarea>');
+
+               $fixture.append($textarea);
+               $('body').append($fixture);
+
+               //$textarea.textSelection( 'setContents', opt.before.text); // this method is actually missing atm...
+               $textarea.val( opt.before.text ); // won't work with the WikiEditor iframe?
+
+               $textarea.textSelection( 'setSelection', {
+                       start: opt.before.start,
+                       end: opt.before.end
+               });
+
+               $textarea.textSelection( 'encapsulateSelection', opt.replace );
+
+               var text = $textarea.textSelection( 'getContents' );
+
+               equal( text, opt.after.text, 'Checking full text after encapsulation' );
+
+               if (opt.after.selected !== null) {
+                       var selected = $textarea.textSelection( 'getSelection' );
+                       equal( selected, opt.after.selected, 'Checking selected text after encapsulation.' );
+               }
+
+       } );
+};
+
+var sig = {
+       'pre': "--~~~~"
+}, bold = {
+       pre: "'''",
+       peri: 'Bold text',
+       post: "'''"
+}, h2 = {
+       'pre': '== ',
+       'peri': 'Heading 2',
+       'post': ' ==',
+       'regex': /^(\s*)(={1,6})(.*?)\2(\s*)$/,
+       'regexReplace': "\$1==\$3==\$4",
+       'ownline': true
+}, ulist = {
+       'pre': "* ",
+       'peri': 'Bulleted list item',
+       'post': "",
+       'ownline': true,
+       'splitlines': true
+};
+
+encapsulateTest({
+       description: "Adding sig to end of text",
+       before: {
+               text: "Wikilove dude! ",
+               start: 15,
+               end: 15
+       },
+       after: {
+               text: "Wikilove dude! --~~~~",
+               selected: ""
+       },
+       replace: sig
+});
+
+encapsulateTest({
+       description: "Adding bold to empty",
+       before: {
+               text: "",
+               start: 0,
+               end: 0
+       },
+       after: {
+               text: "'''Bold text'''",
+               selected: "Bold text" // selected because it's the default
+       },
+       replace: bold
+});
+
+encapsulateTest({
+       description: "Adding bold to existing text",
+       before: {
+               text: "Now is the time for all good men to come to the aid of their country",
+               start: 20,
+               end: 32
+       },
+       after: {
+               text: "Now is the time for '''all good men''' to come to the aid of their country",
+               selected: "" // empty because it's not the default'
+       },
+       replace: bold
+});
+
+encapsulateTest({
+       description: "ownline option: adding new h2",
+       before: {
+               text:"Before\nAfter",
+               start: 7,
+               end: 7
+       },
+       after: {
+               text: "Before\n== Heading 2 ==\nAfter",
+               selected: "Heading 2"
+       },
+       replace: h2
+});
+
+encapsulateTest({
+       description: "ownline option: turn a whole line into new h2",
+       before: {
+               text:"Before\nMy heading\nAfter",
+               start: 7,
+               end: 17
+       },
+       after: {
+               text: "Before\n== My heading ==\nAfter",
+               selected: ""
+       },
+       replace: h2
+});
+
+
+encapsulateTest({
+       description: "ownline option: turn a partial line into new h2",
+       before: {
+               text:"BeforeMy headingAfter",
+               start: 6,
+               end: 16
+       },
+       after: {
+               text: "Before\n== My heading ==\nAfter",
+               selected: ""
+       },
+       replace: h2
+});
+
+
+encapsulateTest({
+       description: "splitlines option: no selection, insert new list item",
+       before: {
+               text: "Before\nAfter",
+               start: 7,
+               end: 7
+       },
+       after: {
+               text: "Before\n* Bulleted list item\nAfter"
+       },
+       replace: ulist
+});
+
+encapsulateTest({
+       description: "splitlines option: single partial line selection, insert new list item",
+       before: {
+               text: "BeforeMy List ItemAfter",
+               start: 6,
+               end: 18
+       },
+       after: {
+               text: "Before\n* My List Item\nAfter"
+       },
+       replace: ulist
+});
+
+encapsulateTest({
+       description: "splitlines option: multiple lines",
+       before: {
+               text: "Before\nFirst\nSecond\nThird\nAfter",
+               start: 7,
+               end: 25
+       },
+       after: {
+               text: "Before\n* First\n* Second\n* Third\nAfter"
+       },
+       replace: ulist
+});