Merge "Some fixes to our jQuery UI skin for buttons"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.localize.test.js
1 QUnit.module( 'jquery.localize', QUnit.newMwEnvironment() );
2
3 QUnit.test( 'Handle basic replacements', 3, function ( assert ) {
4 var html, $lc;
5 mw.messages.set( 'basic', 'Basic stuff' );
6
7 // Tag: html:msg
8 html = '<div><span><html:msg key="basic" /></span></div>';
9 $lc = $( html ).localize().find( 'span' );
10
11 assert.strictEqual( $lc.text(), 'Basic stuff', 'Tag: html:msg' );
12
13 // Attribute: title-msg
14 html = '<div><span title-msg="basic"></span></div>';
15 $lc = $( html ).localize().find( 'span' );
16
17 assert.strictEqual( $lc.attr( 'title' ), 'Basic stuff', 'Attribute: title-msg' );
18
19 // Attribute: alt-msg
20 html = '<div><span alt-msg="basic"></span></div>';
21 $lc = $( html ).localize().find( 'span' );
22
23 assert.strictEqual( $lc.attr( 'alt' ), 'Basic stuff', 'Attribute: alt-msg' );
24
25 // Attribute: placeholder-msg
26 html = '<div><input placeholder-msg="basic" /></div>';
27 $lc = $( html ).localize().find( 'input' );
28
29 assert.strictEqual( $lc.attr( 'placeholder' ), 'Basic stuff', 'Attribute: placeholder-msg' );
30 } );
31
32 QUnit.test( 'Proper escaping', 2, function ( assert ) {
33 var html, $lc;
34 mw.messages.set( 'properfoo', '<proper esc="test">' );
35
36 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
37 // making sure it is actually using text() and attr() (or something with the same effect)
38
39 // Text escaping
40 html = '<div><span><html:msg key="properfoo"></span></div>';
41 $lc = $( html ).localize().find( 'span' );
42
43 assert.strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
44
45 // Attribute escaping
46 html = '<div><span title-msg="properfoo"></span></div>';
47 $lc = $( html ).localize().find( 'span' );
48
49 assert.strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
50 } );
51
52 QUnit.test( 'Options', 7, function ( assert ) {
53 mw.messages.set( {
54 'foo-lorem': 'Lorem',
55 'foo-ipsum': 'Ipsum',
56 'foo-bar-title': 'Read more about bars',
57 'foo-bar-label': 'The Bars',
58 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
59 'foo-bazz-label': 'The Bazz ($1)',
60 'foo-welcome': 'Welcome to $1! (last visit: $2)'
61 } );
62 var html, $lc, attrs, x, sitename = 'Wikipedia';
63
64 // Message key prefix
65 html = '<div><span title-msg="lorem"><html:msg key="ipsum"></span></div>';
66 $lc = $( html ).localize( {
67 prefix: 'foo-'
68 } ).find( 'span' );
69
70 assert.strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
71 assert.strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' );
72
73 // Variable keys mapping
74 x = 'bar';
75 html = '<div><span title-msg="title"><html:msg key="label"></span></div>';
76 $lc = $( html ).localize( {
77 keys: {
78 'title': 'foo-' + x + '-title',
79 'label': 'foo-' + x + '-label'
80 }
81 } ).find( 'span' );
82
83 assert.strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
84 assert.strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' );
85
86 // Passing parameteters to mw.msg
87 html = '<div><span><html:msg key="foo-welcome"></span></div>';
88 $lc = $( html ).localize( {
89 params: {
90 'foo-welcome': [sitename, 'yesterday']
91 }
92 } ).find( 'span' );
93
94 assert.strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
95
96 // Combination of options prefix, params and keys
97 x = 'bazz';
98 html = '<div><span title-msg="title"><html:msg key="label"></span></div>';
99 $lc = $( html ).localize( {
100 prefix: 'foo-',
101 keys: {
102 'title': x + '-title',
103 'label': x + '-label'
104 },
105 params: {
106 'title': [sitename, '3 minutes ago'],
107 'label': [sitename, '3 minutes ago']
108
109 }
110 } ).find( 'span' );
111
112 assert.strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
113 assert.strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );
114 } );