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