Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 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 {MrCue} from './mr-cue.js'; |
| 7 | import page from 'page'; |
| 8 | import {rootReducer} from 'reducers/base.js'; |
| 9 | |
| 10 | let element; |
| 11 | |
| 12 | describe('mr-cue', () => { |
| 13 | beforeEach(() => { |
| 14 | element = document.createElement('mr-cue'); |
| 15 | document.body.appendChild(element); |
| 16 | |
| 17 | sinon.stub(page, 'call'); |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | document.body.removeChild(element); |
| 22 | |
| 23 | page.call.restore(); |
| 24 | }); |
| 25 | |
| 26 | it('initializes', () => { |
| 27 | assert.instanceOf(element, MrCue); |
| 28 | }); |
| 29 | |
| 30 | it('stateChanged', () => { |
| 31 | const state = rootReducer({ |
| 32 | userV0: {currentUser: {prefs: new Map(), prefsLoaded: false}}, |
| 33 | }, {}); |
| 34 | element.stateChanged(state); |
| 35 | assert.deepEqual(element.prefs, new Map([['render_markdown', false]])); |
| 36 | assert.isFalse(element.prefsLoaded); |
| 37 | }); |
| 38 | |
| 39 | it('cues are hidden before prefs load', () => { |
| 40 | element.prefsLoaded = false; |
| 41 | assert.isTrue(element.hidden); |
| 42 | }); |
| 43 | |
| 44 | it('cue is hidden if user already dismissed it', () => { |
| 45 | element.prefsLoaded = true; |
| 46 | element.cuePrefName = 'code_of_conduct'; |
| 47 | element.prefs = new Map([['code_of_conduct', true]]); |
| 48 | assert.isTrue(element.hidden); |
| 49 | }); |
| 50 | |
| 51 | it('cue is hidden if no relevent message', () => { |
| 52 | element.prefsLoaded = true; |
| 53 | element.cuePrefName = 'this_has_no_message'; |
| 54 | assert.isTrue(element.hidden); |
| 55 | }); |
| 56 | |
| 57 | it('cue is shown if relevant message has not been dismissed', async () => { |
| 58 | element.prefsLoaded = true; |
| 59 | element.cuePrefName = 'code_of_conduct'; |
| 60 | |
| 61 | await element.updateComplete; |
| 62 | |
| 63 | assert.isFalse(element.hidden); |
| 64 | const messageEl = element.shadowRoot.querySelector('#message'); |
| 65 | assert.include(messageEl.innerHTML, 'chromium.googlesource.com'); |
| 66 | }); |
| 67 | |
| 68 | it('code of conduct is specific to the project', async () => { |
| 69 | element.prefsLoaded = true; |
| 70 | element.cuePrefName = 'code_of_conduct'; |
| 71 | element.projectName = 'fuchsia'; |
| 72 | |
| 73 | await element.updateComplete; |
| 74 | |
| 75 | assert.isFalse(element.hidden); |
| 76 | const messageEl = element.shadowRoot.querySelector('#message'); |
| 77 | assert.include(messageEl.innerHTML, 'fuchsia.dev'); |
| 78 | }); |
| 79 | |
| 80 | it('availability cue is hidden if no relevent issue particpants', () => { |
| 81 | element.prefsLoaded = true; |
| 82 | element.cuePrefName = 'availability_msgs'; |
| 83 | element.issue = {summary: 'no owners or cc'}; |
| 84 | assert.isTrue(element.hidden); |
| 85 | |
| 86 | element.issue = { |
| 87 | summary: 'owner and ccs have no availability msg', |
| 88 | ownerRef: {}, |
| 89 | ccRefs: [{}, {}], |
| 90 | }; |
| 91 | assert.isTrue(element.hidden); |
| 92 | }); |
| 93 | |
| 94 | it('availability cue is shown if issue particpants are unavailable', |
| 95 | async () => { |
| 96 | element.prefsLoaded = true; |
| 97 | element.cuePrefName = 'availability_msgs'; |
| 98 | element.referencedUsers = new Map([ |
| 99 | ['user@example.com', {availability: 'Never visited'}], |
| 100 | ]); |
| 101 | |
| 102 | element.issue = { |
| 103 | summary: 'owner is unavailable', |
| 104 | ownerRef: {displayName: 'user@example.com'}, |
| 105 | ccRefs: [{}, {}], |
| 106 | }; |
| 107 | await element.updateComplete; |
| 108 | |
| 109 | assert.isFalse(element.hidden); |
| 110 | const messageEl = element.shadowRoot.querySelector('#message'); |
| 111 | assert.include(messageEl.innerText, 'Clock icons'); |
| 112 | |
| 113 | element.issue = { |
| 114 | summary: 'owner is unavailable', |
| 115 | ownerRef: {}, |
| 116 | ccRefs: [ |
| 117 | {displayName: 'ok@example.com'}, |
| 118 | {displayName: 'user@example.com'}], |
| 119 | }; |
| 120 | await element.updateComplete; |
| 121 | assert.isFalse(element.hidden); |
| 122 | assert.include(messageEl.innerText, 'Clock icons'); |
| 123 | }); |
| 124 | |
| 125 | it('switch_to_parent_account cue is hidden if no linked account', () => { |
| 126 | element.prefsLoaded = true; |
| 127 | element.cuePrefName = 'switch_to_parent_account'; |
| 128 | |
| 129 | element.user = undefined; |
| 130 | assert.isTrue(element.hidden); |
| 131 | |
| 132 | element.user = {groups: []}; |
| 133 | assert.isTrue(element.hidden); |
| 134 | }); |
| 135 | |
| 136 | it('switch_to_parent_account is shown if user has parent account', |
| 137 | async () => { |
| 138 | element.prefsLoaded = true; |
| 139 | element.cuePrefName = 'switch_to_parent_account'; |
| 140 | element.user = {linkedParentRef: {displayName: 'parent@example.com'}}; |
| 141 | |
| 142 | await element.updateComplete; |
| 143 | assert.isFalse(element.hidden); |
| 144 | const messageEl = element.shadowRoot.querySelector('#message'); |
| 145 | assert.include(messageEl.innerText, 'a linked account'); |
| 146 | }); |
| 147 | |
| 148 | it('search_for_numbers cue is hidden if no number was used', () => { |
| 149 | element.prefsLoaded = true; |
| 150 | element.cuePrefName = 'search_for_numbers'; |
| 151 | element.issue = {}; |
| 152 | element.jumpLocalId = null; |
| 153 | assert.isTrue(element.hidden); |
| 154 | }); |
| 155 | |
| 156 | it('search_for_numbers cue is shown if jumped to issue ID', |
| 157 | async () => { |
| 158 | element.prefsLoaded = true; |
| 159 | element.cuePrefName = 'search_for_numbers'; |
| 160 | element.issue = {}; |
| 161 | element.jumpLocalId = '123'.match(new RegExp('^\\d+$')); |
| 162 | |
| 163 | await element.updateComplete; |
| 164 | assert.isFalse(element.hidden); |
| 165 | const messageEl = element.shadowRoot.querySelector('#message'); |
| 166 | assert.include(messageEl.innerText, 'use quotes'); |
| 167 | }); |
| 168 | |
| 169 | it('cue is dismissible unless there is attribute nondismissible', |
| 170 | async () => { |
| 171 | assert.isFalse(element.nondismissible); |
| 172 | |
| 173 | element.setAttribute('nondismissible', ''); |
| 174 | await element.updateComplete; |
| 175 | assert.isTrue(element.nondismissible); |
| 176 | }); |
| 177 | }); |