From a786ef815a96f5841369270615352960a51980a6 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Wed, 13 Jul 2011 22:37:51 +0000 Subject: [PATCH] Unit tests for jquery.localize (Follow-up r92069) Also fixing a bug in jquery.localize. Previously it did mw.msg(key, [p1, p2, ...]); (passing an array as second argument), however mw.msg doesn't take an array as second argument, instead the second argument is the first in a list of optional variadic arguments. (like mw.msg(key, p1, p2, ..); In cases were only 1 variable is passed, this didn't brake the test as [p1].toString() === p1. Fixing by using .apply instead and creating an array of arguments, starting with the key (unshifted) and if available adding parameters. All these tests appear to be broken in Gecko-browsers because it uses a different attribute order (WebKit moslty in order of touch, Gecko mostly alphabetical, Trident/Presto engine different too). They are reported because the elements are compared as strings. I'll fix this in a better way when I think of one (soon!) --- resources/jquery/jquery.localize.js | 5 +- tests/qunit/index.html | 2 + .../resources/jquery/jquery.localize.js | 134 ++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 tests/qunit/suites/resources/jquery/jquery.localize.js diff --git a/resources/jquery/jquery.localize.js b/resources/jquery/jquery.localize.js index 23b02dddca..653767a73a 100644 --- a/resources/jquery/jquery.localize.js +++ b/resources/jquery/jquery.localize.js @@ -34,7 +34,10 @@ $.fn.localize = function( options ) { options = $.extend( { 'prefix': '', 'keys': {}, 'params': {} }, options ); function msg( key ) { - return mw.msg( options.prefix + ( key in options.keys ? options.keys[key] : key ), ( key in options.params ? options.params[key] : [] ) ) + var args = key in options.params ? options.params[key] : []; + // Format: mw.msg( key [, p1, p2, ...] ) + args.unshift( options.prefix + ( key in options.keys ? options.keys[key] : key ) ); + return mw.msg.apply( mw, args ); }; return $(this) .find( 'msg,html\\:msg' ) diff --git a/tests/qunit/index.html b/tests/qunit/index.html index 2834200ce9..81fe7ecbd6 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -43,6 +43,7 @@ + @@ -67,6 +68,7 @@ + diff --git a/tests/qunit/suites/resources/jquery/jquery.localize.js b/tests/qunit/suites/resources/jquery/jquery.localize.js new file mode 100644 index 0000000000..66b4a98ed3 --- /dev/null +++ b/tests/qunit/suites/resources/jquery/jquery.localize.js @@ -0,0 +1,134 @@ +module( 'jquery.localize.js' ); + +test( '-- Initial check', function() { + expect(1); + ok( $.fn.localize, 'jQuery.fn.localize defined' ); +} ); + +test( 'Handle basic replacements', function() { + expect(4); + + var html, $lc, expected; + mw.messages.set( 'basic', 'Basic stuff' ); + + // Tag: html:msg + html = '
'; + $lc = $( html ).localize(); + expected = 'Basic stuff'; + + strictEqual( $lc.html(), expected, 'Tag: html:msg' ); + + // Tag: msg (deprecated) + html = '
'; + $lc = $( html ).localize(); + expected = 'Basic stuff'; + + strictEqual( $lc.html(), expected, 'Tag: msg' ); + + // Attribute: title-msg + html = '
'; + $lc = $( html ).localize(); + expected = ''; + + strictEqual( $lc.html(), expected, 'Attribute: title-msg' ); + + // Attribute: alt-msg + html = '
'; + $lc = $( html ).localize(); + expected = ''; + + strictEqual( $lc.html(), expected, 'Attribute: alt-msg' ); +} ); + +test( 'Proper escaping', function() { + expect(2); + + var html, $lc, expected; + mw.messages.set( 'properfoo', '' ); + + // This is handled by jQuery inside $.fn.localize, just a simple sanity checked + // making sure it is actually using text() and attr() (or something with the same effect) + + // Text escaping + html = '
'; + $lc = $( html ).localize(); + expected = '<proper esc="test">'; + + strictEqual( $lc.html(), expected, 'Content is inserted as text, not as html.' ); + + // Attribute escaping + html = '
'; + $lc = $( html ).localize(); + expected = ''; + + strictEqual( $lc.html(), expected, 'Attributes are not inserted raw.' ); + + +} ); + +test( 'Options', function() { + expect(4); + + mw.messages.set( { + 'foo-lorem': 'Lorem', + 'foo-ipsum': 'Ipsum', + 'foo-bar-title': 'Read more about bars', + 'foo-bar-label': 'The Bars', + 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)', + 'foo-bazz-label': 'The Bazz ($1)', + 'foo-welcome': 'Welcome to $1! (last visit: $2)' + } ); + var html, $lc, expected, x, sitename = 'Wikipedia'; + + // Message key prefix + html = '
'; + $lc = $( html ).localize( { + prefix: 'foo-' + } ); + expected = 'Ipsum'; + + strictEqual( $lc.html(), expected, 'Message key prefix' ); + + // Variable keys mapping + x = 'bar'; + html = '
'; + $lc = $( html ).localize( { + keys: { + 'title': 'foo-' + x + '-title', + 'label': 'foo-' + x + '-label' + } + } ); + expected = 'The Bars'; + + strictEqual( $lc.html(), expected, 'Message prefix' ); + + // Passing parameteters to mw.msg + html = '
'; + $lc = $( html ).localize( { + params: { + 'foo-welcome': [sitename, 'yesterday'] + } + } ); + expected = 'Welcome to Wikipedia! (last visit: yesterday)'; + + strictEqual( $lc.html(), expected, 'Message prefix' ); + + // Combination of options prefix, params and keys + x = 'bazz'; + html = '
'; + $lc = $( html ).localize( { + prefix: 'foo-', + keys: { + 'title': x + '-title', + 'label': x + '-label' + }, + params: { + 'title': [sitename, '3 minutes ago'], + 'label': [sitename, '3 minutes ago'] + + } + } ); + expected = 'The Bazz (Wikipedia)'; + + strictEqual( $lc.html(), expected, 'Message prefix' ); +} ); -- 2.20.1