From 1eeec3c0a88b346e023960404cc41329266e8770 Mon Sep 17 00:00:00 2001 From: Molly Date: Wed, 25 Jul 2012 16:39:58 -0500 Subject: [PATCH] Molly - fixing null pointer of item id --- .../webapp/WEB-INF/freemarker/order/new.ftl | 2 +- src/main/webapp/static/js/order.js | 26 ++++++++++-------- src/test/java/OrderFunctionalTest.java | 27 +++++++++++++++++++ src/test/javascript/spec/order/order.spec.js | 20 ++++++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 src/test/java/OrderFunctionalTest.java diff --git a/src/main/webapp/WEB-INF/freemarker/order/new.ftl b/src/main/webapp/WEB-INF/freemarker/order/new.ftl index 4813882..660eeff 100644 --- a/src/main/webapp/WEB-INF/freemarker/order/new.ftl +++ b/src/main/webapp/WEB-INF/freemarker/order/new.ftl @@ -3,7 +3,7 @@ <@layout.template 'Add Order' > <#assign first_item=items[0]/> - <#assign create_url='/order/create?itemId='/> + <#assign create_url='/order/create?itemId=${first_item.id}'/>

Place Your Order

diff --git a/src/main/webapp/static/js/order.js b/src/main/webapp/static/js/order.js index 51d1c7d..3c91007 100644 --- a/src/main/webapp/static/js/order.js +++ b/src/main/webapp/static/js/order.js @@ -17,17 +17,23 @@ var OrderForm = function() { self.get_formatted_field = function(field) { return parseFloat(field.text()).toFixed(2); - } + }; + + self.get_action = function() { + return "/twuFunctionalTesting/order/create?itemId=" + self.item_id; + }; - self.update = function(new_price, new_tax) { + self.update = function(new_price, new_tax, new_item_id) { self.price.text(new_price); self.tax.text(new_tax); - } + self.item_id = new_item_id; + }; - init = function(price_field, tax_field, total_field) { + init = function(price_field, tax_field, total_field, item_selected) { self.price = price_field; self.tax = tax_field; self.total = total_field; + self.item_id = item_selected; self.hidden_total = $("#hidden_current_total"); return self; @@ -37,17 +43,15 @@ var OrderForm = function() { }(); $(function(){ - var order_form = OrderForm.init($("#current_price"), $("#current_tax"), $("#current_total")); + var selected_item_index = $("#items option:selected").val(); + var order_form = OrderForm.init($("#current_price"), $("#current_tax"), $("#current_total"), selected_item_index); order_form.calculate_total(); $("#submitButton").click(function(event) { validator = OrderFormValidator.init($("#name_field"), $("#email_field")); if(validator.validate()) { - var new_form = $("#newOrderForm"); - var action = new_form.attr("action"); - var selected_item_index = $("#items option:selected").val(); - - new_form.attr("action", (action + selected_item_index)); + var new_action = order_form.get_action(); + $("#newOrderForm").attr("action", new_action); } else { event.preventDefault(); } @@ -62,7 +66,7 @@ $(function(){ data: {item_id : id}, success: function(data) { data_as_json = JSON.parse(data); - order_form.update(data_as_json["price"], data_as_json["tax"]); + order_form.update(data_as_json["price"], data_as_json["tax"], id); order_form.calculate_total(); }, error: function() { diff --git a/src/test/java/OrderFunctionalTest.java b/src/test/java/OrderFunctionalTest.java new file mode 100644 index 0000000..9e1992b --- /dev/null +++ b/src/test/java/OrderFunctionalTest.java @@ -0,0 +1,27 @@ +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.firefox.FirefoxDriver; + +public class OrderFunctionalTest { + + @Test + public void shouldBeAbleToSubmitOrder() { + WebDriver driver = new FirefoxDriver(); + + driver.get("http://localhost:8080/twuFunctionalTesting/order/new"); + + WebElement nameElement = driver.findElement(By.id("name_field")); + nameElement.sendKeys("awesome"); + + WebElement emailElement = driver.findElement(By.id("email_field")); + emailElement.sendKeys("awesome@awesome.com"); + + WebElement submitButtonElement = driver.findElement(By.id("submitButton")); + submitButtonElement.submit(); +// submitButtonElement.click(); + + driver.close(); + } +} diff --git a/src/test/javascript/spec/order/order.spec.js b/src/test/javascript/spec/order/order.spec.js index 49bd8a4..5018172 100644 --- a/src/test/javascript/spec/order/order.spec.js +++ b/src/test/javascript/spec/order/order.spec.js @@ -61,4 +61,24 @@ describe("OrderForm", function() { expect(order_form.price.text()).toBe("75.00"); expect(order_form.tax.text()).toBe("0.10"); }); + + it("should change the selected item id", function() { + var order_form = OrderForm.init(null, null, null, 3); + + var form_action = order_form.get_action(); + + expect(form_action).toBe('/twuFunctionalTesting/order/create?itemId=3'); + }); + + it("should update action", function() { + var price_field = $("3.00"); + var tax_field = $("0.10"); + + var order_form = OrderForm.init(price_field, tax_field, null, 1); + + order_form.update("75.00", "0.10", 7); + var form_action = order_form.get_action(); + + expect(form_action).toBe('/twuFunctionalTesting/order/create?itemId=7'); + }); }); \ No newline at end of file