Skip to content
Permalink
Browse files

Tests: Convert closeEnough() to a proper QUnit assertion

Should eventually replace with qunit-assert-close.

Ref #10119
Ref gh-1528
  • Loading branch information...
scottgonzalez committed Mar 31, 2015
1 parent 7c896dd commit 2e1c72fb59b3e41da15ebcd83640dbdf13a90d27
@@ -20,7 +20,6 @@

"globals": {
"asyncTest": false,
"closeEnough": false,
"deepEqual": false,
"define": false,
"domEqual": false,
@@ -40,6 +40,12 @@ QUnit.reset = ( function( reset ) {
};
} )( QUnit.reset );

// TODO: switch to qunit-assert-close plugin
QUnit.assert.close = function( actual, expected, maxDifference, message ) {
var passes = ( actual === expected ) || Math.abs( actual - expected ) <= maxDifference;
QUnit.push( passes, actual, expected, message );
};

return QUnit;

} );
@@ -262,83 +262,83 @@ test("height", function() {
element.remove();
});

test("maxHeight", function() {
test("maxHeight", function( assert ) {
expect(3);

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

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

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

test("maxWidth", function() {
test("maxWidth", function( assert ) {
expect(3);

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

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

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

test("minHeight", function() {
test("minHeight", function( assert ) {
expect(3);

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

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

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

test("minWidth", function() {
test("minWidth", function( assert ) {
expect(3);

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

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

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

test( "position, default center on window", function() {
test( "position, default center on window", function( assert ) {
expect( 2 );

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

test( "position, right bottom at right bottom via ui.position args", function() {
test( "position, right bottom at right bottom via ui.position args", function( assert ) {
expect( 2 );

// 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()
dialog = element.dialog("widget"),
offset = dialog.offset();

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

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

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

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

offset = dialog.offset();

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

element.remove();
parent.remove();
@@ -475,17 +475,17 @@ test( "title", function() {
element.remove();
});

test("width", function() {
test("width", function( assert ) {
expect(3);

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

element = $("<div></div>").dialog({width: 437 });
closeEnough(element.dialog("widget").width(), 437, 1, "explicit width");
assert.close(element.dialog("widget").width(), 437, 1, "explicit width");
element.dialog("option", "width", 438);
closeEnough(element.dialog("widget").width(), 438, 1, "explicit width after init");
assert.close(element.dialog("widget").width(), 438, 1, "explicit width after init");
element.remove();
});

@@ -6,7 +6,7 @@

module( "draggable: core" );

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

// Support: FF, Chrome, and IE9,
// there are some rounding errors in so we can't say equal, we have to settle for close enough
closeEnough( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
closeEnough( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + ">" );
assert.close( offsetBefore.left, offsetAfter.left - 50, 1, "dragged[50, 50] " + "<" + typeName + "> left" );
assert.close( offsetBefore.top, offsetAfter.top - 50, 1, "dragged[50, 50] " + "<" + typeName + "> top" );
el.draggable("destroy");
el.remove();
});
@@ -519,7 +519,7 @@ test( "{ containment: 'parent' }, absolute", function() {
deepEqual( offsetAfter, expected, "compare offset to parent" );
});

test( "containment, account for border", function() {
test( "containment, account for border", function( assert ) {
expect( 2 );

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

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

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

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

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

test( "snap, snapMode, and snapTolerance", function() {
test( "snap, snapMode, and snapTolerance", function( assert ) {
expect( 10 );

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

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

newX += 3;

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

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

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

@@ -3,11 +3,11 @@ module( "effect.scale: Scale" );

function run( position, v, h, vo, ho ) {
var desc = "End Position Correct: " + position + " (" + v + "," + h + ") - origin: (" + vo + "," + ho + ")";
asyncTest( desc, function() {
asyncTest( desc, function( assert ) {
expect( 2 );
function complete() {
closeEnough( parseInt( test.css( h ), 10 ), target[ h ], 1, "Horizontal Position Correct " + desc );
closeEnough( parseInt( test.css( v ), 10 ), target[ v ], 1, "Vertical Position Correct " + desc );
assert.close( parseInt( test.css( h ), 10 ), target[ h ], 1, "Horizontal Position Correct " + desc );
assert.close( parseInt( test.css( v ), 10 ), target[ v ], 1, "Vertical Position Correct " + desc );
start();
}
var test = $( ".testScale" ),
@@ -224,8 +224,8 @@ TestHelpers.forceScrollableWindow = function( appendTo ) {
}).appendTo( appendTo || "#qunit-fixture" );
};

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

0 comments on commit 2e1c72f

Please sign in to comment.
You can’t perform that action at this time.