File: view_example_group.rb

package info (click to toggle)
ruby-rspec-rails 8.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: ruby: 10,902; sh: 199; makefile: 6
file content (214 lines) | stat: -rw-r--r-- 7,259 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'rspec/rails/view_assigns'
require 'rspec/rails/view_spec_methods'
require 'rspec/rails/view_path_builder'

module RSpec
  module Rails
    # @api public
    # Container class for view spec functionality.
    module ViewExampleGroup
      extend ActiveSupport::Concern
      include RSpec::Rails::RailsExampleGroup
      include ActionView::TestCase::Behavior
      include RSpec::Rails::ViewAssigns
      include RSpec::Rails::Matchers::RenderTemplate

      # @private
      module StubResolverCache
        def self.resolver_for(hash)
          @resolvers ||= {}
          @resolvers[hash] ||= ActionView::FixtureResolver.new(hash)
        end
      end

      # @private
      module ClassMethods
        def _default_helper
          base = metadata[:description].split('/')[0..-2].join('/')
          (base.camelize + 'Helper').constantize unless base.to_s.empty?
        rescue NameError
          nil
        end

        def _default_helpers
          helpers = [_default_helper].compact
          helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
          helpers
        end
      end

      # DSL exposed to view specs.
      module ExampleMethods
        extend ActiveSupport::Concern

        included do
          include ::Rails.application.routes.url_helpers
          include ::Rails.application.routes.mounted_helpers
        end

        # @overload render
        # @overload render({partial: path_to_file})
        # @overload render({partial: path_to_file}, {... locals ...})
        # @overload render({partial: path_to_file}, {... locals ...}) do ... end
        #
        # Delegates to ActionView::Base#render, so see documentation on that
        # for more info.
        #
        # The only addition is that you can call render with no arguments, and
        # RSpec will pass the top level description to render:
        #
        #     describe "widgets/new.html.erb" do
        #       it "shows all the widgets" do
        #         render # => view.render(file: "widgets/new.html.erb")
        #         # ...
        #       end
        #     end
        def render(options = {}, local_assigns = {}, &block)
          options = _default_render_options if Hash === options && options.empty?
          options = options.merge(_default_render_options) if Hash === options && options.keys == [:locals]
          super(options, local_assigns, &block)
        end

        # The instance of `ActionView::Base` that is used to render the template.
        # Use this to stub methods _before_ calling `render`.
        #
        #     describe "widgets/new.html.erb" do
        #       it "shows all the widgets" do
        #         view.stub(:foo) { "foo" }
        #         render
        #         # ...
        #       end
        #     end
        def view
          _view
        end

        # Simulates the presence of a template on the file system by adding a
        # Rails' FixtureResolver to the front of the view_paths list. Designed to
        # help isolate view examples from partials rendered by the view template
        # that is the subject of the example.
        #
        #     stub_template("widgets/_widget.html.erb" => "This content.")
        def stub_template(hash)
          controller.prepend_view_path(StubResolverCache.resolver_for(hash))
        end

        # Provides access to the params hash that will be available within the
        # view.
        #
        #     params[:foo] = 'bar'
        def params
          controller.params
        end

        # @deprecated Use `view` instead.
        def template
          RSpec.deprecate("template", replacement: "view")
          view
        end

        # @deprecated Use `rendered` instead.
        def response
          # `assert_template` expects `response` to implement a #body method
          # like an `ActionDispatch::Response` does to force the view to
          # render. For backwards compatibility, we use #response as an alias
          # for #rendered, but it needs to implement #body to avoid
          # `assert_template` raising a `NoMethodError`.
          unless rendered.respond_to?(:body)
            def rendered.body
              self
            end
          end

          rendered
        end

      private

        def _default_render_options
          formats = if ActionView::Template::Types.respond_to?(:symbols)
                      ActionView::Template::Types.symbols
                    else
                      [:html, :text, :js, :css, :xml, :json].map(&:to_s)
                    end.map { |x| Regexp.escape(x) }.join("|")

          handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
          locales = "[a-z]{2}(?:-[A-Z]{2})?"
          variants = "[^.]*"
          path_regex = %r{
          \A
          (?<template>.*?)
          (?:\.(?<locale>#{locales}))??
            (?:\.(?<format>#{formats}))??
            (?:\+(?<variant>#{variants}))??
            (?:\.(?<handler>#{handlers}))?
            \z
          }x

          # This regex should always find a match.
          # Worst case, everything will be nil, and :template will just be
          # the original string.
          match = path_regex.match(_default_file_to_render)

          render_options = { template: match[:template] }
          render_options[:handlers] = [match[:handler].to_sym] if match[:handler]
          render_options[:formats] = [match[:format].to_sym] if match[:format]
          render_options[:locales] = [match[:locale].to_sym] if match[:locale]
          render_options[:variants] = [match[:variant].to_sym] if match[:variant]

          render_options
        end

        def _path_parts
          _default_file_to_render.split("/")
        end

        def _controller_path
          _path_parts[0..-2].join("/")
        end

        def _inferred_action
          _path_parts.last.split(".").first
        end

        def _include_controller_helpers
          helpers = controller._helpers
          view.singleton_class.class_exec do
            include helpers unless included_modules.include?(helpers)
          end
        end
      end

      included do
        include ExampleMethods

        helper(*_default_helpers)

        before do
          _include_controller_helpers
          view.lookup_context.prefixes.prepend(_controller_path)

          controller.controller_path = _controller_path

          path_params_to_merge = {}
          path_params_to_merge[:controller] = _controller_path
          path_params_to_merge[:action] = _inferred_action unless _inferred_action =~ /^_/

          path_params = controller.request.path_parameters

          controller.request.path_parameters = path_params.reverse_merge(path_params_to_merge)
          controller.request.path = ViewPathBuilder.new(::Rails.application.routes).path_for(controller.request.path_parameters)
          ViewSpecMethods.add_to(::ActionView::TestCase::TestController)
        end

        after do
          ViewSpecMethods.remove_from(::ActionView::TestCase::TestController)
        end

        let(:_default_file_to_render) do |example|
          example.example_group.top_level_description
        end
      end
    end
  end
end