Permalink
Browse files

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
  • Loading branch information...
1 parent 23c9895 commit 3dbe096ddf6c78e37732767ee984f6fc30b43fdc @quooston quooston committed Feb 27, 2013
Showing with 50 additions and 67 deletions.
  1. +2 −0 SpecRunner.html
  2. +0 −10 cart.min.js
  3. +3 −4 example.html
  4. +43 −51 spec/cart_spec.js
  5. +2 −2 src/cart.js
View
@@ -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

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -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
@@ -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
@@ -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;
};

0 comments on commit 3dbe096

Please sign in to comment.