blob: a0689e1d975f308f26caa4634dfa2f872926901b [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 {fireEvent} from '@testing-library/react';
8
9import {MrBulkApprovalUpdate, NO_APPROVALS_MESSAGE,
10 NO_UPDATES_MESSAGE} from './mr-bulk-approval-update.js';
11import {prpcClient} from 'prpc-client-instance.js';
12
13let element;
14
15describe('mr-bulk-approval-update', () => {
16 beforeEach(() => {
17 element = document.createElement('mr-bulk-approval-update');
18 document.body.appendChild(element);
19
20 sinon.stub(prpcClient, 'call');
21 });
22
23 afterEach(() => {
24 document.body.removeChild(element);
25 prpcClient.call.restore();
26 });
27
28 it('initializes', () => {
29 assert.instanceOf(element, MrBulkApprovalUpdate);
30 });
31
32 it('_computeIssueRefs: missing information', () => {
33 element.projectName = 'chromium';
34 assert.equal(element.issueRefs.length, 0);
35
36 element.projectName = null;
37 element.localIdsStr = '1,2,3,5';
38 assert.equal(element.issueRefs.length, 0);
39
40 element.localIdsStr = null;
41 assert.equal(element.issueRefs.length, 0);
42 });
43
44 it('_computeIssueRefs: normal', () => {
45 const project = 'chromium';
46 element.projectName = project;
47 element.localIdsStr = '1,2,3';
48 assert.deepEqual(element.issueRefs, [
49 {projectName: project, localId: '1'},
50 {projectName: project, localId: '2'},
51 {projectName: project, localId: '3'},
52 ]);
53 });
54
55 it('fetchApprovals: applicable fields exist', async () => {
56 const responseFieldDefs = [
57 {fieldRef: {type: 'INT_TYPE'}},
58 {fieldRef: {type: 'APPROVAL_TYPE'}},
59 {fieldRef: {type: 'APPROVAL_TYPE'}},
60 ];
61 const promise = Promise.resolve({fieldDefs: responseFieldDefs});
62 prpcClient.call.returns(promise);
63
64 sinon.spy(element, 'fetchApprovals');
65
66 await element.updateComplete;
67
68 element.querySelector('.js-showApprovals').click();
69 assert.isTrue(element.fetchApprovals.calledOnce);
70
71 // Wait for promise in fetchApprovals to resolve.
72 await promise;
73
74 assert.deepEqual([
75 {fieldRef: {type: 'APPROVAL_TYPE'}},
76 {fieldRef: {type: 'APPROVAL_TYPE'}},
77 ], element.approvals);
78 assert.equal(null, element.errorMessage);
79 });
80
81 it('fetchApprovals: applicable fields dont exist', async () => {
82 const promise = Promise.resolve({fieldDefs: []});
83 prpcClient.call.returns(promise);
84
85 await element.updateComplete;
86
87 element.querySelector('.js-showApprovals').click();
88
89 await promise;
90
91 assert.equal(element.approvals.length, 0);
92 assert.equal(NO_APPROVALS_MESSAGE, element.errorMessage);
93 });
94
95 it('save: normal', async () => {
96 const promise =
97 Promise.resolve({issueRefs: [{localId: '1'}, {localId: '3'}]});
98 prpcClient.call.returns(promise);
99 const fieldDefs = [
100 {fieldRef: {fieldName: 'Approval-One', type: 'APPROVAL_TYPE'}},
101 {fieldRef: {fieldName: 'Approval-Two', type: 'APPROVAL_TYPE'}},
102 ];
103 element.approvals = fieldDefs;
104 element.projectName = 'chromium';
105 element.localIdsStr = '1,2,3';
106
107 await element.updateComplete;
108
109 fireEvent.change(element.querySelector('#commentText'), {target: {value: 'comment'}});
110 fireEvent.change(element.querySelector('#statusInput'), {target: {value: 'NotApproved'}});
111 element.querySelector('.js-save').click();
112
113 // Wait for promise in save() to resolve.
114 await promise;
115 await element.updateComplete;
116
117 // Assert messages correct
118 assert.equal(
119 true,
120 element.responseMessage.includes(
121 'Updated Approval-One in issues: 1, 3 (2 of 3).'));
122 assert.equal('', element.errorMessage);
123
124 // Assert all inputs not disabled.
125 element.querySelectorAll('input, textarea, select').forEach((input) => {
126 assert.equal(input.disabled, false);
127 });
128
129 // Assert all inputs cleared.
130 element.querySelectorAll('input, textarea').forEach((input) => {
131 assert.equal(input.value, '');
132 });
133 element.querySelectorAll('select').forEach((select) => {
134 assert.equal(select.selectedIndex, 0);
135 });
136
137 // Assert BulkUpdateApprovals correctly called.
138 const expectedMessage = {
139 approvalDelta: {status: 'NOT_APPROVED'},
140 commentContent: 'comment',
141 fieldRef: fieldDefs[0].fieldRef,
142 issueRefs: element.issueRefs,
143 send_email: true,
144 };
145 sinon.assert.calledWith(
146 prpcClient.call,
147 'monorail.Issues',
148 'BulkUpdateApprovals',
149 expectedMessage);
150 });
151
152 it('save: no updates', async () => {
153 const promise = Promise.resolve({issueRefs: []});
154 prpcClient.call.returns(promise);
155 const fieldDefs = [
156 {fieldRef: {fieldName: 'Approval-One', type: 'APPROVAL_TYPE'}},
157 {fieldRef: {fieldName: 'Approval-Two', type: 'APPROVAL_TYPE'}},
158 ];
159 element.approvals = fieldDefs;
160 element.projectName = 'chromium';
161 element.localIdsStr = '1,2,3';
162
163 await element.updateComplete;
164
165 element.querySelector('#commentText').value = 'comment';
166 element.querySelector('#statusInput').value = 'NotApproved';
167 element.querySelector('.js-save').click();
168
169 // Wait for promise in save() to resolve
170 await promise;
171
172 // Assert messages correct.
173 assert.equal('', element.responseMessage);
174 assert.equal(NO_UPDATES_MESSAGE, element.errorMessage);
175
176 // Assert inputs not cleared.
177 assert.equal(element.querySelector('#commentText').value, 'comment');
178 assert.equal(element.querySelector('#statusInput').value, 'NotApproved');
179
180 // Assert inputs not disabled.
181 element.querySelectorAll('input, textarea, select').forEach((input) => {
182 assert.equal(input.disabled, false);
183 });
184 });
185});