Skip to content

Commit 842d903

Browse files
committed
Add tests for deserialization + make #jsonapi_pointers public.
1 parent 78c0ee9 commit 842d903

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
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: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
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' }
3+
describe ActionController::Base, type: :controller do
4+
context 'when specifying a deserializable resource' do
5+
controller do
6+
deserializable_resource :user
67

7-
allow(subject).to receive(:request) do
8-
OpenStruct.new(
9-
env: {
10-
JSONAPI::Rails::ActionController::JSONAPI_POINTERS_KEY => pointers
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' 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' 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
1639
end
1740
end

0 commit comments

Comments
 (0)