|
1 | 1 | require 'rails_helper'
|
2 | 2 |
|
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 | + } |
11 | 20 | }
|
12 |
| - ) |
| 21 | + } |
13 | 22 | end
|
14 | 23 |
|
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 |
16 | 70 | end
|
17 | 71 | end
|
0 commit comments