Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 1 | /** |
| 2 | * @license |
Ben Rohlfs | 94fcbbc | 2022-05-27 10:45:03 +0200 | [diff] [blame] | 3 | * Copyright 2017 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 5 | */ |
Kamil Musin | ac4c188 | 2024-10-25 12:07:32 +0200 | [diff] [blame^] | 6 | import * as sinon from 'sinon'; |
Frank Borden | be9451a | 2022-09-12 15:44:29 +0200 | [diff] [blame] | 7 | import '../../../test/common-test-setup'; |
Frank Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 8 | import './gr-repo-commands'; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 9 | import {GrRepoCommands} from './gr-repo-commands'; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 10 | import { |
| 11 | addListenerForTest, |
| 12 | mockPromise, |
| 13 | queryAndAssert, |
| 14 | stubRestApi, |
| 15 | } from '../../../test/test-utils'; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 16 | import {GrDialog} from '../../shared/gr-dialog/gr-dialog'; |
Ben Rohlfs | a36fa83 | 2023-02-18 14:55:02 +0100 | [diff] [blame] | 17 | import {PageErrorEvent} from '../../../types/events'; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 18 | import {RepoName} from '../../../types/common'; |
| 19 | import {GrButton} from '../../shared/gr-button/gr-button'; |
Frank Borden | e1ba821 | 2022-08-29 15:20:01 +0200 | [diff] [blame] | 20 | import {fixture, html, assert} from '@open-wc/testing'; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 21 | |
| 22 | suite('gr-repo-commands tests', () => { |
| 23 | let element: GrRepoCommands; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 24 | let repoStub: sinon.SinonStub; |
| 25 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 26 | setup(async () => { |
Frank Borden | dedd671 | 2022-08-22 10:56:11 +0200 | [diff] [blame] | 27 | element = await fixture(html`<gr-repo-commands></gr-repo-commands>`); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 28 | // 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 Borden | 535bb91 | 2022-07-15 16:42:20 +0200 | [diff] [blame] | 36 | test('render', () => { |
Frank Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 37 | 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 Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 44 | <div id="form"> |
Ben Rohlfs | 0000aea | 2022-08-26 12:55:06 +0200 | [diff] [blame] | 45 | <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 Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 77 | <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 Borden | 535bb91 | 2022-07-15 16:42:20 +0200 | [diff] [blame] | 82 | </div> |
| 83 | </div> |
Dhruv Srivastava | 31fa311 | 2022-10-27 11:21:17 +0200 | [diff] [blame] | 84 | <dialog id="createChangeModal" tabindex="-1"> |
Frank Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 85 | <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 Srivastava | 31fa311 | 2022-10-27 11:21:17 +0200 | [diff] [blame] | 97 | </dialog> |
Ben Rohlfs | c35ed18 | 2022-11-25 12:02:52 +0000 | [diff] [blame] | 98 | <gr-create-file-edit-dialog id="createFileEditDialog"> |
| 99 | </gr-create-file-edit-dialog> |
Ben Rohlfs | 0000aea | 2022-08-26 12:55:06 +0200 | [diff] [blame] | 100 | `, |
| 101 | {ignoreTags: ['p']} |
Frank Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 102 | ); |
Frank Borden | 535bb91 | 2022-07-15 16:42:20 +0200 | [diff] [blame] | 103 | }); |
| 104 | |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 105 | suite('create new change dialog', () => { |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 106 | test('createNewChange opens modal', () => { |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 107 | const openStub = sinon.stub( |
Dhruv Srivastava | 31fa311 | 2022-10-27 11:21:17 +0200 | [diff] [blame] | 108 | queryAndAssert<HTMLDialogElement>(element, '#createChangeModal'), |
| 109 | 'showModal' |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 110 | ); |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 111 | element.createNewChange(); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 112 | assert.isTrue(openStub.called); |
| 113 | }); |
| 114 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 115 | test('handleCreateChange called when confirm fired', () => { |
| 116 | const handleCreateChangeStub = sinon.stub(element, 'handleCreateChange'); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 117 | 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 none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 126 | test('handleCloseCreateChange called when cancel fired', () => { |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 127 | const handleCloseCreateChangeStub = sinon.stub( |
| 128 | element, |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 129 | 'handleCloseCreateChange' |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 130 | ); |
| 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 none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 143 | let handleSpy: sinon.SinonSpy; |
| 144 | let alertStub: sinon.SinonStub; |
| 145 | |
| 146 | setup(() => { |
| 147 | createChangeStub = stubRestApi('createChange'); |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 148 | handleSpy = sinon.spy(element, 'handleEditRepoConfig'); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 149 | alertStub = sinon.stub(); |
| 150 | element.repo = 'test' as RepoName; |
Ben Rohlfs | a36fa83 | 2023-02-18 14:55:02 +0100 | [diff] [blame] | 151 | element.addEventListener('show-alert', alertStub); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 152 | }); |
| 153 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 154 | test('successful creation of change', async () => { |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 155 | const change = {_number: '1'}; |
| 156 | createChangeStub.returns(Promise.resolve(change)); |
Frank Borden | 6919f44 | 2022-08-29 16:35:24 +0200 | [diff] [blame] | 157 | queryAndAssert<GrButton>(element, '#editRepoConfig').click(); |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 158 | await element.updateComplete; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 159 | assert.isTrue( |
| 160 | queryAndAssert<GrButton>(element, '#editRepoConfig').loading |
| 161 | ); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 162 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 163 | 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 none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 171 | assert.isFalse( |
| 172 | queryAndAssert<GrButton>(element, '#editRepoConfig').loading |
| 173 | ); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 174 | }); |
| 175 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 176 | test('unsuccessful creation of change', async () => { |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 177 | createChangeStub.returns(Promise.resolve(null)); |
Frank Borden | 6919f44 | 2022-08-29 16:35:24 +0200 | [diff] [blame] | 178 | queryAndAssert<GrButton>(element, '#editRepoConfig').click(); |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 179 | await element.updateComplete; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 180 | assert.isTrue( |
| 181 | queryAndAssert<GrButton>(element, '#editRepoConfig').loading |
| 182 | ); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 183 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 184 | 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 none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 192 | assert.isFalse( |
| 193 | queryAndAssert<GrButton>(element, '#editRepoConfig').loading |
| 194 | ); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 195 | }); |
| 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 none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 212 | await element.updateComplete; |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 213 | const promise = mockPromise(); |
| 214 | addListenerForTest(document, 'page-error', e => { |
| 215 | assert.deepEqual((e as PageErrorEvent).detail.response, response); |
| 216 | promise.resolve(); |
| 217 | }); |
| 218 | |
Paladox none | 357b179 | 2021-11-21 19:52:09 +0000 | [diff] [blame] | 219 | element.loadRepo(); |
Paladox none | dd01d57 | 2021-11-21 19:13:53 +0000 | [diff] [blame] | 220 | await promise; |
| 221 | }); |
| 222 | }); |
| 223 | }); |