dimanche 15 avril 2018

why is this test not working in jest?

this is my test:

  let categoriesStub = [
    {id: 1, title: 'Drinks'},
    {id: 2, title: 'kitchenware'},
    {id: 3, title: 'snacks'}
  ];
  let productsStub = [
    {id: 1, title: 'wine01', showDescription: false, categories: [{title: 'wine'}]},
    {id: 2, title: 'coke', showDescription: false, categories: [{title: 'Drinks'}]},
    {id: 3, title: 'burgers', showDescription: false, categories: [{title: 'food'}]}
  ];
  it('should filter products when searched for', () => {
    wrapper.setState({products: productsStub})
    wrapper.setState({categories: categoriesStub})
    wrapper.setState({chosenCategory: 'Drinks'})
    wrapper.instance().filterProducts();
    expect(wrapper.instance().state('products').length).toEqual(1);
  });

function in my code im testing:

  filterProducts = () => {
    return this.state.products.filter((product => (
      product.categories.some((cat) => {
         cat.title == this.state.chosenCategory
      })
    )))
  }

just to explain whats going on:

my function should filter through the products and then only return the ones where the category for that product is equal to the chosenCategory

1) the function works in the code 2) when I log inside the function I get true for 'Drinks' when I expect it to equal the chosenCategory 3) the test fails saying expected 1, received 3. and i dont know why??

i'm calling the function but for some reason it's not filtering the state

ALSO

when I log this: console.log(wrapper.instance().filterProducts(), 'filtered'); it just logs an empty array?

any help?

Aucun commentaire:

Enregistrer un commentaire