Merge "Define 'MW_UPDATER' when running update.php"
[mediawiki.git] / tests / qunit / suites / resources / jquery / jquery.placeholder.test.js
blobca0ea6708c09b91c94d5e506a30bbd9638492a81
1 (function($) {
3         QUnit.module('jquery.placeholder', QUnit.newMwEnvironment());
5         QUnit.test('caches results of feature tests', 2, function(assert) {
6                 assert.strictEqual(typeof $.fn.placeholder.input, 'boolean', '$.fn.placeholder.input');
7                 assert.strictEqual(typeof $.fn.placeholder.textarea, 'boolean', '$.fn.placeholder.textarea');
8         });
10         if ($.fn.placeholder.input && $.fn.placeholder.textarea) {
11                 return;
12         }
14         var html = '<form>' +
15                         '<input id="input-type-search" type="search" placeholder="Search this site...">' +
16                         '<input id="input-type-text" type="text" placeholder="e.g. John Doe">' +
17                         '<input id="input-type-email" type="email" placeholder="e.g. address@example.ext">' +
18                         '<input id="input-type-url" type="url" placeholder="e.g. http://mathiasbynens.be/">' +
19                         '<input id="input-type-tel" type="tel" placeholder="e.g. +32 472 77 69 88">' +
20                         '<input id="input-type-password" type="password" placeholder="e.g. hunter2">' +
21                         '<textarea id="textarea" name="message" placeholder="Your message goes here"></textarea>' +
22                 '</form>',
23         testElement = function($el, assert) {
25                 var el = $el[0],
26                         placeholder = el.getAttribute('placeholder');
28                 assert.strictEqual($el.placeholder(), $el, 'should be chainable');
30                 assert.strictEqual(el.value, placeholder, 'should set `placeholder` text as `value`');
31                 assert.strictEqual($el.prop('value'), '', 'propHooks works properly');
32                 assert.strictEqual($el.val(), '', 'valHooks works properly');
33                 assert.ok($el.hasClass('placeholder'), 'should have `placeholder` class');
35                 // test on focus
36                 $el.focus();
37                 assert.strictEqual(el.value, '', '`value` should be the empty string on focus');
38                 assert.strictEqual($el.prop('value'), '', 'propHooks works properly');
39                 assert.strictEqual($el.val(), '', 'valHooks works properly');
40                 assert.ok(!$el.hasClass('placeholder'), 'should not have `placeholder` class on focus');
42                 // and unfocus (blur) again
43                 $el.blur();
45                 assert.strictEqual(el.value, placeholder, 'should set `placeholder` text as `value`');
46                 assert.strictEqual($el.prop('value'), '', 'propHooks works properly');
47                 assert.strictEqual($el.val(), '', 'valHooks works properly');
48                 assert.ok($el.hasClass('placeholder'), 'should have `placeholder` class');
50                 // change the value
51                 $el.val('lorem ipsum');
52                 assert.strictEqual($el.prop('value'), 'lorem ipsum', '`$el.val(string)` should change the `value` property');
53                 assert.strictEqual(el.value, 'lorem ipsum', '`$el.val(string)` should change the `value` attribute');
54                 assert.ok(!$el.hasClass('placeholder'), '`$el.val(string)` should remove `placeholder` class');
56                 // and clear it again
57                 $el.val('');
58                 assert.strictEqual($el.prop('value'), '', '`$el.val("")` should change the `value` property');
59                 assert.strictEqual(el.value, placeholder, '`$el.val("")` should change the `value` attribute');
60                 assert.ok($el.hasClass('placeholder'), '`$el.val("")` should re-enable `placeholder` class');
62                 // make sure the placeholder property works as expected.
63                 assert.strictEqual($el.prop('placeholder'), placeholder, '$el.prop(`placeholder`) should return the placeholder value');
64                 $el.placeholder('new placeholder');
65                 assert.strictEqual(el.getAttribute('placeholder'), 'new placeholder', '$el.placeholder(<string>) should set the placeholder value');
66                 assert.strictEqual(el.value, 'new placeholder', '$el.placeholder(<string>) should update the displayed placeholder value');
67                 $el.placeholder(placeholder);
68         };
70         QUnit.test('emulates placeholder for <input type=text>', 22, function(assert) {
71                 $('<div>').html(html).appendTo($('#qunit-fixture'));
72                 testElement($('#input-type-text'), assert);
73         });
75         QUnit.test('emulates placeholder for <input type=search>', 22, function(assert) {
76                 $('<div>').html(html).appendTo($('#qunit-fixture'));
77                 testElement($('#input-type-search'), assert);
78         });
80         QUnit.test('emulates placeholder for <input type=email>', 22, function(assert) {
81                 $('<div>').html(html).appendTo($('#qunit-fixture'));
82                 testElement($('#input-type-email'), assert);
83         });
85         QUnit.test('emulates placeholder for <input type=url>', 22, function(assert) {
86                 $('<div>').html(html).appendTo($('#qunit-fixture'));
87                 testElement($('#input-type-url'), assert);
88         });
90         QUnit.test('emulates placeholder for <input type=tel>', 22, function(assert) {
91                 $('<div>').html(html).appendTo($('#qunit-fixture'));
92                 testElement($('#input-type-tel'), assert);
93         });
95         QUnit.test('emulates placeholder for <input type=password>', 13, function(assert) {
96                 $('<div>').html(html).appendTo($('#qunit-fixture'));
98                 var selector = '#input-type-password',
99                         $el = $(selector),
100                         el = $el[0],
101                         placeholder = el.getAttribute('placeholder');
103                 assert.strictEqual($el.placeholder(), $el, 'should be chainable');
105                 // Re-select the element, as it gets replaced by another one in some browsers
106                 $el = $(selector);
107                 el = $el[0];
109                 assert.strictEqual(el.value, placeholder, 'should set `placeholder` text as `value`');
110                 assert.strictEqual($el.prop('value'), '', 'propHooks works properly');
111                 assert.strictEqual($el.val(), '', 'valHooks works properly');
112                 assert.ok($el.hasClass('placeholder'), 'should have `placeholder` class');
114                 // test on focus
115                 $el.focus();
117                 // Re-select the element, as it gets replaced by another one in some browsers
118                 $el = $(selector);
119                 el = $el[0];
121                 assert.strictEqual(el.value, '', '`value` should be the empty string on focus');
122                 assert.strictEqual($el.prop('value'), '', 'propHooks works properly');
123                 assert.strictEqual($el.val(), '', 'valHooks works properly');
124                 assert.ok(!$el.hasClass('placeholder'), 'should not have `placeholder` class on focus');
126                 // and unfocus (blur) again
127                 $el.blur();
129                 // Re-select the element, as it gets replaced by another one in some browsers
130                 $el = $(selector);
131                 el = $el[0];
133                 assert.strictEqual(el.value, placeholder, 'should set `placeholder` text as `value`');
134                 assert.strictEqual($el.prop('value'), '', 'propHooks works properly');
135                 assert.strictEqual($el.val(), '', 'valHooks works properly');
136                 assert.ok($el.hasClass('placeholder'), 'should have `placeholder` class');
138         });
140         QUnit.test('emulates placeholder for <textarea></textarea>', 22, function(assert) {
141                 $('<div>').html(html).appendTo($('#qunit-fixture'));
142                 testElement($('#textarea'), assert);
143         });
145 }(jQuery));