Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 6297f67

Browse files
committed
Added PullRequestUserReviewsViewModel tests.
And refactored the `PullRequestUserReviewsViewModel` a little bit to remove stuff that is no longer necessary.
1 parent d5ac99a commit 6297f67

File tree

5 files changed

+256
-84
lines changed

5 files changed

+256
-84
lines changed

src/GitHub.App/ViewModels/GitHubPane/PullRequestReviewViewModel.cs

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Reactive.Linq;
53
using GitHub.Extensions;
64
using GitHub.Logging;
75
using GitHub.Models;
86
using GitHub.Services;
7+
using ReactiveUI;
98
using Serilog;
109

1110
namespace GitHub.ViewModels.GitHubPane
@@ -19,44 +18,38 @@ public class PullRequestReviewViewModel : ViewModelBase, IPullRequestReviewViewM
1918

2019
readonly IPullRequestEditorService editorService;
2120
readonly IPullRequestSession session;
21+
bool isExpanded;
2222

2323
/// <summary>
2424
/// Initializes a new instance of the <see cref="PullRequestReviewViewModel"/> class.
2525
/// </summary>
26-
/// <param name="localRepository">The local repository.</param>
27-
/// <param name="owner">The pull request's repository owner.</param>
26+
/// <param name="editorService">The pull request editor service.</param>
27+
/// <param name="session">The pull request session.</param>
2828
/// <param name="pullRequest">The pull request model.</param>
29-
/// <param name="pullRequestReviewId">The pull request review ID.</param>
29+
/// <param name="model">The pull request review model.</param>
3030
public PullRequestReviewViewModel(
3131
IPullRequestEditorService editorService,
3232
IPullRequestSession session,
33-
ILocalRepositoryModel localRepository,
34-
string owner,
3533
IPullRequestModel pullRequest,
36-
long pullRequestReviewId)
34+
IPullRequestReviewModel model)
3735
{
3836
Guard.ArgumentNotNull(editorService, nameof(editorService));
3937
Guard.ArgumentNotNull(session, nameof(session));
40-
Guard.ArgumentNotNull(localRepository, nameof(localRepository));
41-
Guard.ArgumentNotNull(owner, nameof(owner));
42-
Guard.ArgumentNotNull(pullRequest, nameof(pullRequest));
38+
Guard.ArgumentNotNull(model, nameof(model));
4339

4440
this.editorService = editorService;
4541
this.session = session;
4642

47-
LocalRepository = localRepository;
48-
RemoteRepositoryOwner = owner;
49-
Model = GetModel(pullRequest, pullRequestReviewId);
50-
PullRequestModel = pullRequest;
43+
Model = model;
5144
Body = string.IsNullOrWhiteSpace(Model.Body) ? null : Model.Body;
5245
StateDisplay = ToString(Model.State);
5346

5447
var comments = new List<IPullRequestReviewFileCommentViewModel>();
5548
var outdated = new List<IPullRequestReviewFileCommentViewModel>();
5649

57-
foreach (var comment in PullRequestModel.ReviewComments)
50+
foreach (var comment in pullRequest.ReviewComments)
5851
{
59-
if (comment.PullRequestReviewId == pullRequestReviewId)
52+
if (comment.PullRequestReviewId == model.Id)
6053
{
6154
var vm = new PullRequestReviewFileCommentViewModel(
6255
editorService,
@@ -76,29 +69,23 @@ public PullRequestReviewViewModel(
7669
HasDetails = Body != null ||
7770
FileComments.Count > 0 ||
7871
OutdatedFileComments.Count > 0;
79-
IsExpanded = HasDetails && CalculateIsLatest(pullRequest, Model);
8072
}
8173

82-
/// <inheritdoc/>
83-
public ILocalRepositoryModel LocalRepository { get; private set; }
84-
85-
/// <inheritdoc/>
86-
public string RemoteRepositoryOwner { get; private set; }
87-
8874
/// <inheritdoc/>
8975
public IPullRequestReviewModel Model { get; }
9076

91-
/// <inheritdoc/>
92-
public IPullRequestModel PullRequestModel { get; }
93-
9477
/// <inheritdoc/>
9578
public string Body { get; }
9679

9780
/// <inheritdoc/>
9881
public string StateDisplay { get; }
9982

10083
/// <inheritdoc/>
101-
public bool IsExpanded { get; }
84+
public bool IsExpanded
85+
{
86+
get { return isExpanded; }
87+
set { this.RaiseAndSetIfChanged(ref isExpanded, value); }
88+
}
10289

10390
/// <inheritdoc/>
10491
public bool HasDetails { get; }
@@ -109,26 +96,6 @@ public PullRequestReviewViewModel(
10996
/// <inheritdoc/>
11097
public IReadOnlyList<IPullRequestReviewFileCommentViewModel> OutdatedFileComments { get; }
11198

112-
static bool CalculateIsLatest(IPullRequestModel pullRequest, IPullRequestReviewModel model)
113-
{
114-
return !pullRequest.Reviews.Any(x =>
115-
x.User.Login == model.User.Login &&
116-
x.SubmittedAt > model.SubmittedAt);
117-
}
118-
119-
static IPullRequestReviewModel GetModel(IPullRequestModel pullRequest, long pullRequestReviewId)
120-
{
121-
var result = pullRequest.Reviews.FirstOrDefault(x => x.Id == pullRequestReviewId);
122-
123-
if (result == null)
124-
{
125-
throw new KeyNotFoundException(
126-
$"Unable to find review {pullRequestReviewId} in pull request #{pullRequest.Number}");
127-
}
128-
129-
return result;
130-
}
131-
13299
static string ToString(PullRequestReviewState state)
133100
{
134101
switch (state)

src/GitHub.App/ViewModels/GitHubPane/PullRequestUserReviewsViewModel.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,28 @@ public override async Task Refresh()
139139
}
140140

141141
/// <inheritdoc/>
142-
public async Task Load(IAccount user, IPullRequestModel pullRequest)
142+
async Task Load(IAccount user, IPullRequestModel pullRequest)
143143
{
144+
IsBusy = true;
145+
144146
try
145147
{
146148
session = await sessionManager.GetSession(pullRequest);
147149
User = user;
148150
PullRequestTitle = pullRequest.Title;
149151

150152
var reviews = new List<IPullRequestReviewViewModel>();
153+
var isFirst = true;
151154

152155
foreach (var review in pullRequest.Reviews.OrderByDescending(x => x.SubmittedAt))
153156
{
154-
if (review.User.Login == user.Login)
157+
if (review.User.Login == user.Login &&
158+
review.State != PullRequestReviewState.Pending)
155159
{
156-
var vm = new PullRequestReviewViewModel(
157-
editorService,
158-
session,
159-
LocalRepository,
160-
RemoteRepositoryOwner,
161-
pullRequest,
162-
review.Id);
160+
var vm = new PullRequestReviewViewModel(editorService, session, pullRequest, review);
161+
vm.IsExpanded = isFirst;
163162
reviews.Add(vm);
163+
isFirst = false;
164164
}
165165
}
166166

src/GitHub.Exports.Reactive/ViewModels/GitHubPane/IPullRequestReviewViewModel.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,11 @@ namespace GitHub.ViewModels.GitHubPane
99
/// </summary>
1010
public interface IPullRequestReviewViewModel : IViewModel
1111
{
12-
/// <summary>
13-
/// Gets the local repository.
14-
/// </summary>
15-
ILocalRepositoryModel LocalRepository { get; }
16-
17-
/// <summary>
18-
/// Gets the owner of the remote repository that contains the pull request.
19-
/// </summary>
20-
/// <remarks>
21-
/// The remote repository may be different from the local repository if the local
22-
/// repository is a fork and the user is viewing pull requests from the parent repository.
23-
/// </remarks>
24-
string RemoteRepositoryOwner { get; }
25-
2612
/// <summary>
2713
/// Gets the underlying pull request review model.
2814
/// </summary>
2915
IPullRequestReviewModel Model { get; }
3016

31-
/// <summary>
32-
/// Gets the underlying pull request model.
33-
/// </summary>
34-
IPullRequestModel PullRequestModel { get; }
35-
3617
/// <summary>
3718
/// Gets the body of the review.
3819
/// </summary>

src/GitHub.Exports.Reactive/ViewModels/GitHubPane/IPullRequestUserReviewsViewModel.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,5 @@ Task InitializeAsync(
6565
string repo,
6666
int pullRequestNumber,
6767
string login);
68-
69-
/// <summary>
70-
/// Loads the view model from pre-loaded models.
71-
/// </summary>
72-
/// <param name="user">The user model.</param>
73-
/// <param name="pullRequest">The pull request model.</param>
74-
/// <returns></returns>
75-
Task Load(IAccount user, IPullRequestModel pullRequest);
7668
}
7769
}

0 commit comments

Comments
 (0)