go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/src/components/metrics_selector/metrics_selector.test.tsx (about)

     1  // Copyright 2023 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  
    17  import {
    18    fireEvent,
    19    screen,
    20    render,
    21  } from '@testing-library/react';
    22  
    23  import { getMockMetricsList } from '@/testing_tools/mocks/metrics_mock';
    24  
    25  import MetricsSelector from './metrics_selector';
    26  
    27  describe('Test ClusterTableMetricSelection component', () => {
    28    it('given a list of metrics, should display select items', async () => {
    29      const metrics = getMockMetricsList('testproject');
    30      render(
    31          <MetricsSelector metrics={metrics} selectedMetrics={[]} handleSelectedMetricsChanged={()=>{
    32            // do nothing.
    33          }}/>,
    34      );
    35  
    36      await (screen.findAllByText('Metrics'));
    37  
    38      await fireEvent.mouseDown(screen.getByRole('button'));
    39  
    40      metrics.forEach(((metric) => expect(screen.getByText(metric.humanReadableName)).toBeInTheDocument()));
    41      metrics.forEach(((metric) => expect(screen.getByText(metric.description)).toBeInTheDocument()));
    42    });
    43  
    44    it('given a list of selected metrics, then should be values of the list', async () => {
    45      const metrics = getMockMetricsList('testproject');
    46  
    47      const selectedMetrics = [metrics[0].metricId, metrics[1].metricId];
    48      render(
    49          <MetricsSelector metrics={metrics} selectedMetrics={selectedMetrics} handleSelectedMetricsChanged={()=>{
    50            // do nothing.
    51          }}/>,
    52      );
    53  
    54      await (screen.findAllByText('Metrics'));
    55      expect(screen.getByTestId('metrics-selector')).toHaveValue(selectedMetrics.join(','));
    56    });
    57  });