Project: pygitweb
from __future__ import annotations
from pygitweb.static_tests.js_runtime import JsPage
from pygitweb.static_tests.pages import posted_json, request_query, requested
def _mr_shell(
*,
project: str = "demo",
mr_ref: str = "refs/tags/mr/aaaabbbbccccdddd",
tag_oid: str = "1" * 40,
) -> str:
return (
f'<div class="card-body">'
f'<div class="mr-comments-section" data-project="{project}" '
f'data-mr-ref="{mr_ref}" data-tag-oid="{tag_oid}">'
'<div class="mr-comments-list"></div>'
'<form class="mr-comment-form d-none">'
'<textarea class="mr-comment-input"></textarea>'
'<button type="submit">Post comment</button></form>'
'<p class="mr-comment-signin-hint d-none">Sign in</p>'
"</div></div>"
)
def _load(js: JsPage) -> None:
js.set_html(_mr_shell())
js.route("comments/list", body=[])
js.route("/auth/status", body={"auth_enabled": False})
js.load("mr-comments.js")
js.tick()
def test_mr_comments_render_and_actions_matrix(js: JsPage) -> None:
js.route(
"comments/list",
body=[
{"oid": "a1", "author": "ada", "content": "**hi**", "can_edit": True, "can_delete": True},
{"oid": "b2", "author": "bob", "content": "theirs", "can_edit": False, "can_delete": True},
{"oid": "c3", "author": "cara", "content": "locked", "can_edit": False, "can_delete": False},
],
)
js.route("/auth/status", body={"auth_enabled": False})
js.set_html(_mr_shell())
js.load("mr-comments.js")
js.tick()
assert js.count(".board-card-comment-item") == 3
assert js.count(".board-card-comment-content strong") == 1
items = js.value(
"Array.from(document.querySelectorAll('.board-card-comment-item')).map(function(row) {"
" return {"
" oid: row.dataset.commentOid,"
" edit: !!row.querySelector('.board-card-comment-edit-btn'),"
" del: !!row.querySelector('.board-card-comment-delete-btn')"
" };"
"})"
)
assert items == [
{"oid": "a1", "edit": True, "del": True},
{"oid": "b2", "edit": False, "del": True},
{"oid": "c3", "edit": False, "del": False},
]
assert "d-none" not in js.classes(".mr-comment-form")
def test_mr_comment_form_hides_until_signed_in(js: JsPage) -> None:
js.route("/auth/status", body={"auth_enabled": True})
js.route("comments/list", body=[])
js.set_html(_mr_shell())
js.load("mr-comments.js")
js.tick()
assert "d-none" in js.classes(".mr-comment-form")
assert "d-none" not in js.classes(".mr-comment-signin-hint")
def test_submitting_mr_comment_posts_and_reloads(js: JsPage) -> None:
js.route("/auth/status", body={"auth_enabled": False})
js.route("comments/create", status=200, body={"oid": "new", "mr_oid": "1" * 40})
js.route("comments/list", body=[])
js.set_html(_mr_shell())
js.load("mr-comments.js")
js.tick()
js.route(
"comments/list",
body=[{"oid": "new", "author": "ada", "content": "hello", "can_edit": True, "can_delete": True}],
)
js.set_prop(".mr-comment-input", "value", " hello ")
js.dispatch(".mr-comment-form", "submit")
q = request_query(js, "comments/create")
assert q["mr"] == "refs/tags/mr/aaaabbbbccccdddd"
assert "task" not in q
assert posted_json(js, "comments/create") == {"content": "hello"}
assert js.text(".board-card-comment-content") == "hello"
def test_mr_comment_create_errors_are_shown(js: JsPage) -> None:
_load(js)
js.set_prop(".mr-comment-input", "value", "hello")
for status, snippet in ((401, "Not authorized"), (500, "Could not post comment")):
js.route("comments/create", status=status, text="no")
js.dispatch(".mr-comment-form", "submit")
assert snippet in js.text(".board-task-error-hint")
js.tick()
def test_editing_mr_comment_posts_modify(js: JsPage) -> None:
js.route("/auth/status", body={"auth_enabled": False})
js.route(
"comments/list",
body=[{"oid": "abc", "author": "ada", "content": "before", "can_edit": True, "can_delete": False}],
)
js.route("comments/modify", status=200, body={"oid": "def", "content": "after", "mr_oid": "1" * 40})
js.set_html(_mr_shell())
js.load("mr-comments.js")
js.tick()
js.click(".board-card-comment-edit-btn")
js.set_prop(".board-card-comment-edit-input", "value", "after")
js.route(
"comments/list",
body=[{"oid": "def", "author": "ada", "content": "after", "can_edit": True, "can_delete": False}],
)
js.click(".board-card-comment-save-btn")
assert request_query(js, "comments/modify")["comment"] == "abc"
assert posted_json(js, "comments/modify") == {"content": "after"}
assert js.text(".board-card-comment-content") == "after"
def test_deleting_mr_comment_confirms_and_posts(js: JsPage) -> None:
js.route("/auth/status", body={"auth_enabled": False})
js.route(
"comments/list",
body=[{"oid": "gone", "author": "ada", "content": "bye", "can_edit": False, "can_delete": True}],
)
js.route("comments/delete", status=200, body={"message": "Comment deleted", "mr_oid": "1" * 40})
js.set_html(_mr_shell())
js.load("mr-comments.js")
js.tick()
js.run("__confirmYes = false; __confirms = [];")
js.click(".board-card-comment-delete-btn")
assert requested(js, "comments/delete") is False
js.run("__confirmYes = true;")
js.route("comments/list", body=[])
js.click(".board-card-comment-delete-btn")
assert request_query(js, "comments/delete")["comment"] == "gone"
assert "No comments yet" in js.text(".mr-comments-empty")