Paladox none | 541a970 | 2021-08-18 02:37:52 +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 | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 5 | */ |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 6 | import '../../../test/common-test-setup-karma'; |
| 7 | import './gr-create-repo-dialog'; |
| 8 | import {GrCreateRepoDialog} from './gr-create-repo-dialog'; |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 9 | import { |
| 10 | mockPromise, |
| 11 | queryAndAssert, |
| 12 | stubRestApi, |
| 13 | } from '../../../test/test-utils'; |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 14 | import {BranchName, GroupId, RepoName} from '../../../types/common'; |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 15 | import {GrAutocomplete} from '../../shared/gr-autocomplete/gr-autocomplete'; |
| 16 | import {GrSelect} from '../../shared/gr-select/gr-select'; |
Frank Borden | 2c4b72d | 2022-08-22 13:01:59 +0200 | [diff] [blame^] | 17 | import {fixture, html} from '@open-wc/testing'; |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 18 | |
| 19 | suite('gr-create-repo-dialog tests', () => { |
| 20 | let element: GrCreateRepoDialog; |
| 21 | |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 22 | setup(async () => { |
Frank Borden | dedd671 | 2022-08-22 10:56:11 +0200 | [diff] [blame] | 23 | element = await fixture( |
| 24 | html`<gr-create-repo-dialog></gr-create-repo-dialog>` |
| 25 | ); |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 26 | }); |
| 27 | |
Frank Borden | 535bb91 | 2022-07-15 16:42:20 +0200 | [diff] [blame] | 28 | test('render', () => { |
Frank Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 29 | 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 Borden | 535bb91 | 2022-07-15 16:42:20 +0200 | [diff] [blame] | 83 | </div> |
Frank Borden | 37c7e42 | 2022-08-19 14:55:52 +0200 | [diff] [blame] | 84 | ` |
| 85 | ); |
Frank Borden | 535bb91 | 2022-07-15 16:42:20 +0200 | [diff] [blame] | 86 | }); |
| 87 | |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 88 | test('default values are populated', () => { |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 89 | assert.isTrue( |
| 90 | queryAndAssert<GrSelect>(element, '#initialCommit').bindValue |
| 91 | ); |
| 92 | assert.isFalse(queryAndAssert<GrSelect>(element, '#parentRepo').bindValue); |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 93 | }); |
| 94 | |
| 95 | test('repo created', async () => { |
| 96 | const configInputObj = { |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 97 | name: 'test-repo-new' as RepoName, |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 98 | 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 none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 107 | const promise = mockPromise(); |
| 108 | element.addEventListener('new-repo-name', () => { |
| 109 | promise.resolve(); |
| 110 | }); |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 111 | |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 112 | element.repoConfig = { |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 113 | name: 'test-repo' as RepoName, |
| 114 | create_empty_commit: true, |
| 115 | parent: 'All-Project' as RepoName, |
| 116 | permissions_only: false, |
| 117 | }; |
| 118 | |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 119 | element.repoOwner = 'test'; |
| 120 | element.repoOwnerId = 'testId' as GroupId; |
| 121 | element.defaultBranch = 'main' as BranchName; |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 122 | |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 123 | 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 none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 137 | |
Paladox none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 138 | assert.deepEqual(element.repoConfig, configInputObj); |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 139 | |
| 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 none | c91cd53 | 2021-10-30 00:39:22 +0000 | [diff] [blame] | 148 | |
| 149 | await promise; |
| 150 | |
| 151 | assert.equal(element.repoConfig.name, configInputObj.name); |
| 152 | assert.equal(element.nameChanged, true); |
Paladox none | 541a970 | 2021-08-18 02:37:52 +0000 | [diff] [blame] | 153 | }); |
| 154 | }); |