code.gitea.io/gitea@v1.21.7/routers/web/repo/pull_review_test.go (about)

     1  // Copyright 2024 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	issues_model "code.gitea.io/gitea/models/issues"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	"code.gitea.io/gitea/modules/context"
    14  	"code.gitea.io/gitea/modules/contexttest"
    15  	"code.gitea.io/gitea/modules/templates"
    16  	"code.gitea.io/gitea/services/pull"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestRenderConversation(t *testing.T) {
    22  	unittest.PrepareTestEnv(t)
    23  
    24  	pr, _ := issues_model.GetPullRequestByID(db.DefaultContext, 2)
    25  	_ = pr.LoadIssue(db.DefaultContext)
    26  	_ = pr.Issue.LoadPoster(db.DefaultContext)
    27  	_ = pr.Issue.LoadRepo(db.DefaultContext)
    28  
    29  	run := func(name string, cb func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder)) {
    30  		t.Run(name, func(t *testing.T) {
    31  			ctx, resp := contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()})
    32  			contexttest.LoadUser(t, ctx, pr.Issue.PosterID)
    33  			contexttest.LoadRepo(t, ctx, pr.BaseRepoID)
    34  			contexttest.LoadGitRepo(t, ctx)
    35  			defer ctx.Repo.GitRepo.Close()
    36  			cb(t, ctx, resp)
    37  		})
    38  	}
    39  
    40  	var preparedComment *issues_model.Comment
    41  	run("prepare", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
    42  		comment, err := pull.CreateCodeComment(ctx, pr.Issue.Poster, ctx.Repo.GitRepo, pr.Issue, 1, "content", "", false, 0, pr.HeadCommitID)
    43  		if !assert.NoError(t, err) {
    44  			return
    45  		}
    46  		comment.Invalidated = true
    47  		err = issues_model.UpdateCommentInvalidate(ctx, comment)
    48  		if !assert.NoError(t, err) {
    49  			return
    50  		}
    51  		preparedComment = comment
    52  	})
    53  	if !assert.NotNil(t, preparedComment) {
    54  		return
    55  	}
    56  	run("diff with outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
    57  		ctx.Data["ShowOutdatedComments"] = true
    58  		renderConversation(ctx, preparedComment, "diff")
    59  		assert.Contains(t, resp.Body.String(), `<div class="content comment-container"`)
    60  	})
    61  	run("diff without outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
    62  		ctx.Data["ShowOutdatedComments"] = false
    63  		renderConversation(ctx, preparedComment, "diff")
    64  		assert.Contains(t, resp.Body.String(), `conversation-not-existing`)
    65  	})
    66  	run("timeline with outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
    67  		ctx.Data["ShowOutdatedComments"] = true
    68  		renderConversation(ctx, preparedComment, "timeline")
    69  		assert.Contains(t, resp.Body.String(), `<div id="code-comments-`)
    70  	})
    71  	run("timeline is not affected by ShowOutdatedComments=false", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
    72  		ctx.Data["ShowOutdatedComments"] = false
    73  		renderConversation(ctx, preparedComment, "timeline")
    74  		assert.Contains(t, resp.Body.String(), `<div id="code-comments-`)
    75  	})
    76  }