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