blob: deec717597579a619d564bdc8c4297c7c962f75b [file] [log] [blame]
Paladox nonedd01d572021-11-21 19:13:53 +00001/**
2 * @license
Ben Rohlfs94fcbbc2022-05-27 10:45:03 +02003 * Copyright 2017 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
Paladox nonedd01d572021-11-21 19:13:53 +00005 */
Kamil Musinac4c1882024-10-25 12:07:32 +02006import * as sinon from 'sinon';
Frank Bordenbe9451a2022-09-12 15:44:29 +02007import '../../../test/common-test-setup';
Frank Borden7b24f732022-09-12 14:29:32 +02008import './gr-repo-commands';
Paladox nonedd01d572021-11-21 19:13:53 +00009import {GrRepoCommands} from './gr-repo-commands';
Paladox nonedd01d572021-11-21 19:13:53 +000010import {
11 addListenerForTest,
12 mockPromise,
13 queryAndAssert,
14 stubRestApi,
15} from '../../../test/test-utils';
Paladox nonedd01d572021-11-21 19:13:53 +000016import {GrDialog} from '../../shared/gr-dialog/gr-dialog';
Ben Rohlfsa36fa832023-02-18 14:55:02 +010017import {PageErrorEvent} from '../../../types/events';
Paladox nonedd01d572021-11-21 19:13:53 +000018import {RepoName} from '../../../types/common';
19import {GrButton} from '../../shared/gr-button/gr-button';
Frank Bordene1ba8212022-08-29 15:20:01 +020020import {fixture, html, assert} from '@open-wc/testing';
Paladox nonedd01d572021-11-21 19:13:53 +000021
22suite('gr-repo-commands tests', () => {
23 let element: GrRepoCommands;
Paladox nonedd01d572021-11-21 19:13:53 +000024 let repoStub: sinon.SinonStub;
25
Paladox none357b1792021-11-21 19:52:09 +000026 setup(async () => {
Frank Bordendedd6712022-08-22 10:56:11 +020027 element = await fixture(html`<gr-repo-commands></gr-repo-commands>`);
Paladox nonedd01d572021-11-21 19:13:53 +000028 // Note that this probably does not achieve what it is supposed to, because
29 // getProjectConfig() is called as soon as the element is attached, so
30 // stubbing it here has not effect anymore.
31 repoStub = stubRestApi('getProjectConfig').returns(
32 Promise.resolve(undefined)
33 );
34 });
35
Frank Borden535bb912022-07-15 16:42:20 +020036 test('render', () => {
Frank Borden37c7e422022-08-19 14:55:52 +020037 assert.shadowDom.equal(
38 element,
39 /* HTML */ `
40 <div class="gr-form-styles main read-only">
41 <h1 class="heading-1" id="Title">Repository Commands</h1>
42 <div class="loading" id="loading">Loading...</div>
43 <div class="loading" id="loadedContent">
Frank Borden37c7e422022-08-19 14:55:52 +020044 <div id="form">
Ben Rohlfs0000aea2022-08-26 12:55:06 +020045 <h2 class="heading-2">Create change</h2>
46 <div>
47 <p>
48 Creates an empty work-in-progress change that can be used to
49 edit files online and send the modifications for review.
50 </p>
51 </div>
52 <div>
53 <gr-button aria-disabled="false" role="button" tabindex="0">
54 Create change
55 </gr-button>
56 </div>
57 <h2 class="heading-2">Edit repo config</h2>
58 <div>
59 <p>
60 Creates a work-in-progress change that allows to edit the
61 <code> project.config </code>
62 file in the
63 <code> refs/meta/config </code>
64 branch and send the modifications for review.
65 </p>
66 </div>
67 <div>
68 <gr-button
69 aria-disabled="false"
70 id="editRepoConfig"
71 role="button"
72 tabindex="0"
73 >
74 Edit repo config
75 </gr-button>
76 </div>
Frank Borden37c7e422022-08-19 14:55:52 +020077 <gr-endpoint-decorator name="repo-command">
78 <gr-endpoint-param name="config"> </gr-endpoint-param>
79 <gr-endpoint-param name="repoName"> </gr-endpoint-param>
80 </gr-endpoint-decorator>
81 </div>
Frank Borden535bb912022-07-15 16:42:20 +020082 </div>
83 </div>
Dhruv Srivastava31fa3112022-10-27 11:21:17 +020084 <dialog id="createChangeModal" tabindex="-1">
Frank Borden37c7e422022-08-19 14:55:52 +020085 <gr-dialog
86 confirm-label="Create"
87 disabled=""
88 id="createChangeDialog"
89 role="dialog"
90 >
91 <div class="header" slot="header">Create Change</div>
92 <div class="main" slot="main">
93 <gr-create-change-dialog id="createNewChangeModal">
94 </gr-create-change-dialog>
95 </div>
96 </gr-dialog>
Dhruv Srivastava31fa3112022-10-27 11:21:17 +020097 </dialog>
Ben Rohlfsc35ed182022-11-25 12:02:52 +000098 <gr-create-file-edit-dialog id="createFileEditDialog">
99 </gr-create-file-edit-dialog>
Ben Rohlfs0000aea2022-08-26 12:55:06 +0200100 `,
101 {ignoreTags: ['p']}
Frank Borden37c7e422022-08-19 14:55:52 +0200102 );
Frank Borden535bb912022-07-15 16:42:20 +0200103 });
104
Paladox nonedd01d572021-11-21 19:13:53 +0000105 suite('create new change dialog', () => {
Paladox none357b1792021-11-21 19:52:09 +0000106 test('createNewChange opens modal', () => {
Paladox nonedd01d572021-11-21 19:13:53 +0000107 const openStub = sinon.stub(
Dhruv Srivastava31fa3112022-10-27 11:21:17 +0200108 queryAndAssert<HTMLDialogElement>(element, '#createChangeModal'),
109 'showModal'
Paladox nonedd01d572021-11-21 19:13:53 +0000110 );
Paladox none357b1792021-11-21 19:52:09 +0000111 element.createNewChange();
Paladox nonedd01d572021-11-21 19:13:53 +0000112 assert.isTrue(openStub.called);
113 });
114
Paladox none357b1792021-11-21 19:52:09 +0000115 test('handleCreateChange called when confirm fired', () => {
116 const handleCreateChangeStub = sinon.stub(element, 'handleCreateChange');
Paladox nonedd01d572021-11-21 19:13:53 +0000117 queryAndAssert<GrDialog>(element, '#createChangeDialog').dispatchEvent(
118 new CustomEvent('confirm', {
119 composed: true,
120 bubbles: true,
121 })
122 );
123 assert.isTrue(handleCreateChangeStub.called);
124 });
125
Paladox none357b1792021-11-21 19:52:09 +0000126 test('handleCloseCreateChange called when cancel fired', () => {
Paladox nonedd01d572021-11-21 19:13:53 +0000127 const handleCloseCreateChangeStub = sinon.stub(
128 element,
Paladox none357b1792021-11-21 19:52:09 +0000129 'handleCloseCreateChange'
Paladox nonedd01d572021-11-21 19:13:53 +0000130 );
131 queryAndAssert<GrDialog>(element, '#createChangeDialog').dispatchEvent(
132 new CustomEvent('cancel', {
133 composed: true,
134 bubbles: true,
135 })
136 );
137 assert.isTrue(handleCloseCreateChangeStub.called);
138 });
139 });
140
141 suite('edit repo config', () => {
142 let createChangeStub: sinon.SinonStub;
Paladox nonedd01d572021-11-21 19:13:53 +0000143 let handleSpy: sinon.SinonSpy;
144 let alertStub: sinon.SinonStub;
145
146 setup(() => {
147 createChangeStub = stubRestApi('createChange');
Paladox none357b1792021-11-21 19:52:09 +0000148 handleSpy = sinon.spy(element, 'handleEditRepoConfig');
Paladox nonedd01d572021-11-21 19:13:53 +0000149 alertStub = sinon.stub();
150 element.repo = 'test' as RepoName;
Ben Rohlfsa36fa832023-02-18 14:55:02 +0100151 element.addEventListener('show-alert', alertStub);
Paladox nonedd01d572021-11-21 19:13:53 +0000152 });
153
Paladox none357b1792021-11-21 19:52:09 +0000154 test('successful creation of change', async () => {
Paladox nonedd01d572021-11-21 19:13:53 +0000155 const change = {_number: '1'};
156 createChangeStub.returns(Promise.resolve(change));
Frank Borden6919f442022-08-29 16:35:24 +0200157 queryAndAssert<GrButton>(element, '#editRepoConfig').click();
Paladox none357b1792021-11-21 19:52:09 +0000158 await element.updateComplete;
Paladox nonedd01d572021-11-21 19:13:53 +0000159 assert.isTrue(
160 queryAndAssert<GrButton>(element, '#editRepoConfig').loading
161 );
Paladox nonedd01d572021-11-21 19:13:53 +0000162
Paladox none357b1792021-11-21 19:52:09 +0000163 await handleSpy.lastCall.returnValue;
164 await element.updateComplete;
165
166 assert.isTrue(alertStub.called);
167 assert.equal(
168 alertStub.lastCall.args[0].detail.message,
169 'Navigating to change'
170 );
Paladox none357b1792021-11-21 19:52:09 +0000171 assert.isFalse(
172 queryAndAssert<GrButton>(element, '#editRepoConfig').loading
173 );
Paladox nonedd01d572021-11-21 19:13:53 +0000174 });
175
Paladox none357b1792021-11-21 19:52:09 +0000176 test('unsuccessful creation of change', async () => {
Paladox nonedd01d572021-11-21 19:13:53 +0000177 createChangeStub.returns(Promise.resolve(null));
Frank Borden6919f442022-08-29 16:35:24 +0200178 queryAndAssert<GrButton>(element, '#editRepoConfig').click();
Paladox none357b1792021-11-21 19:52:09 +0000179 await element.updateComplete;
Paladox nonedd01d572021-11-21 19:13:53 +0000180 assert.isTrue(
181 queryAndAssert<GrButton>(element, '#editRepoConfig').loading
182 );
Paladox nonedd01d572021-11-21 19:13:53 +0000183
Paladox none357b1792021-11-21 19:52:09 +0000184 await handleSpy.lastCall.returnValue;
185 await element.updateComplete;
186
187 assert.isTrue(alertStub.called);
188 assert.equal(
189 alertStub.lastCall.args[0].detail.message,
190 'Failed to create change.'
191 );
Paladox none357b1792021-11-21 19:52:09 +0000192 assert.isFalse(
193 queryAndAssert<GrButton>(element, '#editRepoConfig').loading
194 );
Paladox nonedd01d572021-11-21 19:13:53 +0000195 });
196 });
197
198 suite('404', () => {
199 test('fires page-error', async () => {
200 repoStub.restore();
201
202 element.repo = 'test' as RepoName;
203
204 const response = {status: 404} as Response;
205 stubRestApi('getProjectConfig').callsFake((_repo, errFn) => {
206 if (errFn !== undefined) {
207 errFn(response);
208 }
209 return Promise.resolve(undefined);
210 });
211
Paladox none357b1792021-11-21 19:52:09 +0000212 await element.updateComplete;
Paladox nonedd01d572021-11-21 19:13:53 +0000213 const promise = mockPromise();
214 addListenerForTest(document, 'page-error', e => {
215 assert.deepEqual((e as PageErrorEvent).detail.response, response);
216 promise.resolve();
217 });
218
Paladox none357b1792021-11-21 19:52:09 +0000219 element.loadRepo();
Paladox nonedd01d572021-11-21 19:13:53 +0000220 await promise;
221 });
222 });
223});