aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/effects/core.js
blob: b2d039a541e8a6ded270aa685607aba580cb461c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
define( [
	"qunit",
	"jquery",
	"lib/common",
	"lib/helper",
	"ui/effect",
	"ui/effects/effect-blind",
	"ui/effects/effect-bounce",
	"ui/effects/effect-clip",
	"ui/effects/effect-drop",
	"ui/effects/effect-explode",
	"ui/effects/effect-fade",
	"ui/effects/effect-fold",
	"ui/effects/effect-highlight",
	"ui/effects/effect-puff",
	"ui/effects/effect-pulsate",
	"ui/effects/effect-scale",
	"ui/effects/effect-shake",
	"ui/effects/effect-size",
	"ui/effects/effect-slide",
	"ui/effects/effect-transfer"
], function( QUnit, $, common, helper ) {

QUnit.assert.present = function( value, array, message ) {
	this.push( jQuery.inArray( value, array ) !== -1, value, array, message );
};

QUnit.assert.notPresent = function( value, array, message ) {
	this.push( jQuery.inArray( value, array ) === -1, value, array, message );
};

// MinDuration is used for "short" animate tests where we are only concerned about the final
var minDuration = 60,

	// Duration is used for "long" animates where we plan on testing properties during animation
	duration = 200;

QUnit.module( "effects.core", { afterEach: helper.moduleAfterEach }  );

// TODO: test all signatures of .show(), .hide(), .toggle().
// Look at core's signatures and UI's signatures.
QUnit.test( ".hide() with step", function( assert ) {
	var ready = assert.async();
	assert.expect( 1 );
	var element = $( "#elem" ),
		step = function() {
			assert.ok( true, "step callback invoked" );
			step = $.noop;
		};

	element.hide( {
		step: function() {
			step();
		},
		complete: ready
	} );
} );

QUnit.test( "Immediate Return Conditions", function( assert ) {
	var hidden = $( "div.hidden" ),
		count = 0;
	assert.expect( 3 );
	hidden.hide( "blind", function() {
		assert.equal( ++count, 1, "Hide on hidden returned immediately" );
	} ).show().show( "blind", function() {
		assert.equal( ++count, 2, "Show on shown returned immediately" );
	} );
	assert.equal( ++count, 3, "Both Functions worked properly" );
} );

QUnit.test( ".hide() with hidden parent", function( assert ) {
	assert.expect( 1 );
	var element = $( "div.hidden" ).children();
	element.hide( "blind", function() {
		assert.equal( element.css( "display" ), "none", "display: none" );
	} );
} );

QUnit.test( "Parse of null for options", function( assert ) {
	var ready = assert.async();
	var hidden = $( "div.hidden" ),
		count = 0;
	assert.expect( 1 );
	hidden.show( "blind", null, 1, function() {
		assert.equal( ++count, 1, "null for options still works" );
		ready();
	} );
} );

QUnit.test( "removeClass", function( assert ) {
	assert.expect( 3 );

	var element = $( "<div>" );
	assert.equal( "", element[ 0 ].className );
	element.addClass( "destroyed" );
	assert.equal( "destroyed", element[ 0 ].className );
	element.removeClass();
	assert.equal( "", element[ 0 ].className );
} );

QUnit.module( "effects.core: animateClass", { afterEach: helper.moduleAfterEach }  );

QUnit.test( "animateClass works with borderStyle", function( assert ) {
	var ready = assert.async();
	var test = $( "div.animateClass" );
	assert.expect( 3 );
	test.toggleClass( "testAddBorder", minDuration, function() {
		test.toggleClass( "testAddBorder", minDuration, function() {
			assert.equal( test.css( "borderLeftStyle" ), "none", "None border set" );
			ready();
		} );
		assert.equal( test.css( "borderLeftStyle" ), "solid", "None border not immedately set" );
	} );
	assert.equal( test.css( "borderLeftStyle" ), "solid", "Solid border immedately set" );
} );

QUnit.test( "animateClass works with colors", function( assert ) {
	var ready = assert.async();
	var test = $( "div.animateClass" ),
		oldStep = jQuery.fx.step.backgroundColor;

	assert.expect( 2 );

	// We want to catch the first frame of animation
	jQuery.fx.step.backgroundColor = function( fx ) {
		oldStep.apply( this, arguments );

		// Make sure it has animated somewhere we can detect
		if ( fx.pos > 255 / 2000 ) {
			jQuery.fx.step.backgroundColor = oldStep;
			assert.notPresent( test.css( "backgroundColor" ),
				[ "#000000", "#ffffff", "#000", "#fff", "rgb(0, 0, 0)", "rgb(255,255,255)" ],
				"Color is not endpoints in middle." );
			test.stop( true, true );
		}
	};

	test.toggleClass( "testChangeBackground", {
		duration: 2000,
		complete: function() {
			assert.present( test.css( "backgroundColor" ), [ "#ffffff", "#fff", "rgb(255, 255, 255)" ], "Color is final" );
			ready();
		}
	} );
} );

QUnit.test( "animateClass calls step option", function( assert ) {
	assert.expect( 1 );
	var test = jQuery( "div.animateClass" ),
		step = function() {
			assert.ok( true, "Step Function Called" );
			step = $.noop;
		};
	test.toggleClass( "testChangeBackground", {
		step: function() {
			step();
		}
	} );

	test.stop( true, true );
} );

QUnit.test( "animateClass works with children", function( assert ) {
	assert.expect( 3 );
	var ready = assert.async();
	var animatedChild,
		test = $( "div.animateClass" ),
		h2 = test.find( "h2" );

	test.toggleClass( "testChildren", {
		children: true,
		duration: duration,
		complete: function() {
			assert.equal( h2.css( "fontSize" ), "20px", "Text size is final during complete" );
			test.toggleClass( "testChildren", {
				duration: duration,
				complete: function() {
					assert.equal( h2.css( "fontSize" ), "10px", "Text size revertted after class removed" );

					ready();
				},
				step: function( val, fx ) {
					if ( fx.elem === h2[ 0 ] ) {
						assert.ok( false, "Error - Animating property on h2" );
					}
				}
			} );
		},
		step: function( val, fx ) {
			if ( fx.prop === "fontSize" && fx.elem === h2[ 0 ] && !animatedChild ) {
				assert.equal( fx.end, 20, "animating font size on child" );
				animatedChild = true;
			}
		}
	} );
} );

QUnit.test( "animateClass clears style properties when stopped", function( assert ) {
	var ready = assert.async();
	var test = $( "div.animateClass" ),
		style = test[ 0 ].style,
		orig = style.cssText;

	assert.expect( 2 );

	test.addClass( "testChangeBackground", duration );
	assert.notEqual( orig, style.cssText, "cssText is not the same after starting animation" );

	test
		.stop( true, true )
		.promise()
		.then( function() {
			assert.equal( orig, String.prototype.trim.call( style.cssText ), "cssText is the same after stopping animation midway" );
			ready();
		} );
} );

QUnit.test( "animateClass: css and class changes during animation are not lost (#7106)",
function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );
	var test = $( "div.ticket7106" );

	// Ensure the class stays and that the css property stays
	function animationComplete() {
		assert.hasClasses( test, "testClass", "class change during animateClass was not lost" );
		assert.equal( test.height(), 100, "css change during animateClass was not lost" );
		ready();
	}

	// Add a class and change a style property after starting an animated class
	test.addClass( "animate", minDuration, animationComplete )
		.addClass( "testClass" )
		.height( 100 );
} );

QUnit.test( "createPlaceholder: only created for static or relative elements", function( assert ) {
	assert.expect( 4 );

	assert.ok( $.effects.createPlaceholder( $( ".relative" ) ).length, "placeholder created for relative element" );
	assert.ok( $.effects.createPlaceholder( $( ".static" ) ).length, "placeholder created for static element" );
	assert.ok( !$.effects.createPlaceholder( $( ".absolute" ) ), "placeholder not created for absolute element" );
	assert.ok( !$.effects.createPlaceholder( $( ".fixed" ) ), "placeholder not created for fixed element" );
} );

QUnit.test( "createPlaceholder: preserves layout affecting properties", function( assert ) {
	assert.expect( 7 );

	var position = 5,
		element = $( ".relative" ).css( {
			top: position,
			left: position
		} ),
		before = {
			offset: element.offset(),
			outerWidth: element.outerWidth( true ),
			outerHeight: element.outerHeight( true ),
			"float": element.css( "float" ),
			position: element.position()
		},
		placeholder = $.effects.createPlaceholder( element );

	// Placeholders are only placed to preserve the effect on layout. Considering
	// top and left do not change layout, they are not preserved, which makes some
	// of the math simpler in the implementation.
	assert.deepEqual( before.offset.top - position, placeholder.offset().top, "offset top preserved" );
	assert.deepEqual( before.offset.left - position, placeholder.offset().left, "offset left preserved" );
	assert.deepEqual( before.position.top - position, placeholder.position().top, "position top preserved" );
	assert.deepEqual( before.position.left - position, placeholder.position().left, "position left preserved" );

	assert.deepEqual( before[ "float" ], placeholder.css( "float" ), "float preserved" );
	assert.deepEqual( before.outerWidth, placeholder.outerWidth( true ), "width preserved" );
	assert.deepEqual( before.outerHeight, placeholder.outerHeight( true ), "height preserved" );
} );

QUnit.module( "transfer", { afterEach: helper.moduleAfterEach }  );

QUnit.test( "transfer() without callback", function( assert ) {
	var ready = assert.async();
	assert.expect( 0 );

	// Verify that the effect works without a callback
	$( "#elem" ).transfer( {
		to: ".animateClass",
		duration: 1
	} );
	setTimeout( function() {
		ready();
	}, 25 );
} );

QUnit.test( "transfer() with callback", function( assert ) {
	var ready = assert.async();
	assert.expect( 1 );
	$( "#elem" ).transfer( {
		to: ".animateClass",
		duration: 1
	}, function() {
		assert.ok( true, "callback invoked" );
		ready();
	} );
} );

$.each( $.effects.effect, function( effect ) {
	QUnit.module( "effects." + effect );

	common.testJshint( "effects/effect-" + effect );

	if ( effect === "transfer" ) {
		return;
	}
	QUnit.test( "show/hide", function( assert ) {
		var ready = assert.async();
		assert.expect( 12 );
		var hidden = $( "div.hidden" ),
			count = 0,
			test = 0;

		function queueTest( fn ) {
			count++;
			var point = count;
			return function( next ) {
				test++;
				assert.equal( point, test, "Queue function fired in order" );
				if ( fn ) {
					fn();
				} else {
					setTimeout( next, minDuration );
				}
			};
		}

		function duringTest( fn ) {
			return function( next ) {
				setTimeout( fn, minDuration / 2 );
				next();
			};
		}

		hidden
			.queue( queueTest() )
			.queue( duringTest( function() {
				assert.ok( hidden.is( ":animated" ),
					"Hidden is seen as animated during .show(\"" + effect + "\", time)" );
			} ) )
			.show( effect, minDuration, queueTest( function() {
				assert.equal( hidden.css( "display" ), "block",
					"Hidden is shown after .show(\"" + effect + "\", time)" );
				assert.ok( !$( ".ui-effects-placeholder" ).length,
					"No placeholder remains after .show(\"" + effect + "\", time)" );
			} ) )
			.queue( queueTest() )
			.queue( duringTest( function() {
				assert.ok( hidden.is( ":animated" ),
					"Hidden is seen as animated during .hide(\"" + effect + "\", time)" );
			} ) )
			.hide( effect, minDuration, queueTest( function() {
				assert.equal( hidden.css( "display" ), "none",
					"Back to hidden after .hide(\"" + effect + "\", time)" );
				assert.ok( !$( ".ui-effects-placeholder" ).length,
					"No placeholder remains after .hide(\"" + effect + "\", time)" );
			} ) )
			.queue( queueTest( function() {
				assert.deepEqual( hidden.queue(), [ "inprogress" ], "Only the inprogress sentinel remains" );
				ready();
			} ) );
	} );

	QUnit.test( "relative width & height - properties are preserved", function( assert ) {
		var ready = assert.async();
		var test = $( "div.relWidth.relHeight" ),
			width = test.width(), height = test.height(),
			cssWidth = test[ 0 ].style.width, cssHeight = test[ 0 ].style.height;

		assert.expect( 4 );
		test.toggle( effect, minDuration, function() {
			assert.equal( test[ 0 ].style.width, cssWidth, "Inline CSS Width has been reset after animation ended" );
			assert.equal( test[ 0 ].style.height, cssHeight, "Inline CSS Height has been rest after animation ended" );
			ready();
		} );
		assert.ok( Math.abs( test.width() - width ) / width < 0.05,
			"Width is close to the value when animation started" );
		assert.ok( Math.abs( test.height() - height ) / height < 0.05,
			"Height is close to the value when animation started" );
	} );
} );

} );