javascript - how to loop to expect a list of item in jest - Stack Overflow

I have added the below jest unit test to render a ponent and I am expecting some of its elements using

I have added the below jest unit test to render a ponent and I am expecting some of its elements using "getAllByText" as there are multiple similar elements. I got a review ment to loop over the list of element if there are more than 1 and expect each of them to be visible. Can someone please help me in modifying the test to loop over all the elements.

it('should render seat total wrapper on small breakpoint', () => {
    render(
      <MockViewportProvider viewSize={ViewSizes.SMALL}>
        <SeatMapContext.Provider value={{ state: contextObj, dispatch: noop }}>
          <SeatMapCabinAndSeatWrapper
            flightSeatMapPlaneData={seatMapPlanesData}
            flightLegsDetails={FlightLegsDetails}
            onChangeLegIndex={jest.fn}
            selectedLegIndex={0}
            handleLegendButtonClick={jest.fn()}
            handleApplySeatSelection={handleApplySeatSelection}
          />
        </SeatMapContext.Provider>
      </MockViewportProvider>
    );
    expect(screen.getByRole('button', { name: 'Next Flight' })).toBeVisible();
    expect(screen.getAllByText('$276.24')[0]).toBeVisible();
  });

Review ments -> "if there are expected to be more than 1 item then please loop over the list and expect each element to be visible if there should only be 1 item then please determine how to use getByText".

I have added the below jest unit test to render a ponent and I am expecting some of its elements using "getAllByText" as there are multiple similar elements. I got a review ment to loop over the list of element if there are more than 1 and expect each of them to be visible. Can someone please help me in modifying the test to loop over all the elements.

it('should render seat total wrapper on small breakpoint', () => {
    render(
      <MockViewportProvider viewSize={ViewSizes.SMALL}>
        <SeatMapContext.Provider value={{ state: contextObj, dispatch: noop }}>
          <SeatMapCabinAndSeatWrapper
            flightSeatMapPlaneData={seatMapPlanesData}
            flightLegsDetails={FlightLegsDetails}
            onChangeLegIndex={jest.fn}
            selectedLegIndex={0}
            handleLegendButtonClick={jest.fn()}
            handleApplySeatSelection={handleApplySeatSelection}
          />
        </SeatMapContext.Provider>
      </MockViewportProvider>
    );
    expect(screen.getByRole('button', { name: 'Next Flight' })).toBeVisible();
    expect(screen.getAllByText('$276.24')[0]).toBeVisible();
  });

Review ments -> "if there are expected to be more than 1 item then please loop over the list and expect each element to be visible if there should only be 1 item then please determine how to use getByText".

Share Improve this question edited Aug 27, 2022 at 13:01 Sunderam Dubey 8,83712 gold badges24 silver badges42 bronze badges asked Feb 8, 2022 at 18:03 SaurabhSaurabh 431 gold badge1 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Just use a for loop.

for (const element of screen.getAllByText('$276.24')) {
    expect(element).toBeVisible();
}

For an implementation with no for..of loop:

const list = screen.getAllByText('$276.24');
for (let i = 0; i < list.length; i++) {
    expect(list[i]).toBeVisible();
}

Or:

screen.getAllByText('$276.24').forEach((element) => {
    expect(element).toBeVisible();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744310801a4567937.html

相关推荐

  • javascript - how to loop to expect a list of item in jest - Stack Overflow

    I have added the below jest unit test to render a ponent and I am expecting some of its elements using

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信