Skip to content

Commit

Permalink
Merge pull request #46 from teesofttech/fest/unit-test-subscribed-com…
Browse files Browse the repository at this point in the history
…ment-update

Created a setup method for all the comment test
  • Loading branch information
thisisnabi authored Sep 7, 2024
2 parents 5c96abb + df040bf commit 232ae4e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 50 deletions.
104 changes: 55 additions & 49 deletions tests/Blogger.UnitTests/Domain/CommentAggregateTests/CommentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,72 @@ namespace Blogger.UnitTests.Domain.CommentAggregateTests;

public class CommentTests
{
#region [Tests]

[Fact]
public void Create_ShouldInitializeCorrectly()
{
// Arrange
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
var content = "This is a test comment.";
var approveLink = ApproveLink.Create("approve-link", DateTime.UtcNow.AddDays(30));

// Act
var comment = Comment.Create(articleId, client, content, approveLink);
var comment = SetupCommentData("approve-link", content);

// Assert
comment.Should().NotBeNull();
comment.ArticleId.Should().Be(articleId);
comment.Client.Should().Be(client);
comment.Content.Should().Be(content);
comment.CreatedOnUtc.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
comment.IsApproved.Should().BeFalse();
comment.ApproveLink.Should().Be(approveLink);
comment.Replies.Should().BeEmpty();
comment.commentResponse.ArticleId.Should().Be(articleId);
comment.commentResponse.Client.Should().Be(comment.client);
comment.commentResponse.Content.Should().Be(content);
comment.commentResponse.CreatedOnUtc.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
comment.commentResponse.IsApproved.Should().BeFalse();
comment.commentResponse.ApproveLink.Should().Be(comment.approveLink);
comment.commentResponse.Replies.Should().BeEmpty();
}

[Fact]
public void Approve_ShouldSetIsApprovedToTrue()
{
// Arrange
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
// Arrange
var content = "This is a test comment.";
var approveLink = ApproveLink.Create("approve-link", DateTime.UtcNow.AddDays(30));
var comment = SetupCommentData("approve-link", content);

var comment = Comment.Create(articleId, client, content, approveLink);
// Act
comment.Approve();
comment.commentResponse.Approve();

// Assert
comment.IsApproved.Should().BeTrue();
comment.commentResponse.IsApproved.Should().BeTrue();
}

[Fact]
public void ReplyComment_ShouldAddReplyToApprovedComment()
{
// Arrange
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
var content = "This is a test comment.";
var approveLink = ApproveLink.Create("approve-link", DateTime.UtcNow.AddDays(30));
var comment = Comment.Create(articleId, client, content, approveLink);
var comment = SetupCommentData("reply-approve-link", content);

// Act
comment.Approve();
var reply = comment.ReplyComment(client, content, approveLink);
comment.commentResponse.Approve();
var reply = comment.commentResponse.ReplyComment(comment.client, content, comment.approveLink);

// Assert
reply.Should().NotBeNull();
reply.Content.Should().Be(content);
reply.Client.Should().Be(client);
reply.ApproveLink.Should().Be(approveLink);
comment.Replies.Should().ContainSingle();
comment.Replies.First().Should().Be(reply);
reply.Client.Should().Be(comment.client);
reply.ApproveLink.Should().Be(comment.approveLink);
comment.commentResponse.Replies.Should().ContainSingle();
comment.commentResponse.Replies.First().Should().Be(reply);
}

[Fact]
public void ReplyComment_ShouldThrowExceptionIfCommentNotApproved()
{
// Arrange
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
var content = "This is a test comment.";
var approveLink = ApproveLink.Create("approve-link", DateTime.UtcNow.AddDays(30));
var comment = Comment.Create(articleId, client, content, approveLink);
string content = "This is a reply.";
var comment = SetupCommentData("approve-link", content);

// Act
Action act = () => comment.ReplyComment(client, "This is a reply.", approveLink);
Action act = () => comment.commentResponse.ReplyComment(comment.client, content, comment.approveLink);

// Assert
act.Should().Throw<UnapprovedCommentException>();
Expand All @@ -89,16 +80,13 @@ public void ReplyComment_ShouldThrowExceptionIfCommentNotApproved()
public void ApproveReply_ShouldSetReplyAsApproved()
{
// Arrange
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
var content = "This is a test comment.";
var approveLink = ApproveLink.Create("reply-approve-link", DateTime.UtcNow.AddDays(30));
var comment = Comment.Create(articleId, client, content, approveLink);
string content = "This is a reply.";
var comment = SetupCommentData("reply-approve-link", content);

// Act
comment.Approve();
var reply = comment.ReplyComment(client, "This is a reply.", approveLink);
var replyId = comment.ApproveReply("reply-approve-link");
comment.commentResponse.Approve();
var reply = comment.commentResponse.ReplyComment(comment.client, content, comment.approveLink);
var replyId = comment.commentResponse.ApproveReply("reply-approve-link");

// Assert
replyId.Should().Be(reply.Id);
Expand All @@ -109,18 +97,36 @@ public void ApproveReply_ShouldSetReplyAsApproved()
public void ApproveReply_ShouldThrowExceptionIfLinkIsInvalid()
{
// Arrange
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
var content = "This is a test comment.";
var approveLink = ApproveLink.Create("approve-link", DateTime.UtcNow.AddDays(30));
var comment = Comment.Create(articleId, client, content, approveLink);
comment.Approve();
comment.ReplyComment(client, "This is a reply.", approveLink);
string content = "This is a reply.";
var comment = SetupCommentData("approve-link", content);
comment.commentResponse.Approve();
comment.commentResponse.ReplyComment(comment.client, content, comment.approveLink);

// Act
Action act = () => comment.ApproveReply("invalid-link");
Action act = () => comment.commentResponse.ApproveReply("invalid-link");

// Assert
act.Should().Throw<InvalidReplyApprovalLinkException>();
}

#endregion [Tests]

#region [Private Method]

/// <summary>
/// Data setup method to use across the comment test class
/// </summary>
/// <param name="link">Approve link</param>
/// <param name="content">Content to use</param>
/// <returns>Return Comment, Client & Approve link</returns>
private static (Comment commentResponse, Client client, ApproveLink approveLink) SetupCommentData(string link, string content)
{
var articleId = ArticleId.CreateUniqueId("this-is-nabi");
var client = Client.Create("nabi", "[email protected]");
var approveLink = ApproveLink.Create(link, DateTime.UtcNow.AddDays(30));
var comment = Comment.Create(articleId, client, content, approveLink);
return (comment, client, approveLink);
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Blogger.UnitTests.Domain.SubscriberAggregateTests;
public class SubscriberIdTests
{
[Fact]

public void CreateUniqueId_ShouldThrowInvalidEmailAddressException_WhenHaveIncorrectEmail()
{
// act
Expand Down

0 comments on commit 232ae4e

Please sign in to comment.