mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 23:54:16 +08:00
DEV: makes user-card-chat-button uses glimmer (#22496)
This commit also namespaces the component to now be: `<Chat::UserCardButton />`
This commit is contained in:
parent
9830c40386
commit
8270d76f16
|
@ -0,0 +1,8 @@
|
|||
{{#if this.shouldRender}}
|
||||
<DButton
|
||||
@class="btn-primary chat-user-card-btn"
|
||||
@action={{this.startChatting}}
|
||||
@label="chat.title_capitalized"
|
||||
@icon="d-chat"
|
||||
/>
|
||||
{{/if}}
|
|
@ -0,0 +1,23 @@
|
|||
import Component from "@glimmer/component";
|
||||
import { action } from "@ember/object";
|
||||
import { inject as service } from "@ember/service";
|
||||
|
||||
export default class ChatUserCardButton extends Component {
|
||||
@service chat;
|
||||
@service appEvents;
|
||||
@service router;
|
||||
|
||||
get shouldRender() {
|
||||
return this.chat.userCanDirectMessage && !this.args.user.suspended;
|
||||
}
|
||||
|
||||
@action
|
||||
startChatting() {
|
||||
return this.chat
|
||||
.upsertDmChannelForUsernames([this.args.user.username])
|
||||
.then((channel) => {
|
||||
this.router.transitionTo("chat.channel", ...channel.routeModels);
|
||||
this.appEvents.trigger("card:close");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
{{#if (and this.chat.userCanDirectMessage (not @user.suspended))}}
|
||||
<DButton
|
||||
@class="btn-primary user-card-chat-btn"
|
||||
@action={{action "startChatting"}}
|
||||
@label="chat.title_capitalized"
|
||||
@icon="d-chat"
|
||||
/>
|
||||
{{/if}}
|
|
@ -1,19 +0,0 @@
|
|||
import Component from "@ember/component";
|
||||
import { action } from "@ember/object";
|
||||
import { inject as service } from "@ember/service";
|
||||
|
||||
export default class UserCardChatButton extends Component {
|
||||
@service chat;
|
||||
@service appEvents;
|
||||
@service router;
|
||||
|
||||
@action
|
||||
startChatting() {
|
||||
this.chat
|
||||
.upsertDmChannelForUsernames([this.user.username])
|
||||
.then((chatChannel) => {
|
||||
this.router.transitionTo("chat.channel", ...chatChannel.routeModels);
|
||||
this.appEvents.trigger("card:close");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
{{#if this.user.can_chat_user}}
|
||||
<UserCardChatButton @user={{this.user}} />
|
||||
<Chat::UserCardButton @user={{this.user}} />
|
||||
{{/if}}
|
|
@ -114,6 +114,7 @@ function userFabricator(args = {}) {
|
|||
username: args.username || "hawk",
|
||||
name: args.name,
|
||||
avatar_template: "/letter_avatar_proxy/v3/letter/t/41988e/{size}.png",
|
||||
suspended_till: args.suspended_till,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -10,13 +10,13 @@ RSpec.describe "User card", type: :system do
|
|||
|
||||
shared_examples "not showing chat button" do
|
||||
it "doesn’t show the chat buttton" do
|
||||
expect(page).to have_no_css(".user-card-chat-btn")
|
||||
expect(page).to have_no_css(".chat-user-card-btn")
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "showing chat button" do
|
||||
it "shows the chat buttton" do
|
||||
expect(page).to have_css(".user-card-chat-btn")
|
||||
expect(page).to have_css(".chat-user-card-btn")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -49,7 +49,7 @@ RSpec.describe "User card", type: :system do
|
|||
include_examples "showing chat button"
|
||||
|
||||
context "when clicking chat button" do
|
||||
before { find(".user-card-chat-btn").click }
|
||||
before { find(".chat-user-card-btn").click }
|
||||
|
||||
it "opens correct channel" do
|
||||
# at this point the ChatChannel is not created yet
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
||||
import hbs from "htmlbars-inline-precompile";
|
||||
import { render } from "@ember/test-helpers";
|
||||
import { module, test } from "qunit";
|
||||
import sinon from "sinon";
|
||||
import fabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
||||
|
||||
module(
|
||||
"Discourse Chat | Component | <Chat::UserCardButton />",
|
||||
function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test("when current user can send direct messages", async function (assert) {
|
||||
sinon
|
||||
.stub(this.owner.lookup("service:chat"), "userCanDirectMessage")
|
||||
.value(true);
|
||||
this.user = fabricators.user();
|
||||
|
||||
await render(hbs`<Chat::UserCardButton @user={{user}} />`);
|
||||
|
||||
assert.dom(".chat-user-card-btn").exists("it shows the chat button");
|
||||
});
|
||||
|
||||
test("when current user can’t send direct messages", async function (assert) {
|
||||
sinon
|
||||
.stub(this.owner.lookup("service:chat"), "userCanDirectMessage")
|
||||
.value(false);
|
||||
this.user = fabricators.user();
|
||||
|
||||
await render(hbs`<Chat::UserCardButton @user={{user}} />`);
|
||||
|
||||
assert
|
||||
.dom(".chat-user-card-btn")
|
||||
.doesNotExist("it doesn’t show the chat button");
|
||||
});
|
||||
|
||||
test("when displayed user is suspended", async function (assert) {
|
||||
sinon
|
||||
.stub(this.owner.lookup("service:chat"), "userCanDirectMessage")
|
||||
.value(true);
|
||||
|
||||
this.user = fabricators.user({
|
||||
suspended_till: moment().add(1, "year").toDate(),
|
||||
});
|
||||
|
||||
await render(hbs`<Chat::UserCardButton @user={{user}}/>`);
|
||||
|
||||
assert
|
||||
.dom(".chat-user-card-btn")
|
||||
.doesNotExist("it doesn’t show the chat button");
|
||||
});
|
||||
}
|
||||
);
|
|
@ -1,51 +0,0 @@
|
|||
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
||||
import hbs from "htmlbars-inline-precompile";
|
||||
import { render } from "@ember/test-helpers";
|
||||
import { module, test } from "qunit";
|
||||
import sinon from "sinon";
|
||||
import { exists } from "discourse/tests/helpers/qunit-helpers";
|
||||
import User from "discourse/models/user";
|
||||
|
||||
module("Discourse Chat | Component | user-card-chat-button", function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test("when current user can send direct messages", async function (assert) {
|
||||
sinon
|
||||
.stub(this.owner.lookup("service:chat"), "userCanDirectMessage")
|
||||
.value(true);
|
||||
|
||||
await render(hbs`<UserCardChatButton/>`);
|
||||
|
||||
assert.true(exists(".user-card-chat-btn"), "it shows the chat button");
|
||||
});
|
||||
|
||||
test("when current user can’t send direct messages", async function (assert) {
|
||||
sinon
|
||||
.stub(this.owner.lookup("service:chat"), "userCanDirectMessage")
|
||||
.value(false);
|
||||
|
||||
await render(hbs`<UserCardChatButton/>`);
|
||||
|
||||
assert.false(
|
||||
exists(".user-card-chat-btn"),
|
||||
"it doesn’t show the chat button"
|
||||
);
|
||||
});
|
||||
|
||||
test("when displayed user is suspended", async function (assert) {
|
||||
sinon
|
||||
.stub(this.owner.lookup("service:chat"), "userCanDirectMessage")
|
||||
.value(true);
|
||||
|
||||
this.user = User.create({
|
||||
suspended_till: moment().add(1, "year").toDate(),
|
||||
});
|
||||
|
||||
await render(hbs`<UserCardChatButton @user={{user}}/>`);
|
||||
|
||||
assert.false(
|
||||
exists(".user-card-chat-btn"),
|
||||
"it doesn’t show the chat button"
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user