Proxy test does not work in Jasmine

the 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 does not work if the code says cart_proxy.push, but if the code says that cart.pushspec passes. Also in the console I can confirm that it cart_proxy.pushworks accordingly. Failure seems to be something in using a proxy

+4
source share
1 answer

It seems to me that you cannot do the right thing cart_proxyand that the main reason for the failure of the test. In addition, your test may have several problems.

You have a syntax error. Correct 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 ); 
    }
  }
})

, new Proxy(, -, , . cart.push, cart , , - cart_proxy .

, beforeEach:

 beforeEach(function() {
    cartModule.toggleItem("ladder", true)
  })

, toggleItem - cartModule, , 2 .

.

+1

Source: https://habr.com/ru/post/1689172/


All Articles