DEV: Add a test for the "duplicate link" message (#21139)

A followup to cb1e95cd3d

---

`warning_message = nil` in the rb file was an unused variable
This commit is contained in:
Jarek Radosz 2023-04-18 22:23:20 +02:00 committed by GitHub
parent 0650504bf5
commit 0cd8659b06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 30 deletions

View File

@ -649,27 +649,30 @@ export default class ComposerController extends Controller {
const [linkWarn, linkInfo] = linkLookup.check(post, href);
if (linkWarn && !this.get("isWhispering")) {
let body;
if (linkWarn && !this.isWhispering) {
if (linkInfo.username === this.currentUser.username) {
body = I18n.t("composer.duplicate_link_same_user", {
domain: linkInfo.domain,
post_url: topic.urlForPostNumber(linkInfo.post_number),
ago: shortDate(linkInfo.posted_at),
this.appEvents.trigger("composer-messages:create", {
extraClass: "custom-body",
templateName: "education",
body: I18n.t("composer.duplicate_link_same_user", {
domain: linkInfo.domain,
post_url: topic.urlForPostNumber(linkInfo.post_number),
ago: shortDate(linkInfo.posted_at),
}),
});
} else {
body = I18n.t("composer.duplicate_link", {
domain: linkInfo.domain,
username: linkInfo.username,
post_url: topic.urlForPostNumber(linkInfo.post_number),
ago: shortDate(linkInfo.posted_at),
this.appEvents.trigger("composer-messages:create", {
extraClass: "custom-body duplicate-link-message",
templateName: "education",
body: I18n.t("composer.duplicate_link", {
domain: linkInfo.domain,
username: linkInfo.username,
post_url: topic.urlForPostNumber(linkInfo.post_number),
ago: shortDate(linkInfo.posted_at),
}),
});
}
this.appEvents.trigger("composer-messages:create", {
extraClass: "custom-body",
templateName: "education",
body,
});
return false;
}
}

View File

@ -3,9 +3,16 @@ import {
exists,
query,
} from "discourse/tests/helpers/qunit-helpers";
import { click, fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";
import {
click,
fillIn,
triggerKeyEvent,
visit,
waitUntil,
} from "@ember/test-helpers";
import { test } from "qunit";
import I18n from "I18n";
import pretender, { response } from "../helpers/create-pretender";
acceptance("Composer - Messages", function (needs) {
needs.user();
@ -22,13 +29,14 @@ acceptance("Composer - Messages", function (needs) {
test("Shows warning in composer if user hasn't been seen in a long time.", async function (assert) {
await visit("/u/charlie");
await click("button.compose-pm");
assert.ok(
!exists(".composer-popup"),
assert.false(
exists(".composer-popup"),
"composer warning is not shown by default"
);
await triggerKeyEvent(".d-editor-input", "keyup", "Space");
assert.ok(exists(".composer-popup"), "shows composer warning message");
assert.ok(
assert.true(exists(".composer-popup"), "shows composer warning message");
assert.true(
query(".composer-popup").innerHTML.includes(
I18n.t("composer.user_not_seen_in_a_while.single", {
usernames: ['<a class="mention" href="/u/charlie">@charlie</a>'],
@ -60,13 +68,14 @@ acceptance("Composer - Messages - Cannot see group", function (needs) {
test("Shows warning in composer if group hasn't been invited", async function (assert) {
await visit("/t/130");
await click("button.create");
assert.ok(
!exists(".composer-popup"),
assert.false(
exists(".composer-popup"),
"composer warning is not shown by default"
);
await fillIn(".d-editor-input", "Mention @staff");
assert.ok(exists(".composer-popup"), "shows composer warning message");
assert.ok(
assert.true(exists(".composer-popup"), "shows composer warning message");
assert.true(
query(".composer-popup").innerHTML.includes(
I18n.t("composer.cannot_see_group_mention.not_allowed", {
group: "staff",
@ -79,13 +88,14 @@ acceptance("Composer - Messages - Cannot see group", function (needs) {
test("Shows warning in composer if group hasn't been invited, but some members have access already", async function (assert) {
await visit("/t/130");
await click("button.create");
assert.ok(
!exists(".composer-popup"),
assert.false(
exists(".composer-popup"),
"composer warning is not shown by default"
);
await fillIn(".d-editor-input", "Mention @staff2");
assert.ok(exists(".composer-popup"), "shows composer warning message");
assert.ok(
assert.true(exists(".composer-popup"), "shows composer warning message");
assert.true(
query(".composer-popup").innerHTML.includes(
I18n.t("composer.cannot_see_group_mention.some_not_allowed", {
group: "staff2",
@ -96,3 +106,54 @@ acceptance("Composer - Messages - Cannot see group", function (needs) {
);
});
});
acceptance("Composer - Messages - Duplicate links", function (needs) {
needs.user();
test("Shows the warning", async function (assert) {
pretender.get("/inline-onebox", () =>
response({
"inline-oneboxes": [],
})
);
let receivedMessages = false;
pretender.get("/composer_messages", () => {
receivedMessages = true;
return response({
composer_messages: [],
extras: {
duplicate_lookup: {
"test.localhost/t/testing-topic/123/4567": {
domain: "test.localhost",
username: "uwe_keim",
posted_at: "2021-01-01T12:00:00.000Z",
post_number: 1,
},
},
},
});
});
await visit("/t/internationalization-localization/280");
await click("button.create");
// Work around the lack of CSS transitions in the test env
query("#reply-control").dispatchEvent(new Event("transitionend"));
assert
.dom(".composer-popup")
.doesNotExist("composer warning is not shown by default");
await waitUntil(() => receivedMessages);
await fillIn(
".d-editor-input",
"Here's a link: https://test.localhost/t/testing-topic/123/4567"
);
assert
.dom(".composer-popup.duplicate-link-message")
.exists("shows composer warning message");
});
});

View File

@ -22,7 +22,6 @@ class ComposerMessagesController < ApplicationController
usernames = params.require(:usernames)
users = ComposerMessagesFinder.user_not_seen_in_a_while(usernames)
user_count = users.count
warning_message = nil
if user_count > 0
message_locale =