go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/examples/reactjs/example_app/src/components/example/example.test.tsx (about)

     1  // Copyright 2022 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  
    16  /**
    17   * This adds testing-library matchers to jest.
    18   */
    19  import '@testing-library/jest-dom';
    20  
    21  import { Request } from 'node-fetch';
    22  
    23  /**
    24   * This import must be declared before any use of `fetch`
    25   * in your code if you want to mock the `fetch` calls.
    26   *
    27   * If any call is made to `fetch` before this gets to run (for example,
    28   * if you run it in a constructor of a class), your mocks will fail.
    29   */
    30  import fetchMock from 'fetch-mock-jest';
    31  import { Provider } from 'react-redux';
    32  
    33  import {
    34    render,
    35    screen,
    36  } from '@testing-library/react';
    37  
    38  import { store } from '@/store/store';
    39  import Example from './example';
    40  
    41  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    42  // @ts-ignore
    43  window.Request = Request;
    44  
    45  describe('<Example />', () => {
    46    afterEach(() => {
    47      fetchMock.mockClear();
    48      fetchMock.reset();
    49    });
    50  
    51    /**
    52     * This test has been disabled as it needs different
    53     *  set-up to work with react query.
    54     */
    55    // eslint-disable-next-line jest/no-disabled-tests
    56    it.skip('Wait for component to load', async () => {
    57      fetchMock.mock('/api/v2/user/user', ['user']);
    58      render(
    59          <Provider store={store}>
    60            <Example exampleProp='test'/>,
    61          </Provider>,
    62      );
    63  
    64      await screen.findByRole('article');
    65  
    66      expect(screen.getByText('test')).toBeInTheDocument();
    67    });
    68  });