Skip to content

Commit 22ff8e9

Browse files
committed
Added manipulation tests for setter function args.
1 parent 8fa9e9d commit 22ff8e9

File tree

1 file changed

+175
-3
lines changed

1 file changed

+175
-3
lines changed

test/unit/manipulation.js

Lines changed: 175 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,68 @@ test("append(String|Element|Array<Element>|jQuery)", function() {
288288

289289
test("append(Function)", function() {
290290
testAppend(functionReturningObj);
291-
})
291+
});
292+
293+
test("append(Function) with incoming value", function() {
294+
expect(12);
295+
296+
var defaultText = 'Try them out:', old = jQuery("#first").html();
297+
298+
var result = jQuery('#first').append(function(i, val){
299+
equals( val, old, "Make sure the incoming value is correct." );
300+
return '<b>buga</b>';
301+
});
302+
equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
303+
304+
var select = jQuery('#select3');
305+
old = select.html();
306+
307+
equals( select.append(function(i, val){
308+
equals( val, old, "Make sure the incoming value is correct." );
309+
return '<option value="appendTest">Append Test</option>';
310+
}).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
311+
312+
reset();
313+
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
314+
old = jQuery("#sap").html();
315+
316+
jQuery('#sap').append(function(i, val){
317+
equals( val, old, "Make sure the incoming value is correct." );
318+
return document.getElementById('first');
319+
});
320+
equals( expected, jQuery('#sap').text(), "Check for appending of element" );
321+
322+
reset();
323+
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
324+
old = jQuery("#sap").html();
325+
326+
jQuery('#sap').append(function(i, val){
327+
equals( val, old, "Make sure the incoming value is correct." );
328+
return [document.getElementById('first'), document.getElementById('yahoo')];
329+
});
330+
equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
331+
332+
reset();
333+
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
334+
old = jQuery("#sap").html();
335+
336+
jQuery('#sap').append(function(i, val){
337+
equals( val, old, "Make sure the incoming value is correct." );
338+
return jQuery("#first, #yahoo");
339+
});
340+
equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
341+
342+
reset();
343+
old = jQuery("#sap").html();
344+
345+
jQuery("#sap").append(function(i, val){
346+
equals( val, old, "Make sure the incoming value is correct." );
347+
return 5;
348+
});
349+
ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
350+
351+
reset();
352+
});
292353

293354
test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
294355
expect(12);
@@ -362,15 +423,66 @@ var testPrepend = function(val) {
362423
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
363424
jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
364425
equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
365-
}
426+
};
366427

367428
test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
368429
testPrepend(bareObj);
369430
});
370431

371432
test("prepend(Function)", function() {
372433
testPrepend(functionReturningObj);
373-
})
434+
});
435+
436+
test("prepend(Function) with incoming value", function() {
437+
expect(10);
438+
439+
var defaultText = 'Try them out:', old = jQuery('#first').html();
440+
var result = jQuery('#first').prepend(function(i, val) {
441+
equals( val, old, "Make sure the incoming value is correct." );
442+
return '<b>buga</b>';
443+
});
444+
equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
445+
446+
old = jQuery("#select3").html();
447+
448+
equals( jQuery('#select3').prepend(function(i, val) {
449+
equals( val, old, "Make sure the incoming value is correct." );
450+
return '<option value="prependTest">Prepend Test</option>';
451+
}).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
452+
453+
reset();
454+
var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
455+
old = jQuery('#sap').html();
456+
457+
jQuery('#sap').prepend(function(i, val) {
458+
equals( val, old, "Make sure the incoming value is correct." );
459+
return document.getElementById('first');
460+
});
461+
462+
equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
463+
464+
reset();
465+
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
466+
old = jQuery('#sap').html();
467+
468+
jQuery('#sap').prepend(function(i, val) {
469+
equals( val, old, "Make sure the incoming value is correct." );
470+
return [document.getElementById('first'), document.getElementById('yahoo')];
471+
});
472+
473+
equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
474+
475+
reset();
476+
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
477+
old = jQuery('#sap').html();
478+
479+
jQuery('#sap').prepend(function(i, val) {
480+
equals( val, old, "Make sure the incoming value is correct." );
481+
return jQuery("#first, #yahoo");
482+
});
483+
484+
equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
485+
});
374486

375487
test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
376488
expect(6);
@@ -714,6 +826,66 @@ test("html(Function)", function() {
714826
testHtml(functionReturningObj);
715827
});
716828

829+
test("html(Function) with incoming value", function() {
830+
expect(20);
831+
832+
var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
833+
834+
div.html(function(i, val) {
835+
equals( val, old[i], "Make sure the incoming value is correct." );
836+
return "<b>test</b>";
837+
});
838+
839+
var pass = true;
840+
div.each(function(){
841+
if ( this.childNodes.length !== 1 ) {
842+
pass = false;
843+
}
844+
})
845+
ok( pass, "Set HTML" );
846+
847+
reset();
848+
// using contents will get comments regular, text, and comment nodes
849+
var j = jQuery("#nonnodes").contents();
850+
old = j.map(function(){ return jQuery(this).html(); });
851+
852+
j.html(function(i, val) {
853+
equals( val, old[i], "Make sure the incoming value is correct." );
854+
return "<b>bold</b>";
855+
});
856+
857+
j.find('b').removeData();
858+
equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
859+
860+
var $div = jQuery('<div />');
861+
862+
equals( $div.html(function(i, val) {
863+
equals( val, "", "Make sure the incoming value is correct." );
864+
return 5;
865+
}).html(), '5', 'Setting a number as html' );
866+
867+
equals( $div.html(function(i, val) {
868+
equals( val, "5", "Make sure the incoming value is correct." );
869+
return 0;
870+
}).html(), '0', 'Setting a zero as html' );
871+
872+
var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
873+
equals( $div2.html(function(i, val) {
874+
equals( val, "", "Make sure the incoming value is correct." );
875+
return insert;
876+
}).html(), insert, "Verify escaped insertion." );
877+
878+
equals( $div2.html(function(i, val) {
879+
equals( val, insert, "Make sure the incoming value is correct." );
880+
return "x" + insert;
881+
}).html(), "x" + insert, "Verify escaped insertion." );
882+
883+
equals( $div2.html(function(i, val) {
884+
equals( val, "x" + insert, "Make sure the incoming value is correct." );
885+
return " " + insert;
886+
}).html(), " " + insert, "Verify escaped insertion." );
887+
});
888+
717889
var testRemove = function(method) {
718890
expect(9);
719891

0 commit comments

Comments
 (0)