2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
require "rails/generators/channel/channel_generator"
|
2015-12-18 23:43:11 -05:00
|
|
|
|
|
|
|
class ChannelGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
tests Rails::Generators::ChannelGenerator
|
|
|
|
|
2016-02-10 17:36:11 -05:00
|
|
|
def test_application_cable_skeleton_is_created
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["books"]
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2016-02-28 17:59:32 -05:00
|
|
|
assert_file "app/channels/application_cable/channel.rb" do |cable|
|
|
|
|
assert_match(/module ApplicationCable\n class Channel < ActionCable::Channel::Base\n/, cable)
|
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2016-02-28 17:59:32 -05:00
|
|
|
assert_file "app/channels/application_cable/connection.rb" do |cable|
|
|
|
|
assert_match(/module ApplicationCable\n class Connection < ActionCable::Connection::Base\n/, cable)
|
|
|
|
end
|
|
|
|
end
|
2016-02-10 17:36:11 -05:00
|
|
|
|
2015-12-18 23:43:11 -05:00
|
|
|
def test_channel_is_created
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["chat"]
|
2015-12-21 11:44:47 -05:00
|
|
|
|
2015-12-18 23:43:11 -05:00
|
|
|
assert_file "app/channels/chat_channel.rb" do |channel|
|
|
|
|
assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
|
|
|
|
end
|
|
|
|
|
2016-05-08 10:22:43 -04:00
|
|
|
assert_file "app/assets/javascripts/channels/chat.js" do |channel|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/App\.chat = App\.cable\.subscriptions\.create\("ChatChannel/, channel)
|
2016-05-08 10:22:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_channel_with_multiple_actions_is_created
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["chat", "speak", "mute"]
|
2016-05-08 10:22:43 -04:00
|
|
|
|
|
|
|
assert_file "app/channels/chat_channel.rb" do |channel|
|
|
|
|
assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
|
|
|
|
assert_match(/def speak/, channel)
|
|
|
|
assert_match(/def mute/, channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "app/assets/javascripts/channels/chat.js" do |channel|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/App\.chat = App\.cable\.subscriptions\.create\("ChatChannel/, channel)
|
2016-05-08 10:22:43 -04:00
|
|
|
assert_match(/,\n\n speak/, channel)
|
|
|
|
assert_match(/,\n\n mute: function\(\) \{\n return this\.perform\('mute'\);\n \}\n\}\);/, channel)
|
2015-12-18 23:43:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-21 15:58:08 -05:00
|
|
|
def test_channel_asset_is_not_created_when_skip_assets_is_passed
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["chat", "--skip-assets"]
|
2015-12-18 23:43:11 -05:00
|
|
|
|
|
|
|
assert_file "app/channels/chat_channel.rb" do |channel|
|
|
|
|
assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
|
|
|
|
end
|
|
|
|
|
2016-05-08 10:22:43 -04:00
|
|
|
assert_no_file "app/assets/javascripts/channels/chat.js"
|
2015-12-18 23:43:11 -05:00
|
|
|
end
|
2016-04-12 13:19:38 -04:00
|
|
|
|
|
|
|
def test_cable_js_is_created_if_not_present_already
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["chat"]
|
2016-04-12 13:19:38 -04:00
|
|
|
FileUtils.rm("#{destination_root}/app/assets/javascripts/cable.js")
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["camp"]
|
2016-04-12 13:19:38 -04:00
|
|
|
|
|
|
|
assert_file "app/assets/javascripts/cable.js"
|
|
|
|
end
|
2016-04-16 02:19:41 -04:00
|
|
|
|
|
|
|
def test_channel_on_revoke
|
2016-08-06 13:16:09 -04:00
|
|
|
run_generator ["chat"]
|
|
|
|
run_generator ["chat"], behavior: :revoke
|
2016-04-16 02:19:41 -04:00
|
|
|
|
|
|
|
assert_no_file "app/channels/chat_channel.rb"
|
2016-05-08 10:22:43 -04:00
|
|
|
assert_no_file "app/assets/javascripts/channels/chat.js"
|
2016-04-16 02:19:41 -04:00
|
|
|
|
|
|
|
assert_file "app/channels/application_cable/channel.rb"
|
|
|
|
assert_file "app/channels/application_cable/connection.rb"
|
|
|
|
assert_file "app/assets/javascripts/cable.js"
|
|
|
|
end
|
2015-12-18 23:43:11 -05:00
|
|
|
end
|