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:
Joffrey JAFFEUX 2023-07-10 14:04:26 +02:00 committed by GitHub
parent 9830c40386
commit 8270d76f16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 82 deletions

View File

@ -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}}

View File

@ -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");
});
}
}

View File

@ -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}}

View File

@ -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");
});
}
}

View File

@ -1,3 +1,3 @@
{{#if this.user.can_chat_user}}
<UserCardChatButton @user={{this.user}} />
<Chat::UserCardButton @user={{this.user}} />
{{/if}}

View File

@ -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,
});
}

View File

@ -10,13 +10,13 @@ RSpec.describe "User card", type: :system do
shared_examples "not showing chat button" do
it "doesnt 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

View File

@ -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 cant 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 doesnt 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 doesnt show the chat button");
});
}
);

View File

@ -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 cant 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 doesnt 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 doesnt show the chat button"
);
});
});