Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2020 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 sinon from 'sinon'; |
| 7 | import {MrProjectStar} from './mr-project-star.js'; |
| 8 | import {stars} from 'reducers/stars.js'; |
| 9 | |
| 10 | let element; |
| 11 | |
| 12 | describe('mr-project-star (disconnected)', () => { |
| 13 | beforeEach(() => { |
| 14 | element = document.createElement('mr-project-star'); |
| 15 | document.body.appendChild(element); |
| 16 | |
| 17 | sinon.stub(element, 'stateChanged'); |
| 18 | sinon.spy(stars, 'starProject'); |
| 19 | sinon.spy(stars, 'unstarProject'); |
| 20 | }); |
| 21 | |
| 22 | afterEach(() => { |
| 23 | document.body.removeChild(element); |
| 24 | |
| 25 | stars.starProject.restore(); |
| 26 | stars.unstarProject.restore(); |
| 27 | }); |
| 28 | |
| 29 | it('initializes', () => { |
| 30 | assert.instanceOf(element, MrProjectStar); |
| 31 | }); |
| 32 | |
| 33 | it('clicking on star when logged out logs in user', async () => { |
| 34 | element._currentUserName = undefined; |
| 35 | sinon.stub(element, 'login'); |
| 36 | |
| 37 | await element.updateComplete; |
| 38 | |
| 39 | const star = element.shadowRoot.querySelector('button'); |
| 40 | |
| 41 | star.click(); |
| 42 | |
| 43 | sinon.assert.calledOnce(element.login); |
| 44 | }); |
| 45 | |
| 46 | it('star dispatches star request', () => { |
| 47 | element._currentUserName = 'users/1234'; |
| 48 | element.name = 'projects/monorail'; |
| 49 | |
| 50 | element.star(); |
| 51 | |
| 52 | sinon.assert.calledWith(stars.starProject, |
| 53 | 'projects/monorail', 'users/1234'); |
| 54 | }); |
| 55 | |
| 56 | it('unstar dispatches unstar request', () => { |
| 57 | element._currentUserName = 'users/1234'; |
| 58 | element.name = 'projects/monorail'; |
| 59 | |
| 60 | element.unstar(); |
| 61 | |
| 62 | sinon.assert.calledWith(stars.unstarProject, |
| 63 | 'projects/monorail', 'users/1234'); |
| 64 | }); |
| 65 | |
| 66 | describe('isStarred', () => { |
| 67 | beforeEach(() => { |
| 68 | element._stars = { |
| 69 | 'users/1234/projectStars/monorail': |
| 70 | {name: 'users/1234/projectStars/monorail'}, |
| 71 | 'users/5678/projectStars/chromium': |
| 72 | {name: 'users/5678/projectStars/chromium'}, |
| 73 | }; |
| 74 | }); |
| 75 | |
| 76 | it('false when no data', () => { |
| 77 | element._stars = {}; |
| 78 | assert.isFalse(element.isStarred); |
| 79 | }); |
| 80 | |
| 81 | it('false when user is not logged in', () => { |
| 82 | element._currentUserName = ''; |
| 83 | element.name = 'projects/monorail'; |
| 84 | |
| 85 | assert.isFalse(element.isStarred); |
| 86 | }); |
| 87 | |
| 88 | it('false when project is not starred', () => { |
| 89 | element._currentUserName = 'users/1234'; |
| 90 | element.name = 'projects/chromium'; |
| 91 | |
| 92 | assert.isFalse(element.isStarred); |
| 93 | |
| 94 | element._currentUserName = 'users/5678'; |
| 95 | element.name = 'projects/monorail'; |
| 96 | |
| 97 | assert.isFalse(element.isStarred); |
| 98 | }); |
| 99 | |
| 100 | it('true when user has starred project', () => { |
| 101 | element._currentUserName = 'users/1234'; |
| 102 | element.name = 'projects/monorail'; |
| 103 | |
| 104 | assert.isTrue(element.isStarred); |
| 105 | |
| 106 | element._currentUserName = 'users/5678'; |
| 107 | element.name = 'projects/chromium'; |
| 108 | |
| 109 | assert.isTrue(element.isStarred); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe('_starringEnabled', () => { |
| 114 | beforeEach(() => { |
| 115 | element._currentUserName = 'users/1234'; |
| 116 | element.name = 'projects/monorail'; |
| 117 | }); |
| 118 | |
| 119 | it('disabled when user is not logged in', () => { |
| 120 | element._currentUserName = ''; |
| 121 | |
| 122 | assert.isFalse(element._starringEnabled); |
| 123 | }); |
| 124 | |
| 125 | it('disabled when stars are being fetched', () => { |
| 126 | element._fetchingStars = true; |
| 127 | element._starringProjects = {}; |
| 128 | element._unstarringProjects = {}; |
| 129 | |
| 130 | assert.isFalse(element._starringEnabled); |
| 131 | }); |
| 132 | |
| 133 | it('disabled when user is starring project', () => { |
| 134 | element._fetchingStars = false; |
| 135 | element._starringProjects = |
| 136 | {'users/1234/projectStars/monorail': {requesting: true}}; |
| 137 | element._unstarringProjects = {}; |
| 138 | |
| 139 | assert.isFalse(element._starringEnabled); |
| 140 | }); |
| 141 | |
| 142 | it('disabled when user is unstarring project', () => { |
| 143 | element._fetchingStars = false; |
| 144 | element._starringProjects = {}; |
| 145 | element._unstarringProjects = |
| 146 | {'users/1234/projectStars/monorail': {requesting: true}}; |
| 147 | |
| 148 | assert.isFalse(element._starringEnabled); |
| 149 | }); |
| 150 | |
| 151 | it('enabled when user is starring an unrelated project', () => { |
| 152 | element._fetchingStars = false; |
| 153 | element._starringProjects = { |
| 154 | 'users/1234/projectStars/chromium': {requesting: true}, |
| 155 | 'users/1234/projectStars/monorail': {requesting: false}, |
| 156 | }; |
| 157 | element._unstarringProjects = {}; |
| 158 | |
| 159 | assert.isTrue(element._starringEnabled); |
| 160 | }); |
| 161 | |
| 162 | it('enabled when user is unstarring an unrelated project', () => { |
| 163 | element._fetchingStars = false; |
| 164 | element._starringProjects = {}; |
| 165 | element._unstarringProjects = { |
| 166 | 'users/1234/projectStars/chromium': {requesting: true}, |
| 167 | 'users/1234/projectStars/monorail': {requesting: false}, |
| 168 | }; |
| 169 | |
| 170 | assert.isTrue(element._starringEnabled); |
| 171 | }); |
| 172 | |
| 173 | it('enabled when no in-flight requests', () => { |
| 174 | element._fetchingStars = false; |
| 175 | element._starringProjects = {}; |
| 176 | element._unstarringProjects = {}; |
| 177 | |
| 178 | assert.isTrue(element._starringEnabled); |
| 179 | }); |
| 180 | }); |
| 181 | }); |