Repository: adobe/react-spectrum
File: packages/react-aria-components/test/Table.test.js (JavaScript)

Code before:
      expect(rows).toHaveLength(5);
      expect(rows[1]).toHaveTextContent('FalconSat');
      expect(rows[2]).toHaveTextContent('DemoSat');
      expect(rows[3]).toHaveTextContent('Trailblazer');
      expect(rows[4]).toHaveTextContent('RatSat');
    });

    it('should not render excessively in React Suspense with transitions', async () => {
      // Only supported in React 19.
      if (!React.use) {
        return;
      }

      jest.useRealTimers();
      const spy = jest.spyOn(module.Table, 'render');

      let tree = await act(() => render(<TableWithSuspense reactTransition />));

      await act(() => stories.makePromise([]));

      expect(spy).toHaveBeenCalledTimes(2);

      let button = tree.getByRole('button');
      await act(() => button.click());

      expect(spy).toHaveBeenCalledTimes(6);
      expect(button).toHaveTextContent('Loading');

      await act(() => stories.makePromise([]));

      button = tree.getByRole('button');
      expect(button).toHaveTextContent('Load more');

      expect(spy).toHaveBeenCalledTimes(10);
    });
  });

  describe('shouldSelectOnPressUp', () => {
    it('should select an item on pressing down when shouldSelectOnPressUp is not provided', async () => {
      let onSelectionChange = jest.fn();
      let {getAllByRole} = renderTable({tableProps: {selectionMode: 'single', onSelectionChange}});
      let items = getAllByRole('row');

      await user.pointer({target: items[1], keys: '[MouseLeft>]'});   
      expect(onSelectionChange).toBeCalledTimes(1);
  
      await user.pointer({target: items[1], keys: '[/MouseLeft]'});
      expect(onSelectionChange).toBeCalledTimes(1);
    });

    it('should select an item on pressing down when shouldSelectOnPressUp is false', async () => {

Review comment (bug):
number of times might be a little arbitrary? we could theoretically add another render to tables through some other means and it wouldn't alert us to the bug that was fixed. or React might batch differently in the future.

That said, probably still good to know if we add renders or if React changes them 🤷🏻 

Code after:
      expect(button).toHaveTextContent('Load more');

      let promise = act(() => button.click());

      expect(button).toHaveTextContent('Loading');

      rows = tree.getAllByRole('row');
      expect(rows).toHaveLength(3);
      expect(rows[1]).toHaveTextContent('FalconSat');
      expect(rows[2]).toHaveTextContent('DemoSat');

      promise.then(() => {});
      // eslint-disable-next-line
      jest.runAllTimers();
      await promise;

      expect(button).toHaveTextContent('Load more');

      rows = tree.getAllByRole('row');
      expect(rows).toHaveLength(5);
      expect(rows[1]).toHaveTextContent('FalconSat');
      expect(rows[2]).toHaveTextContent('DemoSat');
      expect(rows[3]).toHaveTextContent('Trailblazer');
      expect(rows[4]).toHaveTextContent('RatSat');
    });

    it('should not render excessively in React Suspense with transitions', async () => {
      // Only supported in React 19.
      if (!React.use) {
        return;
      }

      jest.useRealTimers();

      let tree = await act(() => render(<TableWithSuspense reactTransition />));

      await act(() => stories.makePromise([]));

      expect(mockCollectionUpdate).toHaveBeenCalledTimes(1);

      let button = tree.getByRole('button');
      await act(() => button.click());

      expect(mockCollectionUpdate).toHaveBeenCalledTimes(2);
    });
  });

  describe('shouldSelectOnPressUp', () => {
    it('should select an item on pressing down when shouldSelectOnPressUp is not provided', async () => {
      let onSelectionChange = jest.fn();
      let {getAllByRole} = renderTable({tableProps: {selectionMode: 'single', onSelectionChange}});