Skip to content

Commit

Permalink
Molly - fixing null pointer of item id
Browse files Browse the repository at this point in the history
  • Loading branch information
Molly committed Jul 25, 2012
1 parent 0d41628 commit 1eeec3c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/freemarker/order/new.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}'/>

<form action="<@spring.url '${create_url}'/>" id="newOrderForm" class="form_background" method="POST">
<h4 id="form_title">Place Your Order</h4>
Expand Down
26 changes: 15 additions & 11 deletions src/main/webapp/static/js/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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() {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/OrderFunctionalTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
20 changes: 20 additions & 0 deletions src/test/javascript/spec/order/order.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $("<span>3.00</span>");
var tax_field = $("<span>0.10</span>");

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');
});
});

0 comments on commit 1eeec3c

Please sign in to comment.