Skip to content

Commit 2121d36

Browse files
committed
Add tests + make #jsonapi_pointers public.
1 parent 78c0ee9 commit 2121d36

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

lib/jsonapi/rails/action_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ def _deserializable(key, options, fallback, &block)
3333
end
3434
end
3535

36-
private
37-
3836
def jsonapi_pointers
3937
request.env[JSONAPI_POINTERS_KEY]
4038
end

spec/action_controller_spec.rb

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,71 @@
11
require 'rails_helper'
22

3-
RSpec.describe ActionController::Base do
4-
it 'exposes the deserialization mapping via the jsonapi_pointers method' do
5-
pointers = { id: '/data/id', type: '/data/type' }
6-
7-
allow(subject).to receive(:request) do
8-
OpenStruct.new(
9-
env: {
10-
JSONAPI::Rails::ActionController::JSONAPI_POINTERS_KEY => pointers
3+
describe ActionController::Base, type: :controller do
4+
describe '.deserializable_resource' do
5+
controller do
6+
deserializable_resource :user
7+
8+
def create
9+
render plain: 'ok'
10+
end
11+
end
12+
13+
let(:payload) do
14+
{
15+
_jsonapi: {
16+
'data' => {
17+
'type' => 'users',
18+
'attributes' => { 'foo' => 'bar', 'bar' => 'baz' }
19+
}
1120
}
12-
)
21+
}
1322
end
1423

15-
expect(subject.send(:jsonapi_pointers)).to equal pointers
24+
it 'makes the deserialized resource available in params' do
25+
post :create, params: payload
26+
27+
expected = { 'type' => 'users', 'foo' => 'bar', 'bar' => 'baz' }
28+
expect(controller.params[:user]).to eq(expected)
29+
end
30+
31+
it 'makes the deserialization mapping available via #jsonapi_pointers' do
32+
post :create, params: payload
33+
34+
expected = { foo: '/data/attributes/foo',
35+
bar: '/data/attributes/bar',
36+
type: '/data/type' }
37+
expect(controller.jsonapi_pointers).to eq(expected)
38+
end
39+
end
40+
41+
describe '#render jsonapi:' do
42+
controller do
43+
def index
44+
serializer = Class.new(JSONAPI::Serializable::Resource) do
45+
type :users
46+
attribute :name
47+
end
48+
user = OpenStruct.new(id: 1, name: 'Lucas')
49+
50+
render jsonapi: user, class: serializer
51+
end
52+
end
53+
54+
subject { JSON.parse(response.body) }
55+
let(:serialized_user) do
56+
{
57+
'data' => {
58+
'id' => '1',
59+
'type' => 'users',
60+
'attributes' => { 'name' => 'Lucas' } }
61+
}
62+
end
63+
64+
it 'renders a JSON API document' do
65+
get :index
66+
67+
expect(response.content_type).to eq('application/vnd.api+json')
68+
is_expected.to eq(serialized_user)
69+
end
1670
end
1771
end

0 commit comments

Comments
 (0)