File: render_layout_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 (151 lines) | stat: -rw-r--r-- 4,230 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

require "abstract_unit"
require "test_renderable"

module ControllerLayouts
  class ImplicitController < ::ApplicationController
    self.view_paths = [ActionView::FixtureResolver.new(
      "layouts/application.html.erb" => "Main <%= yield %> Layout",
      "layouts/override.html.erb"    => "Override! <%= yield %>",
      "basic.html.erb"               => "Hello world!",
      "controller_layouts/implicit/layout_false.html.erb" => "hi(layout_false.html.erb)"
    )]

    def index
      render template: "basic"
    end

    def override
      render template: "basic", layout: "override"
    end

    def override_renderable
      render TestRenderable.new, layout: "override"
    end

    def layout_false
      render layout: false
    end

    def builder_override
    end
  end

  class ImplicitNameController < ::ApplicationController
    self.view_paths = [ActionView::FixtureResolver.new(
      "layouts/controller_layouts/implicit_name.html.erb" => "Implicit <%= yield %> Layout",
      "basic.html.erb" => "Hello world!"
    )]

    def index
      render template: "basic"
    end

    def renderable
      render TestRenderable.new
    end
  end

  class RenderLayoutTest < Rack::TestCase
    test "rendering a normal template, but using the implicit layout" do
      get "/controller_layouts/implicit/index"

      assert_body   "Main Hello world! Layout"
      assert_status 200
    end

    test "rendering a normal template, but using an implicit NAMED layout" do
      get "/controller_layouts/implicit_name/index"

      assert_body "Implicit Hello world! Layout"
      assert_status 200
    end

    test "rendering a renderable object, using the implicit layout" do
      get "/controller_layouts/implicit_name/renderable"

      assert_body "Implicit Hello, World! Layout"
      assert_status 200
    end

    test "rendering a renderable object, using the override layout" do
      get "/controller_layouts/implicit/override_renderable"

      assert_body "Override! Hello, World!"
      assert_status 200
    end

    test "overriding an implicit layout with render :layout option" do
      get "/controller_layouts/implicit/override"
      assert_body "Override! Hello world!"
    end
  end

  class LayoutOptionsTest < Rack::TestCase
    testing ControllerLayouts::ImplicitController

    test "rendering with :layout => false leaves out the implicit layout" do
      get :layout_false
      assert_response "hi(layout_false.html.erb)"
    end
  end

  class MismatchFormatController < ::ApplicationController
    self.view_paths = [ActionView::FixtureResolver.new(
      "layouts/application.html.erb" => "<html><%= yield %></html>",
      "controller_layouts/mismatch_format/index.xml.builder" => "xml.instruct!",
      "controller_layouts/mismatch_format/implicit.builder" => "xml.instruct!",
      "controller_layouts/mismatch_format/explicit.js.erb" => "alert('foo');"
    )]

    def explicit
      render layout: "application"
    end
  end

  class MismatchFormatTest < Rack::TestCase
    testing ControllerLayouts::MismatchFormatController

    XML_INSTRUCT = %Q(<?xml version="1.0" encoding="UTF-8"?>\n)

    test "if XML is selected, an HTML template is not also selected" do
      get :index, params: { format: "xml" }
      assert_response XML_INSTRUCT
    end

    test "if XML is implicitly selected, an HTML template is not also selected" do
      get :implicit
      assert_response XML_INSTRUCT
    end

    test "a layout for JS is ignored even if explicitly provided for HTML" do
      get :explicit, params: { format: "js" }
      assert_response "alert('foo');"
    end
  end

  class FalseLayoutMethodController < ::ApplicationController
    self.view_paths = [ActionView::FixtureResolver.new(
      "controller_layouts/false_layout_method/index.js.erb" => "alert('foo');"
    )]

    layout :which_layout?

    def which_layout?
      false
    end

    def index
    end
  end

  class FalseLayoutMethodTest < Rack::TestCase
    testing ControllerLayouts::FalseLayoutMethodController

    test "access false layout returned by a method/proc" do
      get :index, params: { format: "js" }
      assert_response "alert('foo');"
    end
  end
end