go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/associated_bugs_badge/associated_bugs_badge.test.ts (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 { fixture, oneEvent } from '@open-wc/testing-helpers';
    16  import { html } from 'lit';
    17  
    18  import './associated_bugs_badge';
    19  import { AssociatedBugsTooltipElement } from '@/common/components/associated_bugs_tooltip';
    20  import { ShowTooltipEventDetail } from '@/common/components/tooltip';
    21  import { Cluster } from '@/common/services/luci_analysis';
    22  
    23  import { AssociatedBugsBadgeElement } from './associated_bugs_badge';
    24  
    25  const cluster1: Cluster = {
    26    clusterId: {
    27      algorithm: 'rule',
    28      id: 'cluster1',
    29    },
    30    bug: {
    31      system: 'monorail',
    32      id: '1234',
    33      linkText: 'crbug.com/1234',
    34      url: 'http://crbug.com/1234',
    35    },
    36  };
    37  
    38  const cluster2: Cluster = {
    39    clusterId: {
    40      algorithm: 'rule',
    41      id: 'cluster2',
    42    },
    43    bug: {
    44      system: 'monorail',
    45      id: '5678',
    46      linkText: 'crbug.com/5678',
    47      url: 'http://crbug.com/5678',
    48    },
    49  };
    50  
    51  const cluster3: Cluster = {
    52    clusterId: {
    53      algorithm: 'rule',
    54      id: 'cluster2',
    55    },
    56    bug: {
    57      system: 'buganizer',
    58      id: '1234',
    59      linkText: 'b/1234',
    60      url: 'http://b/1234',
    61    },
    62  };
    63  
    64  describe('AssociatedBugsBadge', () => {
    65    test('should remove duplicated bugs', async () => {
    66      const ele = await fixture<AssociatedBugsBadgeElement>(html`
    67        <milo-associated-bugs-badge
    68          .clusters=${[cluster1, cluster2, cluster3, cluster1]}
    69        ></milo-associated-bugs-badge>
    70      `);
    71  
    72      expect(ele.shadowRoot!.textContent).toContain('crbug.com/1234');
    73      expect(ele.shadowRoot!.textContent).not.toMatch(
    74        /crbug\.com\/1234(.*)crbug\.com\/1234/,
    75      );
    76    });
    77  
    78    test('should render a list on hover', async () => {
    79      const ele = await fixture<AssociatedBugsBadgeElement>(html`
    80        <milo-associated-bugs-badge
    81          .clusters=${[cluster1, cluster2, cluster3]}
    82        ></milo-associated-bugs-badge>
    83      `);
    84  
    85      const listener = oneEvent(window, 'show-tooltip', false);
    86      ele
    87        .shadowRoot!.querySelector('.badge')!
    88        .dispatchEvent(new MouseEvent('mouseover'));
    89      const event: CustomEvent<ShowTooltipEventDetail> = await listener;
    90  
    91      const tooltip = event.detail.tooltip.querySelector(
    92        'milo-associated-bugs-tooltip',
    93      )! as AssociatedBugsTooltipElement;
    94  
    95      expect(tooltip.clusters).toEqual([cluster1, cluster2, cluster3]);
    96    });
    97  });