go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/linkified_text/linkified_text.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 } from '@testing-library/react';
    16  
    17  import { LinkifiedText } from './linkified_text';
    18  
    19  it('displays normal text', async () => {
    20    render(<LinkifiedText text="abcd" />);
    21    expect(screen.getByText('abcd')).toBeInTheDocument();
    22  });
    23  
    24  it('displays a solitary link', async () => {
    25    render(<LinkifiedText text="go/abcd" />);
    26    expect(screen.getByRole('link', { name: 'go/abcd' })).toHaveAttribute(
    27      'href',
    28      'http://go/abcd',
    29    );
    30  });
    31  
    32  it('displays a link at the end of the text', async () => {
    33    render(<LinkifiedText text="click on go/abcd" />);
    34    expect(screen.getByText('click on')).toBeInTheDocument();
    35    expect(screen.getByRole('link', { name: 'go/abcd' })).toHaveAttribute(
    36      'href',
    37      'http://go/abcd',
    38    );
    39  });
    40  
    41  it('displays a link at the start of the text', async () => {
    42    render(<LinkifiedText text="go/abcd is great" />);
    43    expect(screen.getByRole('link', { name: 'go/abcd' })).toHaveAttribute(
    44      'href',
    45      'http://go/abcd',
    46    );
    47    expect(screen.getByText('is great')).toBeInTheDocument();
    48  });
    49  
    50  it('displays a link surrounded by text', async () => {
    51    render(<LinkifiedText text="click on go/abcd is great" />);
    52    expect(screen.getByText('click on')).toBeInTheDocument();
    53    expect(screen.getByRole('link', { name: 'go/abcd' })).toHaveAttribute(
    54      'href',
    55      'http://go/abcd',
    56    );
    57    expect(screen.getByText('is great')).toBeInTheDocument();
    58  });
    59  
    60  it('displays two links surrounded by text', async () => {
    61    render(
    62      <LinkifiedText text="click on go/abcd then have a look at crrev.com/c/1234. Have fun!" />,
    63    );
    64    expect(screen.getByText('click on')).toBeInTheDocument();
    65    expect(screen.getByRole('link', { name: 'go/abcd' })).toHaveAttribute(
    66      'href',
    67      'http://go/abcd',
    68    );
    69    expect(screen.getByText('then have a look at')).toBeInTheDocument();
    70    expect(
    71      screen.getByRole('link', { name: 'crrev.com/c/1234' }),
    72    ).toHaveAttribute('href', 'http://crrev.com/c/1234');
    73    expect(screen.getByText('. Have fun!')).toBeInTheDocument();
    74  });
    75  
    76  it('linkifies go links', async () => {
    77    render(<LinkifiedText text="go/abcd" />);
    78    expect(screen.getByRole('link', { name: 'go/abcd' })).toHaveAttribute(
    79      'href',
    80      'http://go/abcd',
    81    );
    82  });
    83  
    84  it('linkifies buganizer links', async () => {
    85    render(<LinkifiedText text="b/1234" />);
    86    expect(screen.getByRole('link', { name: 'b/1234' })).toHaveAttribute(
    87      'href',
    88      'http://b/1234',
    89    );
    90  });
    91  
    92  it('linkifies crrev links', async () => {
    93    render(<LinkifiedText text="crrev.com/c/1234" />);
    94    expect(
    95      screen.getByRole('link', { name: 'crrev.com/c/1234' }),
    96    ).toHaveAttribute('href', 'http://crrev.com/c/1234');
    97  });
    98  
    99  it('linkifies bare links', async () => {
   100    render(<LinkifiedText text="https://google.com" />);
   101    expect(
   102      screen.getByRole('link', { name: 'https://google.com' }),
   103    ).toHaveAttribute('href', 'https://google.com');
   104  });