Code
var cartModule = (function() { var cart = []; var cart_proxy = new Proxy(cart, { set: function(target, property, value) { ... target[property] = value return true } } return { toggleItem: function() { if (value) { cart_proxy.push(new Item(item_name)); } } getItems: function() { return cart.map( object => object.name ); } } }) Spec
describe("when toggleitem is called", function() { beforeEach(function() { cartModule.toggleItem("ladder", true) }) it ('adds item', function() { expect(cartModule.getItems()).toEqual(["ladder"]); }) }) Spec fails if the code says cart_proxy.push, but if code says cart.push spec passes. As well in console, I can confirm that cart_proxy.push is working appropriately. Looks like what's failing is something about the use of the Proxy
2 Answers
Answers 1
For me it seems that, you cannot make cart_proxy properly, and that's the main reason of your test failure. Also, your test may suffers from few issues.
You have syntax error. The correct one is below:
var cartModule = (function() { var cart = []; var cart_proxy = new Proxy(cart, { set: function(target, property, value) { target[property] = value return true } }) return { toggleItem: function() { if (value) { cart_proxy.push(new Item(item_name)); } }, getItems: function() { return cart.map( object => object.name ); } } }) it means that, you forgot to close the parenthesis in the line new Proxy(, and secondly, you lost a , in the return object. Your test works, with cart.push, because your cart is an array and you can inject one more element into it, but because of the stated problem cart_proxy cannot be constructed.
Also, i have some concerns about your beforeEach:
beforeEach(function() { cartModule.toggleItem("ladder", true) }) I think the toggleItem is a function in the cartModule which does not receive any parameter, but here it has received 2 arguments.
In conclusion, I suggest you to clean your code.
Answers 2
I think you also need to write get handler for cart_proxy for getItems to work.
0 comments:
Post a Comment