Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/javascript/controllers/clicker_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { nextFrame } from "helpers/timing_helpers";

export default class extends Controller {
static targets = [ "clickable" ]
static outlets = [ "auto-save" ]

async click() {
if (this.hasAutoSaveOutlet) {
await this.autoSaveOutlet.submit()
}
await nextFrame()
this.#clickable.click()
}
Expand Down
4 changes: 2 additions & 2 deletions app/views/cards/container/footer/_create.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<%= button_to card_publish_path(card), name: "creation_type", value: "add", class: "btn",
title: "Create card (#{ hotkey_label(["ctrl", "enter"]) })",
form: { data: { controller: "form bridge--form" } },
data: { form_target: "submit", bridge__form_target: "submit", controller: "clicker", action: "keydown.ctrl+enter@document->clicker#click keydown.meta+enter@document->clicker#click" } do %>
data: { form_target: "submit", bridge__form_target: "submit", controller: "clicker", clicker_auto_save_outlet: "#card_form", action: "keydown.ctrl+enter@document->clicker#click keydown.meta+enter@document->clicker#click" } do %>
<span>Create card</span>
<% end %>

<%= button_to card_publish_path(card), method: :post, class: "btn btn--reversed", name: "creation_type", value: "add_another",
title: "Create and add another (#{ hotkey_label(["ctrl", "shift", "enter"]) })", form: { data: { controller: "form" } },
data: { form_target: "submit", controller: "clicker", action: "keydown.ctrl+shift+enter@document->clicker#click keydown.meta+shift+enter@document->clicker#click" } do %>
data: { form_target: "submit", controller: "clicker", clicker_auto_save_outlet: "#card_form", action: "keydown.ctrl+shift+enter@document->clicker#click keydown.meta+shift+enter@document->clicker#click" } do %>
<span>Create and add another</span>
<% end %>
</div>
Expand Down
47 changes: 47 additions & 0 deletions test/system/card_creation_race_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "application_system_test_case"

class CardCreationRaceTest < ApplicationSystemTestCase
# Reproduces the race documented in #2778: when the user types a title in a
# new draft and immediately presses Cmd/Ctrl+Enter, the publish request can
# reach the server before the auto-save PATCH commits the title. The publish
# callback then writes "Untitled", which the late PATCH rewrites — generating
# a phantom `card_title_changed` event and a system comment as a side effect.
setup do
CardsController.class_eval do
alias_method :_update_without_test_delay, :update
def update
sleep 0.5
_update_without_test_delay
end
end
end

teardown do
CardsController.class_eval do
alias_method :update, :_update_without_test_delay
remove_method :_update_without_test_delay
end
end

test "Cmd+Enter on a new draft preserves the typed title without phantom events or system comments" do
sign_in_as(users(:david))

visit board_url(boards(:writebook))
click_on "Add a card"

title_field = find("textarea[name='card[title]']")
title_field.send_keys "Race fix verified"
title_field.send_keys [ :control, :enter ]

assert_current_path board_path(boards(:writebook))
assert_text "Race fix verified" # wait for late PATCH to commit before assertions

card = Card.where(creator: users(:david)).order(:created_at).last
assert_equal "Race fix verified", card.reload.title
assert card.published?, "card should be published"
refute Event.exists?(action: "card_title_changed", eventable: card),
"publish should not generate a phantom card_title_changed event"
refute card.comments.any? { |c| c.body.to_plain_text.include?("changed the title") },
"publish should not generate a phantom 'changed the title' system comment"
end
end
Loading