|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActiveModel |
| 4 | + class Serializer |
| 5 | + class Adapter |
| 6 | + class JsonApi |
| 7 | + class ResourceTypeConfigTest < Minitest::Test |
| 8 | + def setup |
| 9 | + @author = Author.new(id: 1, name: 'Steve K.') |
| 10 | + @author.bio = nil |
| 11 | + @author.roles = [] |
| 12 | + @blog = Blog.new(id: 23, name: 'AMS Blog') |
| 13 | + @post = Post.new(id: 42, title: 'New Post', body: 'Body') |
| 14 | + @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!') |
| 15 | + @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') |
| 16 | + @post.comments = [@comment] |
| 17 | + @post.blog = @blog |
| 18 | + @anonymous_post.comments = [] |
| 19 | + @anonymous_post.blog = nil |
| 20 | + @comment.post = @post |
| 21 | + @comment.author = nil |
| 22 | + @post.author = @author |
| 23 | + @anonymous_post.author = nil |
| 24 | + @blog = Blog.new(id: 1, name: "My Blog!!") |
| 25 | + @blog.writer = @author |
| 26 | + @blog.articles = [@post, @anonymous_post] |
| 27 | + @author.posts = [] |
| 28 | + end |
| 29 | + |
| 30 | + def with_jsonapi_resource_type type |
| 31 | + old_type = ActiveModel::Serializer.config[:jsonapi_resource_type] |
| 32 | + ActiveModel::Serializer.config[:jsonapi_resource_type] = type |
| 33 | + yield |
| 34 | + ensure |
| 35 | + ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type |
| 36 | + end |
| 37 | + |
| 38 | + def test_config_plural |
| 39 | + with_jsonapi_resource_type :plural do |
| 40 | + serializer = CommentSerializer.new(@comment) |
| 41 | + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) |
| 42 | + ActionController::Base.cache_store.clear |
| 43 | + assert_equal('comments', adapter.serializable_hash[:data][:type]) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + def test_config_singular |
| 48 | + with_jsonapi_resource_type :singular do |
| 49 | + serializer = CommentSerializer.new(@comment) |
| 50 | + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) |
| 51 | + ActionController::Base.cache_store.clear |
| 52 | + assert_equal('comment', adapter.serializable_hash[:data][:type]) |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments