go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/tools/markdown/plugins/reviewer_line.test.ts (about)

     1  // Copyright 2020 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 } from '@open-wc/testing-helpers';
    16  import markdownIt from 'markdown-it';
    17  
    18  import { reviewerLine } from './reviewer_line';
    19  
    20  const singleReviewerLine = 'R=user@google.com';
    21  const multipleReviewerLinesWithSoftBreak =
    22    'R=user@google.com\nR=user2@google.com';
    23  const multipleReviewerLinesWithHardBreak =
    24    'R=user@google.com  \nR=user2@google.com';
    25  
    26  describe('reviewer_line', () => {
    27    test('does not treat "R=" prefix as part of the email address', async () => {
    28      const md = markdownIt('zero', { breaks: true, linkify: true })
    29        .enable(['newline', 'linkify'])
    30        .use(reviewerLine);
    31  
    32      const ele = await fixture(md.render(singleReviewerLine));
    33  
    34      const anchors = ele.querySelectorAll('a');
    35      expect(anchors.length).toStrictEqual(1);
    36      const anchor1 = anchors.item(0);
    37      expect(anchor1.href).toStrictEqual('mailto:user@google.com');
    38      expect(anchor1.text).toStrictEqual('user@google.com');
    39    });
    40  
    41    describe('When breaks is set to true', () => {
    42      const md = markdownIt('zero', { breaks: true, linkify: true })
    43        .enable(['newline', 'linkify'])
    44        .use(reviewerLine);
    45  
    46      test('can renders multiple reviewer lines with soft break correctly', async () => {
    47        const ele = await fixture(md.render(multipleReviewerLinesWithSoftBreak));
    48  
    49        const anchors = ele.querySelectorAll('a');
    50        expect(anchors.length).toStrictEqual(2);
    51  
    52        const anchor1 = anchors.item(0);
    53        expect(anchor1.href).toStrictEqual('mailto:user@google.com');
    54        expect(anchor1.text).toStrictEqual('user@google.com');
    55  
    56        const anchor2 = anchors.item(1);
    57        expect(anchor2.href).toStrictEqual('mailto:user2@google.com');
    58        expect(anchor2.text).toStrictEqual('user2@google.com');
    59      });
    60  
    61      test('can renders multiple reviewer lines with hard break correctly', async () => {
    62        const ele = await fixture(md.render(multipleReviewerLinesWithHardBreak));
    63  
    64        const anchors = ele.querySelectorAll('a');
    65        expect(anchors.length).toStrictEqual(2);
    66  
    67        const anchor1 = anchors.item(0);
    68        expect(anchor1.href).toStrictEqual('mailto:user@google.com');
    69        expect(anchor1.text).toStrictEqual('user@google.com');
    70  
    71        const anchor2 = anchors.item(1);
    72        expect(anchor2.href).toStrictEqual('mailto:user2@google.com');
    73        expect(anchor2.text).toStrictEqual('user2@google.com');
    74      });
    75    });
    76  
    77    describe('When breaks is set to false', () => {
    78      const md = markdownIt('zero', { breaks: false, linkify: true })
    79        .enable(['newline', 'linkify'])
    80        .use(reviewerLine);
    81  
    82      test('can renders multiple reviewer lines with soft break correctly', async () => {
    83        const ele = await fixture(md.render(multipleReviewerLinesWithSoftBreak));
    84  
    85        const anchors = ele.querySelectorAll('a');
    86        expect(anchors.length).toStrictEqual(2);
    87  
    88        const anchor1 = anchors.item(0);
    89        expect(anchor1.href).toStrictEqual('mailto:user@google.com');
    90        expect(anchor1.text).toStrictEqual('user@google.com');
    91  
    92        const anchor2 = anchors.item(1);
    93        expect(anchor2.href).toStrictEqual('mailto:R=user2@google.com');
    94        expect(anchor2.text).toStrictEqual('R=user2@google.com');
    95      });
    96  
    97      test('can renders multiple reviewer lines with hard break correctly', async () => {
    98        const ele = await fixture(md.render(multipleReviewerLinesWithHardBreak));
    99  
   100        const anchors = ele.querySelectorAll('a');
   101        expect(anchors.length).toStrictEqual(2);
   102  
   103        const anchor1 = anchors.item(0);
   104        expect(anchor1.href).toStrictEqual('mailto:user@google.com');
   105        expect(anchor1.text).toStrictEqual('user@google.com');
   106  
   107        const anchor2 = anchors.item(1);
   108        expect(anchor2.href).toStrictEqual('mailto:user2@google.com');
   109        expect(anchor2.text).toStrictEqual('user2@google.com');
   110      });
   111    });
   112  });