go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/params_pager/params_pager.test.tsx (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { render, screen, fireEvent } from '@testing-library/react';
    16  
    17  import { useSyncedSearchParams } from '@/generic_libs/hooks/synced_search_params';
    18  import { FakeContextProvider } from '@/testing_tools/fakes/fake_context_provider';
    19  
    20  import { ParamsPager } from './params_pager';
    21  import { getPageToken } from './params_pager_utils';
    22  
    23  const ParamsPagerTestContainer = () => {
    24    const [searchParams, _] = useSyncedSearchParams();
    25    const pageToken = getPageToken(searchParams);
    26    const nextPageToken =
    27      { '': 'page2', page2: 'page3', page3: '' }[pageToken] || '';
    28    return <ParamsPager nextPageToken={nextPageToken} />;
    29  };
    30  describe('ParamsPager', () => {
    31    it('should navigate between pages properly', async () => {
    32      render(
    33        <FakeContextProvider>
    34          <ParamsPagerTestContainer />
    35        </FakeContextProvider>,
    36      );
    37  
    38      const prevPageLink = screen.getByText('Previous Page');
    39      const nextPageLink = screen.getByText('Next Page');
    40  
    41      expect(prevPageLink).toHaveAttribute('aria-disabled', 'true');
    42      expect(nextPageLink).not.toHaveAttribute('aria-disabled', 'true');
    43      expect(nextPageLink).toHaveAttribute('href', '/?cursor=page2');
    44  
    45      fireEvent.click(nextPageLink);
    46      expect(prevPageLink).not.toHaveAttribute('aria-disabled', 'true');
    47      expect(prevPageLink).toHaveAttribute('href', '/');
    48      expect(nextPageLink).not.toHaveAttribute('aria-disabled', 'true');
    49      expect(nextPageLink).toHaveAttribute('href', '/?cursor=page3');
    50  
    51      fireEvent.click(nextPageLink);
    52      expect(prevPageLink).not.toHaveAttribute('aria-disabled', 'true');
    53      expect(prevPageLink).toHaveAttribute('href', '/?cursor=page2');
    54      expect(nextPageLink).toHaveAttribute('aria-disabled', 'true');
    55  
    56      fireEvent.click(prevPageLink);
    57      expect(prevPageLink).not.toHaveAttribute('aria-disabled', 'true');
    58      expect(prevPageLink).toHaveAttribute('href', '/');
    59      expect(nextPageLink).not.toHaveAttribute('aria-disabled', 'true');
    60      expect(nextPageLink).toHaveAttribute('href', '/?cursor=page3');
    61  
    62      fireEvent.click(prevPageLink);
    63      expect(prevPageLink).toHaveAttribute('aria-disabled', 'true');
    64      expect(nextPageLink).not.toHaveAttribute('aria-disabled', 'true');
    65      expect(nextPageLink).toHaveAttribute('href', '/?cursor=page2');
    66    });
    67  });