Skip to content

Commit 2e1c72f

Browse files
committed
Tests: Convert closeEnough() to a proper QUnit assertion
Should eventually replace with qunit-assert-close. Ref #10119 Ref gh-1528
1 parent 7c896dd commit 2e1c72f

File tree

7 files changed

+59
-54
lines changed

7 files changed

+59
-54
lines changed

tests/.jshintrc

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
"globals": {
2222
"asyncTest": false,
23-
"closeEnough": false,
2423
"deepEqual": false,
2524
"define": false,
2625
"domEqual": false,

tests/lib/qunit.js

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ QUnit.reset = ( function( reset ) {
4040
};
4141
} )( QUnit.reset );
4242

43+
// TODO: switch to qunit-assert-close plugin
44+
QUnit.assert.close = function( actual, expected, maxDifference, message ) {
45+
var passes = ( actual === expected ) || Math.abs( actual - expected ) <= maxDifference;
46+
QUnit.push( passes, actual, expected, message );
47+
};
48+
4349
return QUnit;
4450

4551
} );

tests/unit/dialog/dialog_options.js

+31-31
Original file line numberDiff line numberDiff line change
@@ -262,83 +262,83 @@ test("height", function() {
262262
element.remove();
263263
});
264264

265-
test("maxHeight", function() {
265+
test("maxHeight", function( assert ) {
266266
expect(3);
267267

268268
var element = $("<div></div>").dialog({ maxHeight: 200 });
269269
TestHelpers.dialog.drag(element, ".ui-resizable-s", 1000, 1000);
270-
closeEnough(element.dialog("widget").height(), 200, 1, "maxHeight");
270+
assert.close(element.dialog("widget").height(), 200, 1, "maxHeight");
271271
element.remove();
272272

273273
element = $("<div></div>").dialog({ maxHeight: 200 });
274274
TestHelpers.dialog.drag(element, ".ui-resizable-n", -1000, -1000);
275-
closeEnough(element.dialog("widget").height(), 200, 1, "maxHeight");
275+
assert.close(element.dialog("widget").height(), 200, 1, "maxHeight");
276276
element.remove();
277277

278278
element = $("<div></div>").dialog({ maxHeight: 200 }).dialog("option", "maxHeight", 300);
279279
TestHelpers.dialog.drag(element, ".ui-resizable-s", 1000, 1000);
280-
closeEnough(element.dialog("widget").height(), 300, 1, "maxHeight");
280+
assert.close(element.dialog("widget").height(), 300, 1, "maxHeight");
281281
element.remove();
282282
});
283283

284-
test("maxWidth", function() {
284+
test("maxWidth", function( assert ) {
285285
expect(3);
286286

287287
var element = $("<div></div>").dialog({ maxWidth: 200 });
288288
TestHelpers.dialog.drag(element, ".ui-resizable-e", 1000, 1000);
289-
closeEnough(element.dialog("widget").width(), 200, 1, "maxWidth");
289+
assert.close(element.dialog("widget").width(), 200, 1, "maxWidth");
290290
element.remove();
291291

292292
element = $("<div></div>").dialog({ maxWidth: 200 });
293293
TestHelpers.dialog.drag(element, ".ui-resizable-w", -1000, -1000);
294-
closeEnough(element.dialog("widget").width(), 200, 1, "maxWidth");
294+
assert.close(element.dialog("widget").width(), 200, 1, "maxWidth");
295295
element.remove();
296296

297297
element = $("<div></div>").dialog({ maxWidth: 200 }).dialog("option", "maxWidth", 300);
298298
TestHelpers.dialog.drag(element, ".ui-resizable-w", -1000, -1000);
299-
closeEnough(element.dialog("widget").width(), 300, 1, "maxWidth");
299+
assert.close(element.dialog("widget").width(), 300, 1, "maxWidth");
300300
element.remove();
301301
});
302302

303-
test("minHeight", function() {
303+
test("minHeight", function( assert ) {
304304
expect(3);
305305

306306
var element = $("<div></div>").dialog({ minHeight: 10 });
307307
TestHelpers.dialog.drag(element, ".ui-resizable-s", -1000, -1000);
308-
closeEnough(element.dialog("widget").height(), 10, 1, "minHeight");
308+
assert.close(element.dialog("widget").height(), 10, 1, "minHeight");
309309
element.remove();
310310

311311
element = $("<div></div>").dialog({ minHeight: 10 });
312312
TestHelpers.dialog.drag(element, ".ui-resizable-n", 1000, 1000);
313-
closeEnough(element.dialog("widget").height(), 10, 1, "minHeight");
313+
assert.close(element.dialog("widget").height(), 10, 1, "minHeight");
314314
element.remove();
315315

316316
element = $("<div></div>").dialog({ minHeight: 10 }).dialog("option", "minHeight", 30);
317317
TestHelpers.dialog.drag(element, ".ui-resizable-n", 1000, 1000);
318-
closeEnough(element.dialog("widget").height(), 30, 1, "minHeight");
318+
assert.close(element.dialog("widget").height(), 30, 1, "minHeight");
319319
element.remove();
320320
});
321321

322-
test("minWidth", function() {
322+
test("minWidth", function( assert ) {
323323
expect(3);
324324

325325
var element = $("<div></div>").dialog({ minWidth: 10 });
326326
TestHelpers.dialog.drag(element, ".ui-resizable-e", -1000, -1000);
327-
closeEnough(element.dialog("widget").width(), 10, 1, "minWidth");
327+
assert.close(element.dialog("widget").width(), 10, 1, "minWidth");
328328
element.remove();
329329

330330
element = $("<div></div>").dialog({ minWidth: 10 });
331331
TestHelpers.dialog.drag(element, ".ui-resizable-w", 1000, 1000);
332-
closeEnough(element.dialog("widget").width(), 10, 1, "minWidth");
332+
assert.close(element.dialog("widget").width(), 10, 1, "minWidth");
333333
element.remove();
334334

335335
element = $("<div></div>").dialog({ minWidth: 30 }).dialog("option", "minWidth", 30);
336336
TestHelpers.dialog.drag(element, ".ui-resizable-w", 1000, 1000);
337-
closeEnough(element.dialog("widget").width(), 30, 1, "minWidth");
337+
assert.close(element.dialog("widget").width(), 30, 1, "minWidth");
338338
element.remove();
339339
});
340340

341-
test( "position, default center on window", function() {
341+
test( "position, default center on window", function( assert ) {
342342
expect( 2 );
343343

344344
// dialogs alter the window width and height in Firefox
@@ -349,12 +349,12 @@ test( "position, default center on window", function() {
349349
element = $("<div></div>").dialog(),
350350
dialog = element.dialog("widget"),
351351
offset = dialog.offset();
352-
closeEnough( offset.left, Math.round( winWidth / 2 - dialog.outerWidth() / 2 ) + $( window ).scrollLeft(), 1, "dialog left position of center on window on initilization" );
353-
closeEnough( offset.top, Math.round( winHeight / 2 - dialog.outerHeight() / 2 ) + $( window ).scrollTop(), 1, "dialog top position of center on window on initilization" );
352+
assert.close( offset.left, Math.round( winWidth / 2 - dialog.outerWidth() / 2 ) + $( window ).scrollLeft(), 1, "dialog left position of center on window on initilization" );
353+
assert.close( offset.top, Math.round( winHeight / 2 - dialog.outerHeight() / 2 ) + $( window ).scrollTop(), 1, "dialog top position of center on window on initilization" );
354354
element.remove();
355355
});
356356

357-
test( "position, right bottom at right bottom via ui.position args", function() {
357+
test( "position, right bottom at right bottom via ui.position args", function( assert ) {
358358
expect( 2 );
359359

360360
// dialogs alter the window width and height in Firefox
@@ -371,12 +371,12 @@ test( "position, right bottom at right bottom via ui.position args", function()
371371
dialog = element.dialog("widget"),
372372
offset = dialog.offset();
373373

374-
closeEnough( offset.left, winWidth - dialog.outerWidth() + $( window ).scrollLeft(), 1, "dialog left position of right bottom at right bottom on initilization" );
375-
closeEnough( offset.top, winHeight - dialog.outerHeight() + $( window ).scrollTop(), 1, "dialog top position of right bottom at right bottom on initilization" );
374+
assert.close( offset.left, winWidth - dialog.outerWidth() + $( window ).scrollLeft(), 1, "dialog left position of right bottom at right bottom on initilization" );
375+
assert.close( offset.top, winHeight - dialog.outerHeight() + $( window ).scrollTop(), 1, "dialog top position of right bottom at right bottom on initilization" );
376376
element.remove();
377377
});
378378

379-
test( "position, at another element", function() {
379+
test( "position, at another element", function( assert ) {
380380
expect( 4 );
381381
var parent = $("<div></div>").css({
382382
position: "absolute",
@@ -398,8 +398,8 @@ test( "position, at another element", function() {
398398
dialog = element.dialog("widget"),
399399
offset = dialog.offset();
400400

401-
closeEnough( offset.left, 600, 1, "dialog left position at another element on initilization" );
402-
closeEnough( offset.top, 400, 1, "dialog top position at another element on initilization" );
401+
assert.close( offset.left, 600, 1, "dialog left position at another element on initilization" );
402+
assert.close( offset.top, 400, 1, "dialog top position at another element on initilization" );
403403

404404
element.dialog("option", "position", {
405405
my: "left top",
@@ -410,8 +410,8 @@ test( "position, at another element", function() {
410410

411411
offset = dialog.offset();
412412

413-
closeEnough( offset.left, 610, 1, "dialog left position at another element via setting option" );
414-
closeEnough( offset.top, 410, 1, "dialog top position at another element via setting option" );
413+
assert.close( offset.left, 610, 1, "dialog left position at another element via setting option" );
414+
assert.close( offset.top, 410, 1, "dialog top position at another element via setting option" );
415415

416416
element.remove();
417417
parent.remove();
@@ -475,17 +475,17 @@ test( "title", function() {
475475
element.remove();
476476
});
477477

478-
test("width", function() {
478+
test("width", function( assert ) {
479479
expect(3);
480480

481481
var element = $("<div></div>").dialog();
482-
closeEnough(element.dialog("widget").width(), 300, 1, "default width");
482+
assert.close(element.dialog("widget").width(), 300, 1, "default width");
483483
element.remove();
484484

485485
element = $("<div></div>").dialog({width: 437 });
486-
closeEnough(element.dialog("widget").width(), 437, 1, "explicit width");
486+
assert.close(element.dialog("widget").width(), 437, 1, "explicit width");
487487
element.dialog("option", "width", 438);
488-
closeEnough(element.dialog("widget").width(), 438, 1, "explicit width after init");
488+
assert.close(element.dialog("widget").width(), 438, 1, "explicit width after init");
489489
element.remove();
490490
});
491491

tests/unit/draggable/draggable_core.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module( "draggable: core" );
88

9-
test( "element types", function() {
9+
test( "element types", function( assert ) {
1010
var typeNames = (
1111
"p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form" +
1212
",table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr" +
@@ -35,8 +35,8 @@ test( "element types", function() {
3535

3636
// Support: FF, Chrome, and IE9,
3737
// there are some rounding errors in so we can't say equal, we have to settle for close enough
38-
closeEnough( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
39-
closeEnough( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
38+
assert.close( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + "> left" );
39+
assert.close( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + "> top" );
4040
el.draggable("destroy");
4141
el.remove();
4242
});

tests/unit/draggable/draggable_options.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ test( "{ containment: 'parent' }, absolute", function() {
519519
deepEqual( offsetAfter, expected, "compare offset to parent" );
520520
});
521521

522-
test( "containment, account for border", function() {
522+
test( "containment, account for border", function( assert ) {
523523
expect( 2 );
524524

525525
var el = $( "#draggable1" ).appendTo( "#scrollParent" ),
@@ -544,9 +544,9 @@ test( "containment, account for border", function() {
544544
dy: 100
545545
});
546546

547-
closeEnough( el.offset().top, parentBottom - parentBorderBottom - el.height(), 1,
547+
assert.close( el.offset().top, parentBottom - parentBorderBottom - el.height(), 1,
548548
"The draggable should be on top of its parent's bottom border" );
549-
closeEnough( el.offset().left, parentRight - parentBorderRight - el.width(), 1,
549+
assert.close( el.offset().left, parentRight - parentBorderRight - el.width(), 1,
550550
"The draggable should be to the right of its parent's right border" );
551551
});
552552

@@ -1137,7 +1137,7 @@ test( "scroll ignores containers that are overflow: hidden", function() {
11371137
equal( scrollParent.scrollLeft(), 0, "container doesn't scroll horizontally" );
11381138
});
11391139

1140-
test( "#6817: auto scroll goes double distance when dragging", function() {
1140+
test( "#6817: auto scroll goes double distance when dragging", function( assert ) {
11411141
expect( 2 );
11421142

11431143
TestHelpers.draggable.restoreScroll( document );
@@ -1149,8 +1149,8 @@ test( "#6817: auto scroll goes double distance when dragging", function() {
11491149
scroll: true,
11501150
stop: function( e, ui ) {
11511151
equal( ui.offset.top, newY, "offset of item matches pointer position after scroll" );
1152-
// TODO: fix IE8 testswarm IFRAME positioning bug so closeEnough can be turned back to equal
1153-
closeEnough( ui.offset.top - offsetBefore.top, distance, 1, "offset of item only moves expected distance after scroll" );
1152+
// TODO: fix IE8 testswarm IFRAME positioning bug so assert.close can be turned back to equal
1153+
assert.close( ui.offset.top - offsetBefore.top, distance, 1, "offset of item only moves expected distance after scroll" );
11541154
}
11551155
}),
11561156
scrollSensitivity = element.draggable( "option", "scrollSensitivity" ),
@@ -1174,7 +1174,7 @@ test( "#6817: auto scroll goes double distance when dragging", function() {
11741174
TestHelpers.draggable.restoreScroll( document );
11751175
});
11761176

1177-
test( "snap, snapMode, and snapTolerance", function() {
1177+
test( "snap, snapMode, and snapTolerance", function( assert ) {
11781178
expect( 10 );
11791179

11801180
var newX, newY,
@@ -1206,9 +1206,9 @@ test( "snap, snapMode, and snapTolerance", function() {
12061206
moves: 1
12071207
});
12081208

1209-
// TODO: fix IE8 testswarm IFRAME positioning bug so closeEnough can be turned back to equal
1210-
closeEnough( element.offset().left, newX, 1, "doesn't snap outside the snapTolerance" );
1211-
closeEnough( element.offset().top, newY, 1, "doesn't snap outside the snapTolerance" );
1209+
// TODO: fix IE8 testswarm IFRAME positioning bug so assert.close can be turned back to equal
1210+
assert.close( element.offset().left, newX, 1, "doesn't snap outside the snapTolerance" );
1211+
assert.close( element.offset().top, newY, 1, "doesn't snap outside the snapTolerance" );
12121212

12131213
newX += 3;
12141214

@@ -1301,7 +1301,7 @@ test( "snap, snapMode, and snapTolerance", function() {
13011301
deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" );
13021302
});
13031303

1304-
test( "#8459: element can snap to an element that was removed during drag", function() {
1304+
test( "#8459: element can snap to an element that was removed during drag", function( assert ) {
13051305
expect( 2 );
13061306

13071307
var newX, newY,
@@ -1337,9 +1337,9 @@ test( "#8459: element can snap to an element that was removed during drag", func
13371337
ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" );
13381338
ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" );
13391339
} else {
1340-
// TODO: fix IE8 testswarm IFRAME positioning bug so closeEnough can be turned back to equal
1341-
closeEnough( element.offset().left, newX, 1, "doesn't snap to a removed element" );
1342-
closeEnough( element.offset().top, newY, 1, "doesn't snap to a removed element" );
1340+
// TODO: fix IE8 testswarm IFRAME positioning bug so assert.close can be turned back to equal
1341+
assert.close( element.offset().left, newX, 1, "doesn't snap to a removed element" );
1342+
assert.close( element.offset().top, newY, 1, "doesn't snap to a removed element" );
13431343
}
13441344
});
13451345

tests/unit/effects/effects_scale.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module( "effect.scale: Scale" );
33

44
function run( position, v, h, vo, ho ) {
55
var desc = "End Position Correct: " + position + " (" + v + "," + h + ") - origin: (" + vo + "," + ho + ")";
6-
asyncTest( desc, function() {
6+
asyncTest( desc, function( assert ) {
77
expect( 2 );
88
function complete() {
9-
closeEnough( parseInt( test.css( h ), 10 ), target[ h ], 1, "Horizontal Position Correct " + desc );
10-
closeEnough( parseInt( test.css( v ), 10 ), target[ v ], 1, "Vertical Position Correct " + desc );
9+
assert.close( parseInt( test.css( h ), 10 ), target[ h ], 1, "Horizontal Position Correct " + desc );
10+
assert.close( parseInt( test.css( v ), 10 ), target[ v ], 1, "Vertical Position Correct " + desc );
1111
start();
1212
}
1313
var test = $( ".testScale" ),

tests/unit/testsuite.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ TestHelpers.forceScrollableWindow = function( appendTo ) {
224224
}).appendTo( appendTo || "#qunit-fixture" );
225225
};
226226

227-
// Taken from https://github.com/jquery/qunit/tree/master/addons/close-enough
228-
window.closeEnough = function( actual, expected, maxDifference, message ) {
227+
// TODO: switch to qunit-assert-close plugin
228+
QUnit.assert.close = function( actual, expected, maxDifference, message ) {
229229
var passes = ( actual === expected ) || Math.abs( actual - expected ) <= maxDifference;
230230
QUnit.push( passes, actual, expected, message );
231231
};

0 commit comments

Comments
 (0)