From 597c14e6d743b2f0ebf58021afd9089677d3eeef Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Thu, 12 Dec 2024 21:23:01 +1100 Subject: [PATCH] Add test --- .../CreatePullRequestsCommandHandlerTests.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/Stack.Tests/Commands/PullRequests/CreatePullRequestsCommandHandlerTests.cs b/src/Stack.Tests/Commands/PullRequests/CreatePullRequestsCommandHandlerTests.cs index e9d7c32..f1bff40 100644 --- a/src/Stack.Tests/Commands/PullRequests/CreatePullRequestsCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/PullRequests/CreatePullRequestsCommandHandlerTests.cs @@ -564,4 +564,67 @@ public async Task WhenAskedWhetherToCreateAPullRequestAsADraft_AndTheAnswerIsYes gitHubOperations.Received().CreatePullRequest("branch-3", "branch-1", "PR Title", Arg.Any(), true); gitHubOperations.Received().CreatePullRequest("branch-5", "branch-3", "PR Title", Arg.Any(), true); } + + [Fact] + public async Task WhenAskedWhetherToEditTheBodyOfAPull_AndTheAnswerIsYes_TheBodyFileIsOpenedInEditor() + { + // Arrange + var gitOperations = Substitute.For(); + var gitHubOperations = Substitute.For(); + var stackConfig = Substitute.For(); + var inputProvider = Substitute.For(); + var outputProvider = Substitute.For(); + var fileOperations = Substitute.For(); + var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, fileOperations, stackConfig); + + var remoteUri = Some.HttpsUri().ToString(); + outputProvider + .WhenForAnyArgs(o => o.Status(Arg.Any(), Arg.Any())) + .Do(ci => ci.ArgAt(1)()); + + gitOperations.GetRemoteUri().Returns(remoteUri); + gitOperations.GetCurrentBranch().Returns("branch-1"); + gitOperations + .GetBranchesThatExistInRemote(Arg.Any()) + .Returns(["branch-1", "branch-3", "branch-5"]); + + gitOperations + .GetBranchesThatExistLocally(Arg.Any()) + .Returns(["branch-1", "branch-3", "branch-5"]); + + var stacks = new List( + [ + new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), + new("Stack2", remoteUri, "branch-2", ["branch-4"]) + ]); + stackConfig.Load().Returns(stacks); + stackConfig + .WhenForAnyArgs(s => s.Save(Arg.Any>())) + .Do(ci => stacks = ci.ArgAt>(0)); + + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); + inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true); + inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true); + inputProvider.Confirm(Questions.CreatePullRequestAsDraft, false).Returns(false); + inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title"); + inputProvider.Confirm(Questions.EditPullRequestBody).Returns(true); + + var prForBranch3 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), true); + gitHubOperations + .CreatePullRequest("branch-3", "branch-1", "PR Title", Arg.Any(), false) + .Returns(prForBranch3); + + var prForBranch5 = new GitHubPullRequest(2, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), true); + gitHubOperations + .CreatePullRequest("branch-5", "branch-3", "PR Title", Arg.Any(), false) + .Returns(prForBranch5); + + // Act + await handler.Handle(CreatePullRequestsCommandInputs.Empty); + + // Assert + gitHubOperations.Received().CreatePullRequest("branch-3", "branch-1", "PR Title", Arg.Any(), false); + gitHubOperations.Received().CreatePullRequest("branch-5", "branch-3", "PR Title", Arg.Any(), false); + gitOperations.Received().OpenFileInEditorAndWaitForClose(Arg.Any()); + } }