Skip to content

Instantly share code, notes, and snippets.

@chrisjpowers
Created November 19, 2011 15:15
Show Gist options
  • Save chrisjpowers/1378942 to your computer and use it in GitHub Desktop.
Save chrisjpowers/1378942 to your computer and use it in GitHub Desktop.
Example of tangled code for refactoring
describe("pressing the done button", function() {
var container, input, button, welcome;
beforeEach(function() {
container = $("div");
input = $("<input>", {"class": "name"});
button = $("<button>", {"class": "done"});
welcome = $("<div>", {"id": "welcome"});
container.append(input, button, welcome);
$("body").append(container);
});
afterEach(function() {
container.remove();
});
it("displays welcome with first name from input", function() {
input.val("Dr. John Doe");
button.click();
expect(welcome.text()).toEqual("Welcome, John!");
});
});
$("button.done").live("click", function() {
var name = $("input.name").val();
name = name.split(" ");
if(name[0] == "Mr." ||
name[0] == "Mrs." ||
name[0] == "Dr.") {
name.shift();
}
$("#welcome").text("Welcome, " + name[0] + "!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment