Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

CartItem.lineTotal bug and failing tests #3

Merged
merged 1 commit into from

2 participants

@quooston

Little things but worth having sorted I think :)

  • the discount in lineTotal() was being applied incorrectly:

    return (cartItem.price - cartItem.discount()) * cartItem.quantity();
    

    The discount above is being calculated for the total of all line items,
    therefore applying it to each item independently is incorrect.

    Instead, the discount should be applied to the item subtotal which is the
    total for all line items:

    return cartItem.subtotal() - cartItem.discount();
    
  • some of the tests were failing

  • added new tests for discount fix
  • removed the cart.min.js file which contained 98479857345 instances of the scripts :) Instead just referenced the scripts directly.
@quooston quooston Fixed lineTotal bug and failing tests
 - the discount in lineTotal() was being applied incorrectly
 - some of the tests were failing
 - added new tests for discount fix
3dbe096
@robconery robconery merged commit ef92f0a into robconery:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 27, 2013
  1. @quooston

    Fixed lineTotal bug and failing tests

    quooston authored
     - the discount in lineTotal() was being applied incorrectly
     - some of the tests were failing
     - added new tests for discount fix
This page is out of date. Refresh to see the latest.
View
2  SpecRunner.html
@@ -11,6 +11,8 @@
<!-- include source files here... -->
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js"></script>
+ <script type="text/javascript" src="src/customBindings.js"></script>
+ <script type="text/javascript" src="src/extensions.js"></script>
<script type="text/javascript" src="src/cart.js"></script>
<!-- include spec files here... -->
View
10 cart.min.js
0 additions, 10 deletions not shown
View
7 example.html
@@ -1,6 +1,3 @@
-
-
-
<button data-bind='clickWithData:addItem' data-description='Tekpub Yearly Subscription' data-price='149.0' data-sku='yearly'>Buy</button>
<button data-bind='clickWithData:addItem' data-description='Tekpub Forever Subscription' data-price='10000.0' data-sku='forever'>Buy</button>
@@ -59,7 +56,9 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js"></script>
-<script src="cart.min.js"></script>
+<script src="src/customBindings.js"></script>
+<script src="src/extensions.js"></script>
+<script src="src/cart.js"></script>
<script>
$(function(){
View
94 spec/cart_spec.js
@@ -3,9 +3,9 @@ describe("Shopping Cart", function() {
cart = {};
var product = {price : 5.00,sku : "monkey",description : "test product"};
var product2 = {price : 5.00,sku : "chunky",description : "test product"};
+ var discountableProduct = {price : 125.00, sku : "expensive", description : "expensive product"};
beforeEach(function() {
- //cart = new Tekpub.Cart();
cart = new Tekpub.Cart();
});
@@ -24,8 +24,9 @@ describe("Shopping Cart", function() {
});
it("stores a sku", function(){
- expect(cart.contains("monkey")).toBeTruthy();
+ expect(cart.find("monkey")).toBeTruthy();
});
+
it("has a count", function(){
expect(cart.itemCount()).toEqual(1);
});
@@ -45,7 +46,7 @@ describe("Shopping Cart", function() {
it("increments quantity with duplicate", function() {
cart.addItem(product);
item = cart.find("monkey");
- expect(item.quantity).toEqual(2);
+ expect(item.quantity()).toEqual(2);
});
it("empties the cart", function(){
cart.empty();
@@ -104,52 +105,43 @@ describe("Shopping Cart", function() {
});
});
- // it("should be able to play a Song", function() {
- // player.play(song);
- // expect(player.currentlyPlayingSong).toEqual(song);
-
- // //demonstrates use of custom matcher
- // expect(player).toBePlaying(song);
- // });
-
- // describe("when song has been paused", function() {
- // beforeEach(function() {
- // player.play(song);
- // player.pause();
- // });
-
- // it("should indicate that the song is currently paused", function() {
- // expect(player.isPlaying).toBeFalsy();
-
- // // demonstrates use of 'not' with a custom matcher
- // expect(player).not.toBePlaying(song);
- // });
-
- // it("should be possible to resume", function() {
- // player.resume();
- // expect(player.isPlaying).toBeTruthy();
- // expect(player.currentlyPlayingSong).toEqual(song);
- // });
- // });
-
- // // demonstrates use of spies to intercept and test method calls
- // it("tells the current song if the user has made it a favorite", function() {
- // spyOn(song, 'persistFavoriteStatus');
-
- // player.play(song);
- // player.makeFavorite();
-
- // expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
- // });
-
- // //demonstrates use of expected exceptions
- // describe("#resume", function() {
- // it("should throw an exception if song is already playing", function() {
- // player.play(song);
-
- // expect(function() {
- // player.resume();
- // }).toThrow("song is already playing");
- // });
- // });
+
+ describe("discounts", function(){
+ beforeEach(function() {
+ //nothing...
+ });
+
+ afterEach(function(){
+ cart.empty();
+ });
+
+ it("should not apply discount for less than 5 items in cart", function() {
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+
+ expect(cart.rowCount()).toEqual(1);
+
+ var cartItem = cart.items()[0];
+ expect(cartItem.quantity()).toEqual(4);
+ expect(cartItem.discount()).toEqual(0);
+ });
+
+ it("should apply discount for 5 or more items in cart", function() {
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+ cart.addItem(discountableProduct);
+
+ expect(cart.rowCount()).toEqual(1);
+
+ var cartItem = cart.items()[0];
+ expect(cartItem.quantity()).toEqual(5);
+ expect(cartItem.discount()).toEqual(125.00);
+ });
+
+ });
+
});
View
4 src/cart.js
@@ -23,7 +23,7 @@ Tekpub.CartItem = function(options,callback){
});
cartItem.lineTotal = ko.computed(function() {
- return (cartItem.price - cartItem.discount()) * cartItem.quantity();
+ return cartItem.subtotal() - cartItem.discount();
});
if(callback){
@@ -83,7 +83,7 @@ Tekpub.Cart = function(){
self.itemCount = function() {
var itemCount = 0;
ko.utils.arrayForEach(self.items(),function(item){
- itemCount += item.quantity;
+ itemCount += item.quantity();
});
return itemCount;
};
Something went wrong with that request. Please try again.