blob: bd433543c075b2940d41cd53de452c01736a84c8 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2020 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6import {MrIssueStar} from './mr-issue-star.js';
7import {issueRefToString} from 'shared/convertersV0.js';
8import sinon from 'sinon';
9
10
11let element;
12
13describe('mr-issue-star', () => {
14 beforeEach(() => {
15 element = document.createElement('mr-issue-star');
16 document.body.appendChild(element);
17 });
18
19 afterEach(() => {
20 document.body.removeChild(element);
21 });
22
23 it('initializes', () => {
24 assert.instanceOf(element, MrIssueStar);
25 });
26
27 it('starring logins user when user is not logged in', async () => {
28 element._currentUserName = undefined;
29 sinon.stub(element, 'login');
30
31 await element.updateComplete;
32
33 const star = element.shadowRoot.querySelector('button');
34
35 star.click();
36
37 sinon.assert.calledOnce(element.login);
38 });
39
40 it('_isStarring true only when issue ref is being starred', async () => {
41 element._starringIssues = new Map([['chromium:22', {requesting: true}]]);
42 element.issueRef = {projectName: 'chromium', localId: 5};
43
44 assert.isFalse(element._isStarring);
45
46 element.issueRef = {projectName: 'chromium', localId: 22};
47
48 assert.isTrue(element._isStarring);
49
50 element._starringIssues = new Map([['chromium:22', {requesting: false}]]);
51
52 assert.isFalse(element._isStarring);
53 });
54
55 it('starring is disabled when _isStarring true', () => {
56 element._currentUserName = 'users/1234';
57 sinon.stub(element, '_isStarring').get(() => true);
58
59 assert.isFalse(element._starringEnabled);
60 });
61
62 it('starring is disabled when _fetchingIsStarred true', () => {
63 element._currentUserName = 'users/1234';
64 element._fetchingIsStarred = true;
65
66 assert.isFalse(element._starringEnabled);
67 });
68
69 it('_starredIssues changes displayed icon', async () => {
70 element.issueRef = {projectName: 'proj', localId: 1};
71
72 element._starredIssues = new Set([issueRefToString(element.issueRef)]);
73
74 await element.updateComplete;
75
76 const star = element.shadowRoot.querySelector('button');
77 assert.equal(star.textContent.trim(), 'star');
78
79 element._starredIssues = new Set();
80
81 await element.updateComplete;
82
83 assert.equal(star.textContent.trim(), 'star_border');
84 });
85});