Skip to content

Improve specs #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/jsonapi/rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def _deserializable(key, options, fallback, &block)
end
end

private

def jsonapi_pointers
request.env[JSONAPI_POINTERS_KEY]
end
Expand Down
74 changes: 64 additions & 10 deletions spec/action_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,71 @@
require 'rails_helper'

RSpec.describe ActionController::Base do
it 'exposes the deserialization mapping via the jsonapi_pointers method' do
pointers = { id: '/data/id', type: '/data/type' }

allow(subject).to receive(:request) do
OpenStruct.new(
env: {
JSONAPI::Rails::ActionController::JSONAPI_POINTERS_KEY => pointers
describe ActionController::Base, type: :controller do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block has too many lines. [30/25]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block has too many lines. [56/25]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block has too many lines. [57/25]

describe '.deserializable_resource' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block has too many lines. [28/25]

controller do
deserializable_resource :user

def create
render plain: 'ok'
end
end

let(:payload) do
{
_jsonapi: {
'data' => {
'type' => 'users',
'attributes' => { 'foo' => 'bar', 'bar' => 'baz' }
}
}
)
}
end

expect(subject.send(:jsonapi_pointers)).to equal pointers
it 'makes the deserialized resource available in params' do
post :create, params: payload

expected = { 'type' => 'users', 'foo' => 'bar', 'bar' => 'baz' }
expect(controller.params[:user]).to eq(expected)
end

it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload

expected = { foo: '/data/attributes/foo',
bar: '/data/attributes/bar',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end

describe '#render jsonapi:' do
controller do
def index
serializer = Class.new(JSONAPI::Serializable::Resource) do
type :users
attribute :name
end
user = OpenStruct.new(id: 1, name: 'Lucas')

render jsonapi: user, class: serializer
end
end

subject { JSON.parse(response.body) }
let(:serialized_user) do
{
'data' => {
'id' => '1',
'type' => 'users',
'attributes' => { 'name' => 'Lucas' } }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing hash brace must be on the line after the last hash element when opening brace is on a separate line from the first hash element.

}
end

it 'renders a JSON API document' do
get :index

expect(response.content_type).to eq('application/vnd.api+json')
is_expected.to eq(serialized_user)
end
end
end
11 changes: 11 additions & 0 deletions spec/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.


describe JSONAPI::Rails::Railtie do
it 'registers the JSON API MIME type' do
expect(Mime[:jsonapi]).to eq('application/vnd.api+json')
end

it 'registers the params parser for the JSON API MIME type' do
expect(::ActionDispatch::Request.parameter_parsers[:jsonapi]).not_to be_nil
end
end