Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {assert} from 'chai'; |
| 6 | import {MrFieldValues} from './mr-field-values.js'; |
| 7 | |
| 8 | import {fieldTypes} from 'shared/issue-fields.js'; |
| 9 | |
| 10 | |
| 11 | let element; |
| 12 | |
| 13 | describe('mr-field-values', () => { |
| 14 | beforeEach(() => { |
| 15 | element = document.createElement('mr-field-values'); |
| 16 | document.body.appendChild(element); |
| 17 | }); |
| 18 | |
| 19 | afterEach(() => { |
| 20 | document.body.removeChild(element); |
| 21 | }); |
| 22 | |
| 23 | it('initializes', () => { |
| 24 | assert.instanceOf(element, MrFieldValues); |
| 25 | }); |
| 26 | |
| 27 | it('renders empty if no values', async () => { |
| 28 | element.values = []; |
| 29 | |
| 30 | await element.updateComplete; |
| 31 | |
| 32 | assert.equal('----', element.shadowRoot.textContent.trim()); |
| 33 | }); |
| 34 | |
| 35 | it('renders user links when type is user', async () => { |
| 36 | element.type = fieldTypes.USER_TYPE; |
| 37 | element.values = ['test@example.com', 'hello@world.com']; |
| 38 | |
| 39 | await element.updateComplete; |
| 40 | |
| 41 | const links = element.shadowRoot.querySelectorAll('mr-user-link'); |
| 42 | |
| 43 | await links.updateComplete; |
| 44 | |
| 45 | assert.equal(2, links.length); |
| 46 | assert.include(links[0].shadowRoot.textContent, 'test@example.com'); |
| 47 | assert.include(links[1].shadowRoot.textContent, 'hello@world.com'); |
| 48 | }); |
| 49 | |
| 50 | it('renders URLs when type is url', async () => { |
| 51 | element.type = fieldTypes.URL_TYPE; |
| 52 | element.values = ['http://hello.world', 'go/link']; |
| 53 | |
| 54 | await element.updateComplete; |
| 55 | |
| 56 | const links = element.shadowRoot.querySelectorAll('a'); |
| 57 | |
| 58 | assert.equal(2, links.length); |
| 59 | assert.include(links[0].textContent, 'http://hello.world'); |
| 60 | assert.include(links[0].href, 'http://hello.world'); |
| 61 | assert.include(links[1].textContent, 'go/link'); |
| 62 | assert.include(links[1].href, 'go/link'); |
| 63 | }); |
| 64 | |
| 65 | it('renders generic field when field is string', async () => { |
| 66 | element.type = fieldTypes.STR_TYPE; |
| 67 | element.values = ['blah', 'random value', 'nothing here']; |
| 68 | element.name = 'fieldName'; |
| 69 | element.projectName = 'project'; |
| 70 | |
| 71 | await element.updateComplete; |
| 72 | |
| 73 | const links = element.shadowRoot.querySelectorAll('a'); |
| 74 | |
| 75 | assert.equal(3, links.length); |
| 76 | assert.include(links[0].textContent, 'blah'); |
| 77 | assert.include(links[0].href, |
| 78 | '/p/project/issues/list?q=fieldName=%22blah%22'); |
| 79 | assert.include(links[1].textContent, 'random value'); |
| 80 | assert.include(links[1].href, |
| 81 | '/p/project/issues/list?q=fieldName=%22random%20value%22'); |
| 82 | assert.include(links[2].textContent, 'nothing here'); |
| 83 | assert.include(links[2].href, |
| 84 | '/p/project/issues/list?q=fieldName=%22nothing%20here%22'); |
| 85 | }); |
| 86 | }); |