-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgrape_rabl_layout_spec.rb
100 lines (87 loc) · 2.85 KB
/
grape_rabl_layout_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
require 'spec_helper'
describe 'Grape::Rabl layout' do
let(:parsed_response) { JSON.parse(last_response.body) }
subject do
Class.new(Grape::API)
end
before do
subject.format :json
subject.formatter :json, Grape::Formatter::Rabl
subject.before do
env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views/layout_test"
end
end
def app
subject
end
context 'default' do
it 'proper render with default layout' do
subject.get('/about', rabl: 'user') do
@user = OpenStruct.new(name: 'LTe')
@project = OpenStruct.new(name: 'First')
@status = 200
end
get('/about')
expect(parsed_response).to eq(
JSON.parse(%({"status":200,"result":{"user":{"name":"LTe","project":{"name":"First"}}}}))
)
end
end
context 'tilt layout is setup' do
before do
subject.before { env['api.tilt.layout'] = 'layouts/another' }
end
it 'proper render with specified layout' do
subject.get('/about', rabl: 'user') do
@user = OpenStruct.new(name: 'LTe')
@project = OpenStruct.new(name: 'First')
@status = 200
end
get('/about')
puts last_response.body
expect(parsed_response).to eq(
JSON.parse(%({"result":{"user":{"name":"LTe","project":{"name":"First"}}}}))
)
end
end
context 'layout cache' do
before do
@views_dir = FileUtils.mkdir_p("#{File.expand_path('..', File.dirname(__FILE__))}/tmp")[0]
@layout = "#{@views_dir}/layouts/application.rabl"
FileUtils.cp_r("#{File.dirname(__FILE__)}/views/layout_test/.", @views_dir)
subject.before { env['api.tilt.root'] = "#{File.expand_path('..', File.dirname(__FILE__))}/tmp" }
subject.get('/home', rabl: 'user') do
@user = OpenStruct.new(name: 'LTe', email: 'email@example.com')
@project = OpenStruct.new(name: 'First')
@status = 200
end
end
after do
Grape::Rabl.reset_configuration!
FileUtils.rm_f(@views_dir)
end
it 'should serve from cache if cache_template_loading' do
Grape::Rabl.configure do |config|
config.cache_template_loading = true
end
get '/home'
expect(last_response.status).to eq(200)
old_response = last_response.body
open(@layout, 'a') { |f| f << 'node(:test) { "test" }' }
get '/home'
expect(last_response.status).to eq(200)
new_response = last_response.body
expect(old_response).to eq(new_response)
end
it 'should serve new template if cache_template_loading' do
get '/home'
expect(last_response.status).to eq(200)
old_response = last_response.body
open(@layout, 'a') { |f| f << 'node(:test) { "test" }' }
get '/home'
expect(last_response.status).to eq(200)
new_response = last_response.body
expect(old_response).not_to eq(new_response)
end
end
end