discourse/plugins/chat/spec/system/chat_composer_draft_spec.rb
Joffrey JAFFEUX 4de1d3952b
FIX: improves draft for channels (#21724)
This commit attempts to correctly change draft when the channel changes. It moves responsibility to the composer instead of the channel.

A new service `chatDraftsManager` is being introduced here to allow finer control and pave the way for future thread draft support.

These changes also now allow an editing message to be stored as a draft.
2023-05-24 15:36:46 +02:00

136 lines
3.6 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "Chat composer draft", type: :system, js: true do
fab!(:current_user) { Fabricate(:user) }
fab!(:channel_1) { Fabricate(:chat_channel) }
fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }
let(:chat_page) { PageObjects::Pages::Chat.new }
let(:channel_page) { PageObjects::Pages::ChatChannel.new }
before { chat_system_bootstrap }
context "when loading a channel with a draft" do
fab!(:draft_1) do
Chat::Draft.create!(
chat_channel: channel_1,
user: current_user,
data: { message: "draft" }.to_json,
)
end
before do
channel_1.add(current_user)
sign_in(current_user)
end
it "loads the draft" do
chat_page.visit_channel(channel_1)
expect(channel_page.composer.value).to eq("draft")
end
context "when loading another channel and back" do
fab!(:channel_2) { Fabricate(:chat_channel) }
fab!(:draft_2) do
Chat::Draft.create!(
chat_channel: channel_2,
user: current_user,
data: { message: "draft2" }.to_json,
)
end
before { channel_2.add(current_user) }
it "loads the correct drafts" do
chat_page.visit_channel(channel_1)
expect(channel_page.composer.value).to eq("draft")
chat_page.visit_channel(channel_2)
expect(channel_page.composer.value).to eq("draft2")
chat_page.visit_channel(channel_1)
expect(channel_page.composer.value).to eq("draft")
end
end
context "with editing" do
fab!(:draft_1) do
Chat::Draft.create!(
chat_channel: channel_1,
user: current_user,
data: { message: message_1.message, id: message_1.id, editing: true }.to_json,
)
end
it "loads the draft with the editing state" do
chat_page.visit_channel(channel_1)
expect(channel_page.composer).to be_editing_message(message_1)
end
end
context "with uploads" do
fab!(:upload_1) do
Fabricate(
:upload,
url: "/images/logo-dark.png",
original_filename: "logo_dark.png",
width: 400,
height: 300,
extension: "png",
)
end
fab!(:draft_1) do
Chat::Draft.create!(
chat_channel: channel_1,
user: current_user,
data: { message: "draft", uploads: [upload_1] }.to_json,
)
end
it "loads the draft with the upload" do
chat_page.visit_channel(channel_1)
expect(channel_page.composer.value).to eq("draft")
expect(page).to have_selector(".chat-composer-upload--image", count: 1)
end
end
context "when replying" do
fab!(:draft_1) do
Chat::Draft.create!(
chat_channel: channel_1,
user: current_user,
data: {
message: "draft",
replyToMsg: {
id: message_1.id,
excerpt: message_1.excerpt,
user: {
id: message_1.user.id,
name: nil,
avatar_template: message_1.user.avatar_template,
username: message_1.user.username,
},
},
}.to_json,
)
end
it "loads the draft with replied to mesage" do
chat_page.visit_channel(channel_1)
expect(channel_page.composer.value).to eq("draft")
expect(page).to have_selector(".chat-reply__username", text: message_1.user.username)
expect(page).to have_selector(".chat-reply__excerpt", text: message_1.excerpt)
end
end
end
end