blob: aba71e399e53e4fc8cfc53455bb1107b4a42a12f [file] [log] [blame]
Paladox none541a9702021-08-18 02:37:52 +00001/**
2 * @license
Ben Rohlfs94fcbbc2022-05-27 10:45:03 +02003 * Copyright 2017 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
Paladox none541a9702021-08-18 02:37:52 +00005 */
Paladox none541a9702021-08-18 02:37:52 +00006import '../../../test/common-test-setup-karma';
7import './gr-create-repo-dialog';
8import {GrCreateRepoDialog} from './gr-create-repo-dialog';
Paladox nonec91cd532021-10-30 00:39:22 +00009import {
10 mockPromise,
11 queryAndAssert,
12 stubRestApi,
13} from '../../../test/test-utils';
Paladox none541a9702021-08-18 02:37:52 +000014import {BranchName, GroupId, RepoName} from '../../../types/common';
Paladox nonec91cd532021-10-30 00:39:22 +000015import {GrAutocomplete} from '../../shared/gr-autocomplete/gr-autocomplete';
16import {GrSelect} from '../../shared/gr-select/gr-select';
Frank Borden2c4b72d2022-08-22 13:01:59 +020017import {fixture, html} from '@open-wc/testing';
Paladox none541a9702021-08-18 02:37:52 +000018
19suite('gr-create-repo-dialog tests', () => {
20 let element: GrCreateRepoDialog;
21
Paladox nonec91cd532021-10-30 00:39:22 +000022 setup(async () => {
Frank Bordendedd6712022-08-22 10:56:11 +020023 element = await fixture(
24 html`<gr-create-repo-dialog></gr-create-repo-dialog>`
25 );
Paladox none541a9702021-08-18 02:37:52 +000026 });
27
Frank Borden535bb912022-07-15 16:42:20 +020028 test('render', () => {
Frank Borden37c7e422022-08-19 14:55:52 +020029 assert.shadowDom.equal(
30 element,
31 /* HTML */ `
32 <div class="gr-form-styles">
33 <div id="form">
34 <section>
35 <span class="title"> Repository name </span>
36 <iron-input>
37 <input autocomplete="on" id="repoNameInput" />
38 </iron-input>
39 </section>
40 <section>
41 <span class="title"> Default Branch </span>
42 <iron-input>
43 <input autocomplete="off" id="defaultBranchNameInput" />
44 </iron-input>
45 </section>
46 <section>
47 <span class="title"> Rights inherit from </span>
48 <span class="value">
49 <gr-autocomplete id="rightsInheritFromInput"> </gr-autocomplete>
50 </span>
51 </section>
52 <section>
53 <span class="title"> Owner </span>
54 <span class="value">
55 <gr-autocomplete id="ownerInput"> </gr-autocomplete>
56 </span>
57 </section>
58 <section>
59 <span class="title"> Create initial empty commit </span>
60 <span class="value">
61 <gr-select id="initialCommit">
62 <select>
63 <option value="false">False</option>
64 <option value="true">True</option>
65 </select>
66 </gr-select>
67 </span>
68 </section>
69 <section>
70 <span class="title">
71 Only serve as parent for other repositories
72 </span>
73 <span class="value">
74 <gr-select id="parentRepo">
75 <select>
76 <option value="false">False</option>
77 <option value="true">True</option>
78 </select>
79 </gr-select>
80 </span>
81 </section>
82 </div>
Frank Borden535bb912022-07-15 16:42:20 +020083 </div>
Frank Borden37c7e422022-08-19 14:55:52 +020084 `
85 );
Frank Borden535bb912022-07-15 16:42:20 +020086 });
87
Paladox none541a9702021-08-18 02:37:52 +000088 test('default values are populated', () => {
Paladox nonec91cd532021-10-30 00:39:22 +000089 assert.isTrue(
90 queryAndAssert<GrSelect>(element, '#initialCommit').bindValue
91 );
92 assert.isFalse(queryAndAssert<GrSelect>(element, '#parentRepo').bindValue);
Paladox none541a9702021-08-18 02:37:52 +000093 });
94
95 test('repo created', async () => {
96 const configInputObj = {
Paladox nonec91cd532021-10-30 00:39:22 +000097 name: 'test-repo-new' as RepoName,
Paladox none541a9702021-08-18 02:37:52 +000098 create_empty_commit: true,
99 parent: 'All-Project' as RepoName,
100 permissions_only: false,
101 };
102
103 const saveStub = stubRestApi('createRepo').returns(
104 Promise.resolve(new Response())
105 );
106
Paladox nonec91cd532021-10-30 00:39:22 +0000107 const promise = mockPromise();
108 element.addEventListener('new-repo-name', () => {
109 promise.resolve();
110 });
Paladox none541a9702021-08-18 02:37:52 +0000111
Paladox nonec91cd532021-10-30 00:39:22 +0000112 element.repoConfig = {
Paladox none541a9702021-08-18 02:37:52 +0000113 name: 'test-repo' as RepoName,
114 create_empty_commit: true,
115 parent: 'All-Project' as RepoName,
116 permissions_only: false,
117 };
118
Paladox nonec91cd532021-10-30 00:39:22 +0000119 element.repoOwner = 'test';
120 element.repoOwnerId = 'testId' as GroupId;
121 element.defaultBranch = 'main' as BranchName;
Paladox none541a9702021-08-18 02:37:52 +0000122
Paladox nonec91cd532021-10-30 00:39:22 +0000123 const repoNameInput = queryAndAssert<HTMLInputElement>(
124 element,
125 '#repoNameInput'
126 );
127 repoNameInput.value = configInputObj.name;
128 repoNameInput.dispatchEvent(
129 new Event('input', {bubbles: true, composed: true})
130 );
131 queryAndAssert<GrAutocomplete>(element, '#rightsInheritFromInput').value =
132 configInputObj.parent;
133 queryAndAssert<GrSelect>(element, '#initialCommit').bindValue =
134 configInputObj.create_empty_commit;
135 queryAndAssert<GrSelect>(element, '#parentRepo').bindValue =
136 configInputObj.permissions_only;
Paladox none541a9702021-08-18 02:37:52 +0000137
Paladox nonec91cd532021-10-30 00:39:22 +0000138 assert.deepEqual(element.repoConfig, configInputObj);
Paladox none541a9702021-08-18 02:37:52 +0000139
140 await element.handleCreateRepo();
141 assert.isTrue(
142 saveStub.lastCall.calledWithExactly({
143 ...configInputObj,
144 owners: ['testId' as GroupId],
145 branches: ['main' as BranchName],
146 })
147 );
Paladox nonec91cd532021-10-30 00:39:22 +0000148
149 await promise;
150
151 assert.equal(element.repoConfig.name, configInputObj.name);
152 assert.equal(element.nameChanged, true);
Paladox none541a9702021-08-18 02:37:52 +0000153 });
154});