File: channel_generator_test.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (172 lines) | stat: -rw-r--r-- 5,725 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# frozen_string_literal: true

require "generators/generators_test_helper"
require "rails/generators/channel/channel_generator"

class ChannelGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  tests Rails::Generators::ChannelGenerator

  setup do
    use_with_javascript
    use_under_importmap
  end

  test "shared channel files are created" do
    run_generator ["books"]

    assert_file "app/channels/application_cable/channel.rb" do |cable|
      assert_match(/module ApplicationCable\n  class Channel < ActionCable::Channel::Base\n/, cable)
    end

    assert_file "app/channels/application_cable/connection.rb" do |cable|
      assert_match(/module ApplicationCable\n  class Connection < ActionCable::Connection::Base\n/, cable)
    end
  end

  test "specific channel files are created under importmap" do
    run_generator ["chat"]

    assert_file "app/channels/chat_channel.rb" do |channel|
      assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
    end

    assert_file "app/javascript/channels/chat_channel.js" do |channel|
      assert_match(/import consumer from "channels\/consumer"\s+consumer\.subscriptions\.create\("ChatChannel/, channel)
    end
  end

  test "specific channel files are created under node" do
    use_under_node
    generator(["chat"]).stub(:install_javascript_dependencies, true) do
      run_generator_instance

      assert_file "app/javascript/channels/chat_channel.js" do |channel|
        assert_match(/import consumer from ".\/consumer"\s+consumer\.subscriptions\.create\("ChatChannel/, channel)
      end
    end
  end

  test "channel with multiple actions is created" do
    run_generator ["chat", "speak", "mute"]

    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/javascript/channels/chat_channel.js" do |channel|
      assert_match(/import consumer from "channels\/consumer"\s+consumer\.subscriptions\.create\("ChatChannel/, channel)
      assert_match(/,\n\n  speak/, channel)
      assert_match(/,\n\n  mute: function\(\) \{\n    return this\.perform\('mute'\);\n  \}\n\}\);/, channel)
    end
  end

  test "shared channel javascript files are created" do
    run_generator ["books"]

    assert_file "app/javascript/channels/index.js"
    assert_file "app/javascript/channels/consumer.js"
  end

  test "import channels in javascript entrypoint" do
    run_generator ["books"]

    assert_file "app/javascript/application.js" do |entrypoint|
      assert_match %r|import "channels"|, entrypoint
    end
  end

  test "import channels in javascript entrypoint under node" do
    use_under_node
    generator(["chat"]).stub(:install_javascript_dependencies, true) do
      run_generator_instance

      assert_file "app/javascript/application.js" do |entrypoint|
        assert_match %r|import "./channels"|, entrypoint
      end
    end
  end

  test "pin javascript dependencies" do
    run_generator ["chat"]

    assert_file "config/importmap.rb" do |content|
      assert_match %r|pin "@rails/actioncable"|, content
      assert_match %r|pin_all_from "app/javascript/channels"|, content
    end
  end

  test "first setup only happens once" do
    run_generator ["chat"]
    assert_file "app/javascript/channels/consumer.js"

    FileUtils.rm("#{destination_root}/app/javascript/channels/consumer.js")
    run_generator ["another"]
    assert_no_file "app/javascript/channels/consumer.js"
  end

  test "javascripts not generated when assets are skipped" do
    run_generator ["chat", "--skip-assets"]

    assert_file "app/channels/chat_channel.rb" do |channel|
      assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
    end

    assert_no_file "app/javascript/channels/chat_channel.js"
  end

  test "invokes default test framework" do
    run_generator %w(chat -t=test_unit)

    assert_file "test/channels/chat_channel_test.rb" do |test|
      assert_match(/class ChatChannelTest < ActionCable::Channel::TestCase/, test)
      assert_match(/# test "subscribes" do/, test)
      assert_match(/#   assert subscription.confirmed\?/, test)
    end
  end

  test "revoking" do
    run_generator ["chat"]
    run_generator ["chat"], behavior: :revoke

    assert_no_file "app/channels/chat_channel.rb"
    assert_no_file "app/javascript/channels/chat_channel.js"
    assert_no_file "test/channels/chat_channel_test.rb"

    assert_file "app/channels/application_cable/channel.rb"
    assert_file "app/channels/application_cable/connection.rb"
    assert_file "app/javascript/channels/consumer.js"
  end

  test "channel suffix is not duplicated" do
    run_generator ["chat_channel"]

    assert_no_file "app/channels/chat_channel_channel.rb"
    assert_file "app/channels/chat_channel.rb"

    assert_no_file "app/javascript/channels/chat_channel_channel.js"
    assert_file "app/javascript/channels/chat_channel.js"

    assert_no_file "test/channels/chat_channel_channel_test.rb"
    assert_file "test/channels/chat_channel_test.rb"
  end

  private
    def use_with_javascript
      FileUtils.mkdir_p("#{destination_root}/app/javascript")
      FileUtils.touch("#{destination_root}/app/javascript/application.js")
    end

    def use_under_importmap
      FileUtils.mkdir_p("#{destination_root}/config")
      FileUtils.touch("#{destination_root}/config/importmap.rb")
      FileUtils.rm_rf("#{destination_root}/package.json")
    end

    def use_under_node
      FileUtils.touch("#{destination_root}/package.json")
      FileUtils.rm_rf("#{destination_root}/config/importmap.rb")
    end
end