Files
forum/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-messages-section-test.js

557 lines
16 KiB
JavaScript

import { click, currentURL, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { NotificationLevels } from "discourse/lib/notification-levels";
import {
acceptance,
publishToMessageBus,
queryAll,
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
import { i18n } from "discourse-i18n";
acceptance(
"Sidebar - Logged on user - Messages Section - user does not have can_send_private_messages permission",
function (needs) {
needs.user({
moderator: false,
admin: false,
can_send_private_messages: false,
});
needs.settings({
navigation_menu: "sidebar",
});
test("clicking on section header button", async function (assert) {
await visit("/");
assert
.dom(".sidebar-section[data-section-name='messages']")
.doesNotExist("does not display messages section in sidebar");
});
}
);
acceptance(
"Sidebar - Logged on user - Messages Section - user does have can_send_private_messages permission",
function (needs) {
needs.user({ can_send_private_messages: true });
needs.settings({
navigation_menu: "sidebar",
});
needs.pretender((server, helper) => {
[
"/topics/private-messages-new/:username.json",
"/topics/private-messages-unread/:username.json",
"/topics/private-messages-archive/:username.json",
"/topics/private-messages-sent/:username.json",
"/topics/private-messages-group/:username/:group_name/new.json",
"/topics/private-messages-group/:username/:group_name.json",
"/topics/private-messages-group/:username/:group_name/unread.json",
"/topics/private-messages-group/:username/:group_name/archive.json",
].forEach((url) => {
server.get(url, () => {
const topics = [
{ id: 1, posters: [] },
{ id: 2, posters: [] },
{ id: 3, posters: [] },
];
return helper.response({
topic_list: {
topics,
},
});
});
});
});
test("clicking on section header button", async function (assert) {
await visit("/");
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-header-button"
);
assert.dom("#reply-control.private-message").exists("opens the composer");
});
test("clicking on section header link", async function (assert) {
await visit("/");
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-header"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-content"
)
.doesNotExist("hides the content of the section");
});
test("personal messages section links", async function (assert) {
await visit("/");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-inbox']"
)
.exists("displays the personal message inbox link");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link"
)
.exists({ count: 1 }, "only displays the personal message inbox link");
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-inbox']"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-inbox'].active"
)
.exists("personal message inbox link is marked as active");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link"
)
.exists(
{ count: 5 },
"expands and displays the links for personal messages"
);
});
["new", "archive", "sent", "unread"].forEach((type) => {
test(`${type} personal messages section link`, async function (assert) {
await visit("/");
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-inbox']"
);
await click(
`.sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-${type}']`
);
assert.strictEqual(
currentURL(),
`/u/eviltrout/messages/${type}`,
`it should transition to user's ${type} personal messages`
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.active"
)
.exists(
{ count: 2 },
"only two links are marked as active in the sidebar"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-inbox'].active"
)
.exists("personal message inbox link is marked as active");
assert
.dom(
`.sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-${type}'].active`
)
.exists(`personal message ${type} link is marked as active`);
assert
.dom(
`.sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-${type}'] .sidebar-section-link-prefix`
)
.doesNotExist(
`prefix is not displayed for ${type} personal message section link`
);
});
});
test("group messages section links", async function (assert) {
updateCurrentUser({
groups: [
{
name: "group3",
has_messages: true,
},
{
name: "group2",
has_messages: false,
},
{
name: "group1",
has_messages: true,
},
],
});
await visit("/");
const groupSectionLinks = queryAll(
".sidebar-section[data-section-name='messages'] .sidebar-section-link"
);
assert.deepEqual(
groupSectionLinks
.toArray()
.map((sectionLink) => sectionLink.textContent.trim()),
["Inbox", "group1", "group3"],
"displays group section links sorted by name"
);
await visit("/u/eviltrout/messages/group/GrOuP1");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link"
)
.exists(
{ count: 6 },
"expands and displays the links for group1 group messages"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.group1"
)
.exists({ count: 4 }, "expands the links for group1 group messages");
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-inbox'].group3"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.group1"
)
.exists({ count: 1 }, "collapses the links for group1 group messages");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.group3"
)
.exists({ count: 4 }, "expands the links for group3 group messages");
});
["new", "archive", "unread"].forEach((type) => {
test(`${type} group messages section link`, async function (assert) {
updateCurrentUser({
groups: [
{
name: "group1",
has_messages: true,
},
{
name: "group2",
has_messages: false,
},
{
name: "group3",
has_messages: true,
},
],
});
await visit("/");
await click(
`.sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-inbox'].group1`
);
await click(
`.sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-${type}'].group1`
);
assert.strictEqual(
currentURL(),
`/u/eviltrout/messages/group/group1/${type}`,
`it should transition to user's ${type} personal messages`
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.active"
)
.exists(
{ count: 2 },
"only two links are marked as active in the sidebar"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-inbox'].group1.active"
)
.exists("group1 group message inbox link is marked as active");
assert
.dom(
`.sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-${type}'].group1.active`
)
.exists(`group1 group message ${type} link is marked as active`);
});
});
test("viewing personal message topic with a group the user is a part of", async function (assert) {
updateCurrentUser({
groups: [
{
name: "foo_group", // based on fixtures
has_messages: true,
},
],
});
await visit("/t/130");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link"
)
.exists({ count: 5 }, "5 section links are displayed");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.personal-messages"
)
.exists(
{ count: 1 },
"personal messages inbox filter links are not shown"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.foo_group"
)
.exists(
{ count: 4 },
"foo_group messages inbox filter links are shown"
);
});
test("viewing personal message topic", async function (assert) {
updateCurrentUser({
groups: [
{
name: "foo_group", // based on fixtures
has_messages: true,
},
],
});
await visit("/t/34");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link"
)
.exists({ count: 6 }, "6 section links are displayed");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.personal-messages"
)
.exists({ count: 5 }, "personal messages inbox filter links are shown");
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link.foo_group"
)
.exists(
{ count: 1 },
"foo_group messages inbox filter links are not shown"
);
});
test("new and unread counts for group messages", async function (assert) {
updateCurrentUser({
groups: [
{
id: 1,
name: "group1",
has_messages: true,
},
],
});
await visit("/");
const pmTopicTrackingState = this.container.lookup(
"service:pm-topic-tracking-state"
);
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-inbox'].group1"
);
await publishToMessageBus(pmTopicTrackingState.groupChannel(1), {
topic_id: 1,
message_type: "unread",
payload: {
last_read_post_number: 1,
highest_post_number: 2,
notification_level: NotificationLevels.TRACKING,
group_ids: [1],
},
});
await publishToMessageBus(pmTopicTrackingState.groupChannel(1), {
topic_id: 2,
message_type: "new_topic",
payload: {
last_read_post_number: null,
highest_post_number: 1,
notification_level: NotificationLevels.TRACKING,
group_ids: [1],
},
});
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-unread'].group1"
)
.hasText(
i18n("sidebar.sections.messages.links.unread_with_count", {
count: 1,
}),
"displays 1 count for group1 unread inbox filter link"
);
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-new'].group1"
)
.hasText(
i18n("sidebar.sections.messages.links.new_with_count", {
count: 1,
}),
"displays 1 count for group1 new inbox filter link"
);
await publishToMessageBus(pmTopicTrackingState.groupChannel(1), {
topic_id: 2,
message_type: "read",
payload: {
last_read_post_number: 1,
highest_post_number: 1,
notification_level: NotificationLevels.TRACKING,
group_ids: [1],
},
});
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='group-messages-new'].group1"
)
.hasText(
i18n("sidebar.sections.messages.links.new"),
"removes count for group1 new inbox filter link"
);
});
test("new and unread counts for personal messages", async function (assert) {
await visit("/");
const pmTopicTrackingState = this.container.lookup(
"service:pm-topic-tracking-state"
);
await click(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-inbox']"
);
await publishToMessageBus(pmTopicTrackingState.userChannel(), {
topic_id: 1,
message_type: "unread",
payload: {
last_read_post_number: 1,
highest_post_number: 2,
notification_level: NotificationLevels.TRACKING,
group_ids: [],
},
});
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-unread']"
)
.hasText(
i18n("sidebar.sections.messages.links.unread_with_count", {
count: 1,
}),
"displays 1 count for the unread inbox filter link"
);
await publishToMessageBus(pmTopicTrackingState.userChannel(), {
topic_id: 2,
message_type: "unread",
payload: {
last_read_post_number: 1,
highest_post_number: 2,
notification_level: NotificationLevels.TRACKING,
group_ids: [],
},
});
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-unread']"
)
.hasText(
i18n("sidebar.sections.messages.links.unread_with_count", {
count: 2,
}),
"displays 2 count for the unread inbox filter link"
);
await publishToMessageBus(pmTopicTrackingState.userChannel(), {
topic_id: 3,
message_type: "new_topic",
payload: {
last_read_post_number: null,
highest_post_number: 1,
notification_level: NotificationLevels.TRACKING,
group_ids: [],
},
});
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-new']"
)
.hasText(
i18n("sidebar.sections.messages.links.new_with_count", {
count: 1,
}),
"displays 1 count for the new inbox filter link"
);
await publishToMessageBus(pmTopicTrackingState.userChannel(), {
topic_id: 3,
message_type: "read",
payload: {
last_read_post_number: 1,
highest_post_number: 1,
notification_level: NotificationLevels.TRACKING,
group_ids: [],
},
});
assert
.dom(
".sidebar-section[data-section-name='messages'] .sidebar-section-link[data-link-name='personal-messages-new']"
)
.hasText(
i18n("sidebar.sections.messages.links.new"),
"removes the count from the new inbox filter link"
);
});
}
);