-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
view_rendering_spec.rb
105 lines (86 loc) · 2.54 KB
/
view_rendering_spec.rb
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
# frozen_string_literal: true
require "hanami"
require "hanami/action"
require "hanami/view"
require "slim"
RSpec.describe "View rendering in application actions", :application_integration do
specify "Views render with a request-specific context object" do
with_tmp_directory(Dir.mktmpdir) do
write "config/application.rb", <<~RUBY
require "hanami"
module TestApp
class Application < Hanami::Application
end
end
RUBY
write "slices/main/lib/main/action.rb", <<~RUBY
# auto_register: false
module Main
class Action < Hanami::Action
end
end
RUBY
write "slices/main/lib/main/actions/test_action.rb", <<~RUBY
require "main/action"
module Main
module Actions
class TestAction < Main::Action
include Deps[view: "views.test_view"]
def handle(req, res)
res[:job] = "Singer"
res.render view, name: req.params[:name]
end
end
end
end
RUBY
write "slices/main/lib/main/view/context.rb", <<~RUBY
module Main
class View < Hanami::View
class Context < Hanami::View::Context
def request
_options.fetch(:request)
end
def response
_options.fetch(:response)
end
end
end
end
RUBY
write "slices/main/lib/main/view.rb", <<~RUBY
# auto_register: false
module Main
class View < Hanami::View
end
end
RUBY
write "slices/main/lib/main/views/test_view.rb", <<~RUBY
require "main/view"
module Main
module Views
class TestView < Main::View
expose :name, :job
end
end
end
RUBY
write "slices/main/web/templates/layouts/application.html.slim", <<~SLIM
html
body
== yield
SLIM
write "slices/main/web/templates/test_view.html.slim", <<~'SLIM'
h1 Hello, #{name}
- request.params.to_h.values.sort.each do |value|
p = value
p = job
SLIM
require "hanami/init"
action = Main::Slice["actions.test_action"]
response = action.(name: "Jennifer", last_name: "Lopez")
rendered = response.body[0]
expect(rendered).to eq "<html><body><h1>Hello, Jennifer</h1><p>Jennifer</p><p>Lopez</p><p>Singer</p></body></html>"
end
end
end