Browse files

Extract duplication into intention-revealing helper method.

  • Loading branch information...
1 parent c3ed777 commit a1e729028d91a2c67f8206803a8c70a21f70ce90 @floehopper floehopper committed Jan 27, 2012
Showing with 22 additions and 18 deletions.
  1. +22 −18 test/javascripts/multiple_file_upload_test.js
View
40 test/javascripts/multiple_file_upload_test.js
@@ -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() {
this.first_input.change();
equal(this.fieldset.children(".file_upload").length, 2);
@@ -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() {
- this.first_input.change();
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
+ fireChangeEventOnLastFileInputOf(this.fieldset);
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);
});
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");
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");
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() {
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
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.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];
equal(latest_input.id, "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() {
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("label:contains('File'):last");
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");
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() {
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
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.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];
equal(latest_input.id, "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() {
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
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.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];
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]");
});
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=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=text]:last");
equal(latest_input.val(), "");
});
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");
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
var latest_input = this.fieldset.find("input[type=hidden]:last");
equal(latest_input.val(), "");
});
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>');
$("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:last").text(), "");
});
@@ -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() {
- 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(".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() {
- this.fieldset.find("input[type=file]:last").change();
+ fireChangeEventOnLastFileInputOf(this.fieldset);
equal(this.fieldset.find("input[type=file]").length, 2);
equal(this.fieldset.find(".field_with_errors input[type=file]").length, 1);
});

0 comments on commit a1e7290

Please sign in to comment.