(bug 37390) Clean up QUnit test suites
[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
26 QUnit.test( 'Proper escaping', 2, function ( assert ) {
27 var html, $lc;
28 mw.messages.set( 'properfoo', '<proper esc="test">' );
29
30 // This is handled by jQuery inside $.fn.localize, just a simple sanity checked
31 // making sure it is actually using text() and attr() (or something with the same effect)
32
33 // Text escaping
34 html = '<div><span><html:msg key="properfoo" /></span></div>';
35 $lc = $( html ).localize().find( 'span' );
36
37 assert.strictEqual( $lc.text(), mw.msg( 'properfoo' ), 'Content is inserted as text, not as html.' );
38
39 // Attribute escaping
40 html = '<div><span title-msg="properfoo" /></span></div>';
41 $lc = $( html ).localize().find( 'span' );
42
43 assert.strictEqual( $lc.attr( 'title' ), mw.msg( 'properfoo' ), 'Attributes are not inserted raw.' );
44 } );
45
46 QUnit.test( 'Options', 7, function ( assert ) {
47 mw.messages.set( {
48 'foo-lorem': 'Lorem',
49 'foo-ipsum': 'Ipsum',
50 'foo-bar-title': 'Read more about bars',
51 'foo-bar-label': 'The Bars',
52 'foo-bazz-title': 'Read more about bazz at $1 (last modified: $2)',
53 'foo-bazz-label': 'The Bazz ($1)',
54 'foo-welcome': 'Welcome to $1! (last visit: $2)'
55 } );
56 var html, $lc, attrs, x, sitename = 'Wikipedia';
57
58 // Message key prefix
59 html = '<div><span title-msg="lorem"><html:msg key="ipsum" /></span></div>';
60 $lc = $( html ).localize( {
61 prefix: 'foo-'
62 } ).find( 'span' );
63
64 assert.strictEqual( $lc.attr( 'title' ), 'Lorem', 'Message key prefix - attr' );
65 assert.strictEqual( $lc.text(), 'Ipsum', 'Message key prefix - text' );
66
67 // Variable keys mapping
68 x = 'bar';
69 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
70 $lc = $( html ).localize( {
71 keys: {
72 'title': 'foo-' + x + '-title',
73 'label': 'foo-' + x + '-label'
74 }
75 } ).find( 'span' );
76
77 assert.strictEqual( $lc.attr( 'title' ), 'Read more about bars', 'Variable keys mapping - attr' );
78 assert.strictEqual( $lc.text(), 'The Bars', 'Variable keys mapping - text' );
79
80 // Passing parameteters to mw.msg
81 html = '<div><span><html:msg key="foo-welcome" /></span></div>';
82 $lc = $( html ).localize( {
83 params: {
84 'foo-welcome': [sitename, 'yesterday']
85 }
86 } ).find( 'span' );
87
88 assert.strictEqual( $lc.text(), 'Welcome to Wikipedia! (last visit: yesterday)', 'Passing parameteters to mw.msg' );
89
90 // Combination of options prefix, params and keys
91 x = 'bazz';
92 html = '<div><span title-msg="title"><html:msg key="label" /></span></div>';
93 $lc = $( html ).localize( {
94 prefix: 'foo-',
95 keys: {
96 'title': x + '-title',
97 'label': x + '-label'
98 },
99 params: {
100 'title': [sitename, '3 minutes ago'],
101 'label': [sitename, '3 minutes ago']
102
103 }
104 } ).find( 'span' );
105
106 assert.strictEqual( $lc.text(), 'The Bazz (Wikipedia)', 'Combination of options prefix, params and keys - text' );
107 assert.strictEqual( $lc.attr( 'title' ), 'Read more about bazz at Wikipedia (last modified: 3 minutes ago)', 'Combination of options prefix, params and keys - attr' );
108 } );