|
| 1 | +require "application_system_test_case" |
| 2 | + |
| 3 | +class CardRefreshTest < ApplicationSystemTestCase |
| 4 | + include ActionView::RecordIdentifier |
| 5 | + |
| 6 | + setup do |
| 7 | + sign_in_as(users(:david)) |
| 8 | + Current.user = users(:kevin) |
| 9 | + @card = cards(:layout) |
| 10 | + end |
| 11 | + |
| 12 | + test "a broadcast refresh shows card changes made elsewhere" do |
| 13 | + visit card_url(@card) |
| 14 | + assert_selector "h1", text: @card.title |
| 15 | + |
| 16 | + update_card_elsewhere |
| 17 | + |
| 18 | + assert_selector "h1", text: "Retitled elsewhere", wait: 5 |
| 19 | + assert_text "Description updated elsewhere" |
| 20 | + end |
| 21 | + |
| 22 | + test "a broadcast refresh preserves an edit in progress" do |
| 23 | + visit card_url(@card) |
| 24 | + click_on @card.title |
| 25 | + |
| 26 | + within_edit_frame do |
| 27 | + fill_in_lexxy with: "Draft description in progress" |
| 28 | + end |
| 29 | + |
| 30 | + update_card_elsewhere |
| 31 | + |
| 32 | + # The title change's system comment appearing proves the refresh reached the page. |
| 33 | + assert_text "changed the title", wait: 5 |
| 34 | + within_edit_frame do |
| 35 | + assert_selector "lexxy-editor", text: "Draft description in progress" |
| 36 | + end |
| 37 | + assert_no_text "Description updated elsewhere" |
| 38 | + end |
| 39 | + |
| 40 | + test "canceling an edit reveals card changes made elsewhere during the edit" do |
| 41 | + visit card_url(@card) |
| 42 | + click_on @card.title |
| 43 | + within_edit_frame { assert_selector "form" } |
| 44 | + |
| 45 | + update_card_elsewhere |
| 46 | + assert_text "changed the title", wait: 5 |
| 47 | + |
| 48 | + send_keys :escape |
| 49 | + |
| 50 | + assert_selector "h1", text: "Retitled elsewhere" |
| 51 | + assert_text "Description updated elsewhere" |
| 52 | + end |
| 53 | + |
| 54 | + test "a broadcast refresh shows card changes after an edit is canceled" do |
| 55 | + visit card_url(@card) |
| 56 | + click_on @card.title |
| 57 | + within_edit_frame { assert_selector "form" } |
| 58 | + |
| 59 | + send_keys :escape |
| 60 | + assert_selector "a.card__title-link", text: @card.title |
| 61 | + |
| 62 | + update_card_elsewhere |
| 63 | + |
| 64 | + assert_selector "h1", text: "Retitled elsewhere", wait: 5 |
| 65 | + assert_text "Description updated elsewhere" |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + def update_card_elsewhere |
| 70 | + wait_for_cable_subscriptions |
| 71 | + @card.update!(title: "Retitled elsewhere", description: "Description updated elsewhere") |
| 72 | + perform_enqueued_jobs only: Turbo::Streams::BroadcastStreamJob |
| 73 | + end |
| 74 | + |
| 75 | + # A broadcast sent before the page's subscriptions are confirmed is lost. |
| 76 | + def wait_for_cable_subscriptions |
| 77 | + assert_selector "turbo-cable-stream-source[connected]", visible: :all |
| 78 | + assert_no_selector "turbo-cable-stream-source:not([connected])", visible: :all |
| 79 | + end |
| 80 | + |
| 81 | + def within_edit_frame(&block) |
| 82 | + within "##{dom_id(@card, :edit)}", &block |
| 83 | + end |
| 84 | +end |
0 commit comments