go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/src/components/rules_table/rules_table.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  import '@testing-library/jest-dom';
    16  import 'node-fetch';
    17  
    18  import fetchMock from 'fetch-mock-jest';
    19  
    20  import { fireEvent, screen } from '@testing-library/react';
    21  
    22  import { renderWithRouterAndClient } from '@/testing_tools/libs/mock_router';
    23  import { mockFetchAuthState } from '@/testing_tools/mocks/authstate_mock';
    24  import {
    25    mockFetchRules,
    26    createDefaultMockListRulesResponse,
    27  } from '@/testing_tools/mocks/rules_mock';
    28  
    29  import { mockFetchProjectConfig } from '@/testing_tools/mocks/projects_mock';
    30  import RulesTable from './rules_table';
    31  
    32  describe('Test RulesTable component', () => {
    33    beforeEach(() => {
    34      mockFetchAuthState();
    35      mockFetchProjectConfig();
    36    });
    37    afterEach(() => {
    38      fetchMock.mockClear();
    39      fetchMock.reset();
    40    });
    41  
    42    it('given a project, should display the active rules', async () => {
    43      mockFetchRules(createDefaultMockListRulesResponse());
    44  
    45      renderWithRouterAndClient(
    46          <RulesTable
    47            project='chromium'/>,
    48          '/p/chromium/rules',
    49          '/p/:project/rules',
    50      );
    51      await screen.findByText('Rule Definition');
    52  
    53      expect(screen.getByText('crbug.com/90001')).toBeInTheDocument();
    54      expect(screen.getByText('test LIKE "rule1%"')).toBeInTheDocument();
    55      expect(screen.getByText('exonerations')).toBeInTheDocument();
    56      expect(screen.getByText('P2')).toBeInTheDocument(); // Priority shown as problem is active.
    57  
    58      expect(screen.getByText('crbug.com/90002')).toBeInTheDocument();
    59      expect(screen.getByText('reason LIKE "rule2%"')).toBeInTheDocument();
    60      expect(screen.getByText('cls-rejected')).toBeInTheDocument();
    61    });
    62  
    63    it('if rule definitions are unavailable, filler text is displayed', async () => {
    64      const response = createDefaultMockListRulesResponse();
    65      response.rules?.forEach((r) => {
    66        r.ruleDefinition = '';
    67      });
    68  
    69      mockFetchRules(response);
    70  
    71      renderWithRouterAndClient(
    72          <RulesTable
    73            project='chromium'/>,
    74          '/p/chromium/rules',
    75          '/p/:project/rules',
    76      );
    77      await screen.findByText('Rule Definition');
    78  
    79      expect(screen.queryAllByText('Click to see example failures.')).toHaveLength(2);
    80    });
    81  
    82    it('problem filter filters rules', async () => {
    83      const response = createDefaultMockListRulesResponse();
    84  
    85      mockFetchRules(response);
    86  
    87      renderWithRouterAndClient(
    88          <RulesTable
    89            project='chromium'/>,
    90          '/p/chromium/rules',
    91          '/p/:project/rules',
    92      );
    93      await screen.findByText('Rule Definition');
    94  
    95      await fireEvent.change(screen.getByTestId('problem_filter_input'), { target: { value: 'exonerations' } });
    96  
    97      // Rule 1 visible.
    98      expect(screen.getByText('crbug.com/90001')).toBeInTheDocument();
    99      // Rule 2 not visible.
   100      expect(screen.queryByText('crbug.com/90002')).toBeNull();
   101    });
   102  });