How to test if component counts array length properly with Jest

I have a component that I am giving an array with objects as props to it like this:

describe('component', () => {
  it('should return the correct number of items passed in the array', () => {
    const comp = shallowMount(component, {propsData: {
      buttons: [
        {name:'button1'},
        {name:'button2'}
      ]
    }});
    expect(component.length).toHaveLength(buttons).length
  });
});

How can I test that the provided array has the correct length, for example if there are two objects in the array, the component should return two, if there is one, one, if there are none then it should return 0, how can I achieve that? I tried

expect(component.length).toHaveLength(buttons).length

But that does not work

5 thoughts on “How to test if component counts array length properly with Jest”

  1. const buttons = [
      {name:'button1'},
      {name:'button2'}
    ]
    
    const comp = shallowMount(component, {propsData: { buttons }});
    
    expect(comp.findAll(Button)).toHaveLength(buttons.length)
    
    Reply

Leave a Comment