Skip to content

Commit

Permalink
Extract duplication into intention-revealing helper method.
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Jan 27, 2012
1 parent c3ed777 commit a1e7290
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions test/javascripts/multiple_file_upload_test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module("Uploading multiple files", {
} }
}); });


var fireChangeEventOnLastFileInputOf = function(fieldset) {
fieldset.find("input[type=file]:last").change();
}

test("should add a new file input when a file is selected", function() { test("should add a new file input when a file is selected", function() {
this.first_input.change(); this.first_input.change();
equal(this.fieldset.children(".file_upload").length, 2); equal(this.fieldset.children(".file_upload").length, 2);
Expand All @@ -28,88 +32,88 @@ test("should not add a new file input when a selected file is changed", function
}); });


test("should continue adding new inputs as new files are selected", function() { test("should continue adding new inputs as new files are selected", function() {
this.first_input.change(); fireChangeEventOnLastFileInputOf(this.fieldset);
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
equal(this.fieldset.children(".file_upload").length, 3); equal(this.fieldset.children(".file_upload").length, 3);


this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
equal(this.fieldset.children(".file_upload").length, 4); equal(this.fieldset.children(".file_upload").length, 4);
}); });


test("should increment the referenced ID of the title label for each new set of inputs added", function() { test("should increment the referenced ID of the title label for each new set of inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("label:contains('Title'):last"); var latest_input = this.fieldset.find("label:contains('Title'):last");
equal(latest_input.attr('for'), "document_document_attachments_attributes_1_attachment_attributes_title"); equal(latest_input.attr('for'), "document_document_attachments_attributes_1_attachment_attributes_title");


this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
latest_input = this.fieldset.find("label:contains('Title'):last"); latest_input = this.fieldset.find("label:contains('Title'):last");
equal(latest_input.attr('for'), "document_document_attachments_attributes_2_attachment_attributes_title"); equal(latest_input.attr('for'), "document_document_attachments_attributes_2_attachment_attributes_title");
}); });


test("should increment the ID and name of the text input for each set of new inputs added", function() { test("should increment the ID and name of the text input for each set of new inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=text]:last")[0]; var latest_input = this.fieldset.find("input[type=text]:last")[0];
equal(latest_input.id, "document_document_attachments_attributes_1_attachment_attributes_title"); equal(latest_input.id, "document_document_attachments_attributes_1_attachment_attributes_title");
equal(latest_input.name, "document[document_attachments_attributes][1][attachment_attributes][title]"); equal(latest_input.name, "document[document_attachments_attributes][1][attachment_attributes][title]");


this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
latest_input = this.fieldset.find("input[type=text]:last")[0]; latest_input = this.fieldset.find("input[type=text]:last")[0];
equal(latest_input.id, "document_document_attachments_attributes_2_attachment_attributes_title"); equal(latest_input.id, "document_document_attachments_attributes_2_attachment_attributes_title");
equal(latest_input.name, "document[document_attachments_attributes][2][attachment_attributes][title]"); equal(latest_input.name, "document[document_attachments_attributes][2][attachment_attributes][title]");
}); });


test("should increment the referenced ID of the file label for each set of new inputs added", function() { test("should increment the referenced ID of the file label for each set of new inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("label:contains('File'):last"); var latest_input = this.fieldset.find("label:contains('File'):last");
equal(latest_input.attr('for'), "document_document_attachments_attributes_1_attachment_attributes_file"); equal(latest_input.attr('for'), "document_document_attachments_attributes_1_attachment_attributes_file");


this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
latest_input = this.fieldset.find("label:contains('File'):last"); latest_input = this.fieldset.find("label:contains('File'):last");
equal(latest_input.attr('for'), "document_document_attachments_attributes_2_attachment_attributes_file"); equal(latest_input.attr('for'), "document_document_attachments_attributes_2_attachment_attributes_file");
}); });


test("should increment the ID and name of the file input for each set of new inputs added", function() { test("should increment the ID and name of the file input for each set of new inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=file]:last")[0]; var latest_input = this.fieldset.find("input[type=file]:last")[0];
equal(latest_input.id, "document_document_attachments_attributes_1_attachment_attributes_file"); equal(latest_input.id, "document_document_attachments_attributes_1_attachment_attributes_file");
equal(latest_input.name, "document[document_attachments_attributes][1][attachment_attributes][file]"); equal(latest_input.name, "document[document_attachments_attributes][1][attachment_attributes][file]");


this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
latest_input = this.fieldset.find("input[type=file]:last")[0]; latest_input = this.fieldset.find("input[type=file]:last")[0];
equal(latest_input.id, "document_document_attachments_attributes_2_attachment_attributes_file"); equal(latest_input.id, "document_document_attachments_attributes_2_attachment_attributes_file");
equal(latest_input.name, "document[document_attachments_attributes][2][attachment_attributes][file]"); equal(latest_input.name, "document[document_attachments_attributes][2][attachment_attributes][file]");
}); });


test("should increment the ID and name of the hidden cache input for each set of new inputs added", function() { test("should increment the ID and name of the hidden cache input for each set of new inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=hidden]:last")[0]; var latest_input = this.fieldset.find("input[type=hidden]:last")[0];
equal(latest_input.id, "document_document_attachments_attributes_1_attachment_attributes_file_cache"); equal(latest_input.id, "document_document_attachments_attributes_1_attachment_attributes_file_cache");
equal(latest_input.name, "document[document_attachments_attributes][1][attachment_attributes][file_cache]"); equal(latest_input.name, "document[document_attachments_attributes][1][attachment_attributes][file_cache]");


this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
latest_input = this.fieldset.find("input[type=hidden]:last")[0]; latest_input = this.fieldset.find("input[type=hidden]:last")[0];
equal(latest_input.id, "document_document_attachments_attributes_2_attachment_attributes_file_cache"); equal(latest_input.id, "document_document_attachments_attributes_2_attachment_attributes_file_cache");
equal(latest_input.name, "document[document_attachments_attributes][2][attachment_attributes][file_cache]"); equal(latest_input.name, "document[document_attachments_attributes][2][attachment_attributes][file_cache]");
}); });


test("should make the value of the text input blank for each set of new inputs added", function() { test("should make the value of the text input blank for each set of new inputs added", function() {
this.fieldset.find("input[type=text]:last").val("not-blank"); this.fieldset.find("input[type=text]:last").val("not-blank");
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=text]:last"); var latest_input = this.fieldset.find("input[type=text]:last");
equal(latest_input.val(), ""); equal(latest_input.val(), "");
}); });


test("should set the value of the hidden cache input to blank for each new input added", function() { test("should set the value of the hidden cache input to blank for each new input added", function() {
$("input[type=hidden]:last").val("not-blank"); $("input[type=hidden]:last").val("not-blank");
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=hidden]:last"); var latest_input = this.fieldset.find("input[type=hidden]:last");
equal(latest_input.val(), ""); equal(latest_input.val(), "");
}); });


test("should set the text of the already_uploaded element to blank for each new input added", function() { test("should set the text of the already_uploaded element to blank for each new input added", function() {
already_uploaded = $('<span class="already_uploaded">some-file.pdf already uploaded</span>'); already_uploaded = $('<span class="already_uploaded">some-file.pdf already uploaded</span>');
$("input[type=file]:last").after(already_uploaded); $("input[type=file]:last").after(already_uploaded);
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
equal(this.fieldset.find(".already_uploaded").length, 2); equal(this.fieldset.find(".already_uploaded").length, 2);
equal(this.fieldset.find(".already_uploaded:last").text(), ""); equal(this.fieldset.find(".already_uploaded:last").text(), "");
}); });
Expand Down Expand Up @@ -137,13 +141,13 @@ module("Uploading multiple files after file field validation error", {
}); });


test("should copy the file label without error wrapper for each set of new inputs added", function() { test("should copy the file label without error wrapper for each set of new inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
equal(this.fieldset.find("label:contains('File'):last").parent().hasClass("field_with_errors"), false); equal(this.fieldset.find("label:contains('File'):last").parent().hasClass("field_with_errors"), false);
equal(this.fieldset.find(".field_with_errors label:contains('File')").length, 1); equal(this.fieldset.find(".field_with_errors label:contains('File')").length, 1);
}); });


test("should copy the file input without error wrapper for each set of new inputs added", function() { test("should copy the file input without error wrapper for each set of new inputs added", function() {
this.fieldset.find("input[type=file]:last").change(); fireChangeEventOnLastFileInputOf(this.fieldset);
equal(this.fieldset.find("input[type=file]").length, 2); equal(this.fieldset.find("input[type=file]").length, 2);
equal(this.fieldset.find(".field_with_errors input[type=file]").length, 1); equal(this.fieldset.find(".field_with_errors input[type=file]").length, 1);
}); });

0 comments on commit a1e7290

Please sign in to comment.