go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/tools/markdown/plugins/bug_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 { bugLine } from './bug_line'; 19 20 const singleBugLine = 'Bug: 123, 234, not a project, proj-1:345'; 21 const multipleBugLinesWithSoftBreak = 'Bug: 123\nBUG:234'; 22 const multipleBugLinesWithHardBreak = 'Bug: 123 \nBUG:234'; 23 24 describe('bug_line', () => { 25 test('can render single bug line correctly', async () => { 26 const md = markdownIt('zero', { breaks: true }) 27 .enable('newline') 28 .use(bugLine); 29 30 const ele = await fixture(md.render(singleBugLine)); 31 32 const anchors = ele.querySelectorAll('a'); 33 expect(anchors.length).toStrictEqual(3); 34 35 const anchor1 = anchors.item(0); 36 expect(anchor1.href).toStrictEqual('https://crbug.com/123'); 37 expect(anchor1.text).toStrictEqual('123'); 38 39 const anchor2 = anchors.item(1); 40 expect(anchor2.href).toStrictEqual('https://crbug.com/234'); 41 expect(anchor2.text).toStrictEqual('234'); 42 43 const anchor3 = anchors.item(2); 44 expect(anchor3.href).toStrictEqual('https://crbug.com/proj-1/345'); 45 expect(anchor3.text).toStrictEqual('proj-1:345'); 46 47 expect(ele.childNodes[4].textContent).toStrictEqual(', not a project, '); 48 }); 49 50 describe('When breaks is set to true', () => { 51 const md = markdownIt('zero', { breaks: true }) 52 .enable('newline') 53 .use(bugLine); 54 55 test('can renders multiple bug lines with soft break correctly', async () => { 56 const ele = await fixture(md.render(multipleBugLinesWithSoftBreak)); 57 58 const anchors = ele.querySelectorAll('a'); 59 expect(anchors.length).toStrictEqual(2); 60 61 const anchor1 = anchors.item(0); 62 expect(anchor1.href).toStrictEqual('https://crbug.com/123'); 63 expect(anchor1.text).toStrictEqual('123'); 64 65 const anchor2 = anchors.item(1); 66 expect(anchor2.href).toStrictEqual('https://crbug.com/234'); 67 expect(anchor2.text).toStrictEqual('234'); 68 }); 69 70 test('can renders multiple bug lines with hard break correctly', async () => { 71 const ele = await fixture(md.render(multipleBugLinesWithHardBreak)); 72 73 const anchors = ele.querySelectorAll('a'); 74 expect(anchors.length).toStrictEqual(2); 75 76 const anchor1 = anchors.item(0); 77 expect(anchor1.href).toStrictEqual('https://crbug.com/123'); 78 expect(anchor1.text).toStrictEqual('123'); 79 80 const anchor2 = anchors.item(1); 81 expect(anchor2.href).toStrictEqual('https://crbug.com/234'); 82 expect(anchor2.text).toStrictEqual('234'); 83 }); 84 }); 85 86 describe('When breaks is set to false', () => { 87 const md = markdownIt('zero', { breaks: false }) 88 .enable('newline') 89 .use(bugLine); 90 91 test('can renders multiple bug lines with soft break correctly', async () => { 92 const ele = await fixture(md.render(multipleBugLinesWithSoftBreak)); 93 94 const anchors = ele.querySelectorAll('a'); 95 expect(anchors.length).toStrictEqual(2); 96 97 const anchor1 = anchors.item(0); 98 expect(anchor1.href).toStrictEqual('https://crbug.com/123'); 99 expect(anchor1.text).toStrictEqual('123'); 100 101 const anchor2 = anchors.item(1); 102 expect(anchor2.href).toStrictEqual('https://crbug.com/BUG/234'); 103 expect(anchor2.text).toStrictEqual('BUG:234'); 104 }); 105 106 test('can renders multiple bug lines with hard break correctly', async () => { 107 const ele = await fixture(md.render(multipleBugLinesWithHardBreak)); 108 109 const anchors = ele.querySelectorAll('a'); 110 expect(anchors.length).toStrictEqual(2); 111 112 const anchor1 = anchors.item(0); 113 expect(anchor1.href).toStrictEqual('https://crbug.com/123'); 114 expect(anchor1.text).toStrictEqual('123'); 115 116 const anchor2 = anchors.item(1); 117 expect(anchor2.href).toStrictEqual('https://crbug.com/234'); 118 expect(anchor2.text).toStrictEqual('234'); 119 }); 120 }); 121 });