mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-24 17:04:43 +00:00
Added a turbo stream for the current and next match on mat stats page.
This commit is contained in:
68
test/models/match_broadcast_test.rb
Normal file
68
test/models/match_broadcast_test.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
require 'test_helper'
|
||||
|
||||
class MatchBroadcastTest < ActiveSupport::TestCase
|
||||
include ActionView::RecordIdentifier
|
||||
|
||||
test "broadcasts to old and new mats when mat changes" do
|
||||
create_double_elim_tournament_single_weight_1_6(4)
|
||||
mat1 = @tournament.mats.create!(name: "Mat 1")
|
||||
mat2 = @tournament.mats.create!(name: "Mat 2")
|
||||
@tournament.matches.update_all(mat_id: nil)
|
||||
match = @tournament.matches.first
|
||||
|
||||
# Set an initial mat
|
||||
match.update!(mat: mat1)
|
||||
stream1 = stream_name_for(mat1)
|
||||
stream2 = stream_name_for(mat2)
|
||||
|
||||
# Clear the stream so we can test changes from this state
|
||||
clear_streams(stream1, stream2)
|
||||
|
||||
# Update the mat and check the stream
|
||||
match.update!(mat: mat2)
|
||||
assert_equal [mat1.id, mat2.id], match.saved_change_to_mat_id
|
||||
|
||||
assert_equal 1, broadcasts_for(stream1).size
|
||||
assert_equal 1, broadcasts_for(stream2).size
|
||||
|
||||
assert_includes broadcasts_for(stream2).last, dom_id(mat2, :current_match)
|
||||
end
|
||||
|
||||
test "broadcasts when a match is removed from a mat" do
|
||||
create_double_elim_tournament_single_weight_1_6(4)
|
||||
mat = @tournament.mats.create!(name: "Mat 1")
|
||||
@tournament.matches.update_all(mat_id: nil)
|
||||
match = @tournament.matches.first
|
||||
|
||||
# Set an initial mat
|
||||
match.update!(mat: mat)
|
||||
stream = stream_name_for(mat)
|
||||
|
||||
# Clear the stream so we can test changes from this state
|
||||
clear_streams(stream)
|
||||
|
||||
# Update the mat and check the stream
|
||||
match.update!(mat: nil)
|
||||
assert_equal [mat.id, nil], match.saved_change_to_mat_id
|
||||
|
||||
assert_equal 1, broadcasts_for(stream).size
|
||||
assert_includes broadcasts_for(stream).last, dom_id(mat, :current_match)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def broadcasts_for(stream)
|
||||
ActionCable.server.pubsub.broadcasts(stream)
|
||||
end
|
||||
|
||||
def clear_streams(*streams)
|
||||
ActionCable.server.pubsub.clear
|
||||
streams.each do |stream|
|
||||
broadcasts_for(stream).clear
|
||||
end
|
||||
end
|
||||
|
||||
def stream_name_for(streamable)
|
||||
Turbo::StreamsChannel.send(:stream_name_from, [streamable])
|
||||
end
|
||||
end
|
||||
29
test/views/mats_current_match_partial_test.rb
Normal file
29
test/views/mats_current_match_partial_test.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
require "test_helper"
|
||||
|
||||
class MatsCurrentMatchPartialTest < ActionView::TestCase
|
||||
include ActionView::RecordIdentifier
|
||||
|
||||
test "renders current match contents when assigned" do
|
||||
create_double_elim_tournament_single_weight_1_6(4)
|
||||
mat = @tournament.mats.create!(name: "Mat 1")
|
||||
match = @tournament.matches.first
|
||||
|
||||
match.update!(mat: mat)
|
||||
|
||||
render partial: "mats/current_match", locals: { mat: mat }
|
||||
|
||||
assert_includes rendered, "Bout"
|
||||
assert_includes rendered, match.bout_number.to_s
|
||||
assert_includes rendered, mat.name
|
||||
end
|
||||
|
||||
test "renders friendly message when no matches assigned" do
|
||||
create_double_elim_tournament_single_weight_1_6(4)
|
||||
mat = @tournament.mats.create!(name: "Mat 1")
|
||||
@tournament.matches.update_all(mat_id: nil)
|
||||
|
||||
render partial: "mats/current_match", locals: { mat: mat }
|
||||
|
||||
assert_includes rendered, "No matches assigned to this mat."
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user