blob: 0d7468fd78757c6eabbe2c3436bd9efd03226c7c [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// 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
5import {assert} from 'chai';
6import sinon from 'sinon';
7import {MrKeystrokes} from './mr-keystrokes.js';
8import Mousetrap from 'mousetrap';
9
10import {issueRefToString} from 'shared/convertersV0.js';
11
12/** @type {MrKeystrokes} */
13let element;
14
15describe('mr-keystrokes', () => {
16 beforeEach(() => {
17 element = /** @type {MrKeystrokes} */ (
18 document.createElement('mr-keystrokes'));
19 document.body.appendChild(element);
20
21 element._projectName = 'proj';
22 element.issueId = 11;
23 });
24
25 afterEach(() => {
26 document.body.removeChild(element);
27 });
28
29 it('initializes', () => {
30 assert.instanceOf(element, MrKeystrokes);
31 });
32
33 it('tracks if the issue is currently starring', async () => {
34 await element.updateComplete;
35 assert.isFalse(element._isStarring);
36
37 const issueRefStr = issueRefToString(element._issueRef);
38 element._starringIssues.set(issueRefStr, {requesting: true});
39 assert.isTrue(element._isStarring);
40 });
41
42 it('? and esc open and close dialog', async () => {
43 await element.updateComplete;
44 assert.isFalse(element._opened);
45
46 Mousetrap.trigger('?');
47
48 await element.updateComplete;
49 assert.isTrue(element._opened);
50
51 Mousetrap.trigger('esc');
52
53 await element.updateComplete;
54 assert.isFalse(element._opened);
55 });
56
57 describe('issue detail keys', () => {
58 beforeEach(() => {
59 sinon.stub(element, '_page');
60 sinon.stub(element, '_jumpToEditForm');
61 sinon.stub(element, '_starIssue');
62 });
63
64 it('not bound when _projectName not set', async () => {
65 element._projectName = '';
66 element.issueId = 1;
67
68 await element.updateComplete;
69
70 // Navigation hot keys.
71 Mousetrap.trigger('k');
72 Mousetrap.trigger('j');
73 Mousetrap.trigger('u');
74 sinon.assert.notCalled(element._page);
75
76 // Jump to edit form hot key.
77 Mousetrap.trigger('r');
78 sinon.assert.notCalled(element._jumpToEditForm);
79
80 // Star issue hotkey.
81 Mousetrap.trigger('s');
82 sinon.assert.notCalled(element._starIssue);
83 });
84
85 it('not bound when issueId not set', async () => {
86 element._projectName = 'proj';
87 element.issueId = 0;
88
89 await element.updateComplete;
90
91 // Navigation hot keys.
92 Mousetrap.trigger('k');
93 Mousetrap.trigger('j');
94 Mousetrap.trigger('u');
95 sinon.assert.notCalled(element._page);
96
97 // Jump to edit form hot key.
98 Mousetrap.trigger('r');
99 sinon.assert.notCalled(element._jumpToEditForm);
100
101 // Star issue hotkey.
102 Mousetrap.trigger('s');
103 sinon.assert.notCalled(element._starIssue);
104 });
105
106 it('binds j and k navigation hot keys', async () => {
107 element.queryParams = {q: 'something'};
108
109 await element.updateComplete;
110
111 Mousetrap.trigger('k');
112 sinon.assert.calledWith(element._page,
113 '/p/proj/issues/detail/previous?q=something');
114
115 Mousetrap.trigger('j');
116 sinon.assert.calledWith(element._page,
117 '/p/proj/issues/detail/next?q=something');
118
119 Mousetrap.trigger('u');
120 sinon.assert.calledWith(element._page,
121 '/p/proj/issues/list?q=something&cursor=proj%3A11');
122 });
123
124 it('u key navigates back to issue list wth cursor set', async () => {
125 element.queryParams = {q: 'something'};
126
127 await element.updateComplete;
128
129 Mousetrap.trigger('u');
130 sinon.assert.calledWith(element._page,
131 '/p/proj/issues/list?q=something&cursor=proj%3A11');
132 });
133
134 it('u key navigates back to hotlist when hotlist_id set', async () => {
135 element.queryParams = {hotlist_id: 1234};
136
137 await element.updateComplete;
138
139 Mousetrap.trigger('u');
140 sinon.assert.calledWith(element._page,
141 '/p/proj/issues/detail/list?hotlist_id=1234&cursor=proj%3A11');
142 });
143
144 it('does not star when user does not have permission', async () => {
145 element.queryParams = {q: 'something'};
146 element._issuePermissions = [];
147
148 await element.updateComplete;
149
150 Mousetrap.trigger('s');
151 sinon.assert.notCalled(element._starIssue);
152 });
153
154 it('does star when user has permission', async () => {
155 element.queryParams = {q: 'something'};
156 element._issuePermissions = ['setstar'];
157
158 await element.updateComplete;
159
160 Mousetrap.trigger('s');
161 sinon.assert.calledOnce(element._starIssue);
162 });
163
164 it('does not star when user does not have permission', async () => {
165 element.queryParams = {q: 'something'};
166 element._issuePermissions = [];
167
168 await element.updateComplete;
169
170 Mousetrap.trigger('s');
171 sinon.assert.notCalled(element._starIssue);
172 });
173
174 it('does not jump to edit form when user cannot comment', async () => {
175 element.queryParams = {q: 'something'};
176 element._issuePermissions = [];
177
178 await element.updateComplete;
179
180 Mousetrap.trigger('r');
181 sinon.assert.notCalled(element._jumpToEditForm);
182 });
183
184 it('does jump to edit form when user can comment', async () => {
185 element.queryParams = {q: 'something'};
186 element._issuePermissions = ['addissuecomment'];
187
188 await element.updateComplete;
189
190 Mousetrap.trigger('r');
191 sinon.assert.calledOnce(element._jumpToEditForm);
192 });
193 });
194});