mercredi 16 décembre 2020

Jest testing library - multiple calls to load data on click event

I am trying to test a load more button call on an onClick fireEvent but I am having trouble simulating the click to trigger a load data.

it('load more data on load more button click', () => {
    const Items = require('./default').default

    // set initial load values: initVals (2 items)
    // set second call values: secondVals (4 items)

    Items.prototype.getData = jest.fn()
    Items.prototype.getData.mockReturnValue(initVals)
    Items.prototype.getData.mockReturnValue(secondVals)

    const { container } = render(
      <Items
        fields=
      />
    )

    const button = screen.getByText('Load More')
    fireEvent.click(button)

    expect(container.querySelectorAll('.item-box').length).toBe(2)
    expect(container.querySelectorAll('.item-box').length).toBe(4)
  })

So this only reads the last call, finding 4 items.

Calling .mockReturnValue() multiple times has only yielded me the last call instead of it consecutively. I know I am using it wrong but I can't figure out the sequence of running this. My goal is to initialize the component with first values (load 2 items), then on click, it loads more (4 items).

Help?

Aucun commentaire:

Enregistrer un commentaire