qunit: Make eslint config pass on qunit test files
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.placeholder.test.js
1 ( function ( $ ) {
2 var html, testElement;
3
4 QUnit.module( 'jquery.placeholder', QUnit.newMwEnvironment() );
5
6 QUnit.test( 'caches results of feature tests', function ( assert ) {
7 assert.strictEqual( typeof $.fn.placeholder.input, 'boolean', '$.fn.placeholder.input' );
8 assert.strictEqual( typeof $.fn.placeholder.textarea, 'boolean', '$.fn.placeholder.textarea' );
9 } );
10
11 if ( $.fn.placeholder.input && $.fn.placeholder.textarea ) {
12 return;
13 }
14
15 html = '<form>' +
16 '<input id="input-type-search" type="search" placeholder="Search this site...">' +
17 '<input id="input-type-text" type="text" placeholder="e.g. John Doe">' +
18 '<input id="input-type-email" type="email" placeholder="e.g. address@example.ext">' +
19 '<input id="input-type-url" type="url" placeholder="e.g. http://mathiasbynens.be/">' +
20 '<input id="input-type-tel" type="tel" placeholder="e.g. +32 472 77 69 88">' +
21 '<input id="input-type-password" type="password" placeholder="e.g. hunter2">' +
22 '<textarea id="textarea" name="message" placeholder="Your message goes here"></textarea>' +
23 '</form>';
24 testElement = function ( $el, assert ) {
25 var el = $el[ 0 ],
26 placeholder = el.getAttribute( 'placeholder' );
27
28 assert.strictEqual( $el.placeholder(), $el, 'should be chainable' );
29
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' );
34
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' );
41
42 // and unfocus (blur) again
43 $el.blur();
44
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' );
49
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' );
55
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' );
61
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 };
69
70 QUnit.test( 'emulates placeholder for <input type=text>', function ( assert ) {
71 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
72 testElement( $( '#input-type-text' ), assert );
73 } );
74
75 QUnit.test( 'emulates placeholder for <input type=search>', function ( assert ) {
76 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
77 testElement( $( '#input-type-search' ), assert );
78 } );
79
80 QUnit.test( 'emulates placeholder for <input type=email>', function ( assert ) {
81 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
82 testElement( $( '#input-type-email' ), assert );
83 } );
84
85 QUnit.test( 'emulates placeholder for <input type=url>', function ( assert ) {
86 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
87 testElement( $( '#input-type-url' ), assert );
88 } );
89
90 QUnit.test( 'emulates placeholder for <input type=tel>', function ( assert ) {
91 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
92 testElement( $( '#input-type-tel' ), assert );
93 } );
94
95 QUnit.test( 'emulates placeholder for <input type=password>', function ( assert ) {
96 var $el, el, placeholder, selector = '#input-type-password';
97
98 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
99
100 $el = $( selector );
101 el = $el[ 0 ];
102 placeholder = el.getAttribute( 'placeholder' );
103
104 assert.strictEqual( $el.placeholder(), $el, 'should be chainable' );
105
106 // Re-select the element, as it gets replaced by another one in some browsers
107 $el = $( selector );
108 el = $el[ 0 ];
109
110 assert.strictEqual( el.value, placeholder, 'should set `placeholder` text as `value`' );
111 assert.strictEqual( $el.prop( 'value' ), '', 'propHooks works properly' );
112 assert.strictEqual( $el.val(), '', 'valHooks works properly' );
113 assert.ok( $el.hasClass( 'placeholder' ), 'should have `placeholder` class' );
114
115 // test on focus
116 $el.focus();
117
118 // Re-select the element, as it gets replaced by another one in some browsers
119 $el = $( selector );
120 el = $el[ 0 ];
121
122 assert.strictEqual( el.value, '', '`value` should be the empty string on focus' );
123 assert.strictEqual( $el.prop( 'value' ), '', 'propHooks works properly' );
124 assert.strictEqual( $el.val(), '', 'valHooks works properly' );
125 assert.ok( !$el.hasClass( 'placeholder' ), 'should not have `placeholder` class on focus' );
126
127 // and unfocus (blur) again
128 $el.blur();
129
130 // Re-select the element, as it gets replaced by another one in some browsers
131 $el = $( selector );
132 el = $el[ 0 ];
133
134 assert.strictEqual( el.value, placeholder, 'should set `placeholder` text as `value`' );
135 assert.strictEqual( $el.prop( 'value' ), '', 'propHooks works properly' );
136 assert.strictEqual( $el.val(), '', 'valHooks works properly' );
137 assert.ok( $el.hasClass( 'placeholder' ), 'should have `placeholder` class' );
138
139 } );
140
141 QUnit.test( 'emulates placeholder for <textarea>', function ( assert ) {
142 $( '<div>' ).html( html ).appendTo( $( '#qunit-fixture' ) );
143 testElement( $( '#textarea' ), assert );
144 } );
145
146 }( jQuery ) );