-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathjbuilder_generator_test.rb
70 lines (56 loc) · 2.51 KB
/
jbuilder_generator_test.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
require 'test_helper'
require 'rails/generators/test_case'
require 'generators/rails/jbuilder_generator'
class JbuilderGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::JbuilderGenerator
arguments %w(Post title body:text password:digest)
destination File.expand_path('../tmp', __FILE__)
setup :prepare_destination
test 'views are generated' do
run_generator
%w(index show).each do |view|
assert_file "app/views/posts/#{view}.json.jbuilder"
end
assert_file "app/views/posts/_post.json.jbuilder"
end
test 'index content' do
run_generator
assert_file 'app/views/posts/index.json.jbuilder' do |content|
assert_match %r{json\.array! @posts, partial: "posts/post", as: :post}, content
end
assert_file 'app/views/posts/show.json.jbuilder' do |content|
assert_match %r{json\.partial! "posts/post", post: @post}, content
end
assert_file 'app/views/posts/_post.json.jbuilder' do |content|
assert_match %r{json\.extract! post, :id, :title, :body}, content
assert_match %r{:created_at, :updated_at}, content
assert_match %r{json\.url post_url\(post, format: :json\)}, content
end
end
test 'timestamps are not generated in partial with --no-timestamps' do
run_generator %w(Post title body:text --no-timestamps)
assert_file 'app/views/posts/_post.json.jbuilder' do |content|
assert_match %r{json\.extract! post, :id, :title, :body$}, content
assert_no_match %r{:created_at, :updated_at}, content
end
end
test 'namespaced views are generated correctly for index' do
run_generator %w(Admin::Post --model-name=Post)
assert_file 'app/views/admin/posts/index.json.jbuilder' do |content|
assert_match %r{json\.array! @posts, partial: "admin/posts/post", as: :post}, content
end
assert_file 'app/views/admin/posts/show.json.jbuilder' do |content|
assert_match %r{json\.partial! "admin/posts/post", post: @post}, content
end
end
if Rails::VERSION::MAJOR >= 6
test 'handles virtual attributes' do
run_generator %w(Message content:rich_text video:attachment photos:attachments)
assert_file 'app/views/messages/_message.json.jbuilder' do |content|
assert_match %r{json\.content message\.content\.to_s}, content
assert_match %r{json\.video url_for\(message\.video\)}, content
assert_match %r{json\.photos do\n json\.array!\(message\.photos\) do \|photo\|\n json\.id photo\.id\n json\.url url_for\(photo\)\n end\nend}, content
end
end
end
end