Ben Rohlfs | 4250087 | 2023-02-27 19:16:38 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2023 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | import '../../test/common-test-setup'; |
| 7 | import {getAppContext} from '../../services/app-context'; |
| 8 | import {ChangeModel, changeModelToken} from '../change/change-model'; |
| 9 | import {assert} from '@open-wc/testing'; |
| 10 | import {testResolver} from '../../test/common-test-setup'; |
| 11 | import {RelatedChangesModel} from './related-changes-model'; |
| 12 | import {configModelToken} from '../config/config-model'; |
| 13 | import {SinonStub} from 'sinon'; |
| 14 | import { |
| 15 | ChangeInfo, |
| 16 | RelatedChangesInfo, |
| 17 | SubmittedTogetherInfo, |
| 18 | } from '../../types/common'; |
| 19 | import {stubRestApi, waitUntilObserved} from '../../test/test-utils'; |
| 20 | import { |
| 21 | createParsedChange, |
| 22 | createRelatedChangesInfo, |
| 23 | createRelatedChangeAndCommitInfo, |
| 24 | createChange, |
Ben Rohlfs | 6e90472 | 2023-02-28 17:30:25 +0100 | [diff] [blame] | 25 | createChangeMessage, |
Ben Rohlfs | 4250087 | 2023-02-27 19:16:38 +0100 | [diff] [blame] | 26 | } from '../../test/test-data-generators'; |
Ben Rohlfs | 6e90472 | 2023-02-28 17:30:25 +0100 | [diff] [blame] | 27 | import {ChangeStatus, ReviewInputTag, TopicName} from '../../api/rest-api'; |
| 28 | import {MessageTag} from '../../constants/constants'; |
Ben Rohlfs | 4250087 | 2023-02-27 19:16:38 +0100 | [diff] [blame] | 29 | |
| 30 | suite('related-changes-model tests', () => { |
| 31 | let model: RelatedChangesModel; |
| 32 | let changeModel: ChangeModel; |
| 33 | |
| 34 | setup(async () => { |
| 35 | changeModel = testResolver(changeModelToken); |
| 36 | model = new RelatedChangesModel( |
| 37 | changeModel, |
| 38 | testResolver(configModelToken), |
| 39 | getAppContext().restApiService |
| 40 | ); |
| 41 | await waitUntilObserved(changeModel.change$, c => c === undefined); |
| 42 | }); |
| 43 | |
| 44 | teardown(() => { |
| 45 | model.finalize(); |
| 46 | }); |
| 47 | |
| 48 | test('register and fetch', async () => { |
| 49 | assert.equal('', ''); |
| 50 | }); |
| 51 | |
| 52 | suite('related changes and hasParent', async () => { |
| 53 | let getRelatedChangesStub: SinonStub; |
| 54 | let getRelatedChangesResponse: RelatedChangesInfo; |
| 55 | let hasParent: boolean | undefined; |
| 56 | |
| 57 | setup(() => { |
| 58 | getRelatedChangesStub = stubRestApi('getRelatedChanges').callsFake(() => |
| 59 | Promise.resolve(getRelatedChangesResponse) |
| 60 | ); |
| 61 | model.hasParent$.subscribe(x => (hasParent = x)); |
| 62 | }); |
| 63 | |
| 64 | test('relatedChanges initially undefined', async () => { |
| 65 | await waitUntilObserved( |
| 66 | model.relatedChanges$, |
| 67 | relatedChanges => relatedChanges === undefined |
| 68 | ); |
| 69 | assert.isFalse(getRelatedChangesStub.called); |
| 70 | assert.isUndefined(hasParent); |
| 71 | }); |
| 72 | |
| 73 | test('relatedChanges loading empty', async () => { |
| 74 | changeModel.updateStateChange({...createParsedChange()}); |
| 75 | |
| 76 | await waitUntilObserved( |
| 77 | model.relatedChanges$, |
| 78 | relatedChanges => relatedChanges?.length === 0 |
| 79 | ); |
| 80 | assert.isTrue(getRelatedChangesStub.calledOnce); |
| 81 | assert.isFalse(hasParent); |
| 82 | }); |
| 83 | |
| 84 | test('relatedChanges loading one change', async () => { |
| 85 | getRelatedChangesResponse = { |
| 86 | ...createRelatedChangesInfo(), |
| 87 | changes: [createRelatedChangeAndCommitInfo()], |
| 88 | }; |
| 89 | changeModel.updateStateChange({...createParsedChange()}); |
| 90 | |
| 91 | await waitUntilObserved( |
| 92 | model.relatedChanges$, |
| 93 | relatedChanges => relatedChanges?.length === 1 |
| 94 | ); |
| 95 | assert.isTrue(getRelatedChangesStub.calledOnce); |
| 96 | assert.isTrue(hasParent); |
| 97 | }); |
| 98 | }); |
| 99 | |
| 100 | suite('loadSubmittedTogether', async () => { |
| 101 | let getChangesSubmittedTogetherStub: SinonStub; |
| 102 | let getChangesSubmittedTogetherResponse: SubmittedTogetherInfo; |
| 103 | |
| 104 | setup(() => { |
| 105 | getChangesSubmittedTogetherStub = stubRestApi( |
| 106 | 'getChangesSubmittedTogether' |
| 107 | ).callsFake(() => Promise.resolve(getChangesSubmittedTogetherResponse)); |
| 108 | }); |
| 109 | |
| 110 | test('submittedTogether initially undefined', async () => { |
| 111 | await waitUntilObserved( |
| 112 | model.submittedTogether$, |
| 113 | submittedTogether => submittedTogether === undefined |
| 114 | ); |
| 115 | assert.isFalse(getChangesSubmittedTogetherStub.called); |
| 116 | }); |
| 117 | |
| 118 | test('submittedTogether emits', async () => { |
| 119 | getChangesSubmittedTogetherResponse = { |
| 120 | changes: [createChange()], |
| 121 | non_visible_changes: 0, |
| 122 | }; |
| 123 | changeModel.updateStateChange({...createParsedChange()}); |
| 124 | |
| 125 | await waitUntilObserved( |
| 126 | model.submittedTogether$, |
| 127 | submittedTogether => submittedTogether?.changes?.length === 1 |
| 128 | ); |
| 129 | assert.isTrue(getChangesSubmittedTogetherStub.calledOnce); |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | suite('loadCherryPicks', async () => { |
| 134 | let getChangeCherryPicksStub: SinonStub; |
| 135 | let getChangeCherryPicksResponse: ChangeInfo[]; |
| 136 | |
| 137 | setup(() => { |
| 138 | getChangeCherryPicksStub = stubRestApi('getChangeCherryPicks').callsFake( |
| 139 | () => Promise.resolve(getChangeCherryPicksResponse) |
| 140 | ); |
| 141 | }); |
| 142 | |
| 143 | test('cherryPicks initially undefined', async () => { |
| 144 | await waitUntilObserved( |
| 145 | model.cherryPicks$, |
| 146 | cherryPicks => cherryPicks === undefined |
| 147 | ); |
| 148 | assert.isFalse(getChangeCherryPicksStub.called); |
| 149 | }); |
| 150 | |
| 151 | test('cherryPicks emits', async () => { |
| 152 | getChangeCherryPicksResponse = [createChange()]; |
| 153 | changeModel.updateStateChange({...createParsedChange()}); |
| 154 | |
| 155 | await waitUntilObserved( |
| 156 | model.cherryPicks$, |
| 157 | cherryPicks => cherryPicks?.length === 1 |
| 158 | ); |
| 159 | assert.isTrue(getChangeCherryPicksStub.calledOnce); |
| 160 | }); |
| 161 | }); |
| 162 | |
| 163 | suite('loadConflictingChanges', async () => { |
| 164 | let getChangeConflictsStub: SinonStub; |
| 165 | let getChangeConflictsResponse: ChangeInfo[]; |
| 166 | |
| 167 | setup(() => { |
| 168 | getChangeConflictsStub = stubRestApi('getChangeConflicts').callsFake(() => |
| 169 | Promise.resolve(getChangeConflictsResponse) |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | test('conflictingChanges initially undefined', async () => { |
| 174 | await waitUntilObserved( |
| 175 | model.conflictingChanges$, |
| 176 | conflictingChanges => conflictingChanges === undefined |
| 177 | ); |
| 178 | assert.isFalse(getChangeConflictsStub.called); |
| 179 | }); |
| 180 | |
| 181 | test('conflictingChanges not loaded for merged changes', async () => { |
| 182 | getChangeConflictsResponse = [createChange()]; |
| 183 | changeModel.updateStateChange({ |
| 184 | ...createParsedChange(), |
| 185 | mergeable: true, |
| 186 | status: ChangeStatus.MERGED, |
| 187 | }); |
| 188 | |
| 189 | await waitUntilObserved( |
| 190 | model.conflictingChanges$, |
| 191 | conflictingChanges => conflictingChanges === undefined |
| 192 | ); |
| 193 | assert.isFalse(getChangeConflictsStub.called); |
| 194 | }); |
| 195 | |
| 196 | test('conflictingChanges emits', async () => { |
| 197 | getChangeConflictsResponse = [createChange()]; |
| 198 | changeModel.updateStateChange({...createParsedChange(), mergeable: true}); |
| 199 | |
| 200 | await waitUntilObserved( |
| 201 | model.conflictingChanges$, |
| 202 | conflictingChanges => conflictingChanges?.length === 1 |
| 203 | ); |
| 204 | assert.isTrue(getChangeConflictsStub.calledOnce); |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | suite('loadSameTopicChanges', async () => { |
| 209 | let getChangesWithSameTopicStub: SinonStub; |
| 210 | let getChangesWithSameTopicResponse: ChangeInfo[]; |
| 211 | |
| 212 | setup(() => { |
| 213 | getChangesWithSameTopicStub = stubRestApi( |
| 214 | 'getChangesWithSameTopic' |
| 215 | ).callsFake(() => Promise.resolve(getChangesWithSameTopicResponse)); |
| 216 | }); |
| 217 | |
| 218 | test('sameTopicChanges initially undefined', async () => { |
| 219 | await waitUntilObserved( |
| 220 | model.sameTopicChanges$, |
| 221 | sameTopicChanges => sameTopicChanges === undefined |
| 222 | ); |
| 223 | assert.isFalse(getChangesWithSameTopicStub.called); |
| 224 | }); |
| 225 | |
| 226 | test('sameTopicChanges emits', async () => { |
| 227 | getChangesWithSameTopicResponse = [createChange()]; |
| 228 | changeModel.updateStateChange({ |
| 229 | ...createParsedChange(), |
| 230 | topic: 'test-topic' as TopicName, |
| 231 | }); |
| 232 | |
| 233 | await waitUntilObserved( |
| 234 | model.sameTopicChanges$, |
| 235 | sameTopicChanges => sameTopicChanges?.length === 1 |
| 236 | ); |
| 237 | assert.isTrue(getChangesWithSameTopicStub.calledOnce); |
| 238 | }); |
| 239 | }); |
Ben Rohlfs | 6e90472 | 2023-02-28 17:30:25 +0100 | [diff] [blame] | 240 | |
| 241 | suite('loadRevertingChanges', async () => { |
| 242 | let getChangeStub: SinonStub; |
| 243 | |
| 244 | setup(() => { |
| 245 | getChangeStub = stubRestApi('getChange').callsFake(() => |
| 246 | Promise.resolve(createChange()) |
| 247 | ); |
| 248 | }); |
| 249 | |
| 250 | test('revertingChanges initially empty', async () => { |
| 251 | await waitUntilObserved( |
| 252 | model.revertingChanges$, |
| 253 | revertingChanges => revertingChanges.length === 0 |
| 254 | ); |
| 255 | assert.isFalse(getChangeStub.called); |
| 256 | }); |
| 257 | |
| 258 | test('revertingChanges empty when change does not contain a revert message', async () => { |
| 259 | changeModel.updateStateChange(createParsedChange()); |
| 260 | await waitUntilObserved( |
| 261 | model.revertingChanges$, |
| 262 | revertingChanges => revertingChanges.length === 0 |
| 263 | ); |
| 264 | assert.isFalse(getChangeStub.called); |
| 265 | }); |
| 266 | |
| 267 | test('revertingChanges emits', async () => { |
| 268 | changeModel.updateStateChange({ |
| 269 | ...createParsedChange(), |
| 270 | messages: [ |
| 271 | { |
| 272 | ...createChangeMessage(), |
Ben Rohlfs | e60974a | 2023-08-08 11:40:08 +0200 | [diff] [blame] | 273 | message: |
| 274 | 'Created a revert of this change as If02ca1cd494579d6bb92a157bf1819e3689cd6b1', |
Ben Rohlfs | 6e90472 | 2023-02-28 17:30:25 +0100 | [diff] [blame] | 275 | tag: MessageTag.TAG_REVERT as ReviewInputTag, |
| 276 | }, |
| 277 | ], |
| 278 | }); |
| 279 | |
| 280 | await waitUntilObserved( |
| 281 | model.revertingChanges$, |
| 282 | revertingChanges => revertingChanges?.length === 1 |
| 283 | ); |
| 284 | assert.isTrue(getChangeStub.calledOnce); |
| 285 | }); |
| 286 | }); |
Ben Rohlfs | 4250087 | 2023-02-27 19:16:38 +0100 | [diff] [blame] | 287 | }); |