Skip to content

Commit a8d47c2

Browse files
authored
Improve specs (jsonapi-rb#33)
* Add spec for the railtie and (de)serialization. * Make #jsonapi_pointers public. * Cleanup dummy test app.
1 parent 0605a29 commit a8d47c2

File tree

12 files changed

+76
-238
lines changed

12 files changed

+76
-238
lines changed

jsonapi-rails.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ Gem::Specification.new do |spec|
2020
spec.add_development_dependency 'sqlite3'
2121
spec.add_development_dependency 'rake', '~> 11.3'
2222
spec.add_development_dependency 'rspec-rails', '~> 3.5'
23-
spec.add_development_dependency 'dry-validation', '~> 0.10'
2423
end

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: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,72 @@
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+
}
20+
}
21+
}
22+
end
23+
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' }
1161
}
12-
)
62+
}
1363
end
1464

15-
expect(subject.send(:jsonapi_pointers)).to equal pointers
65+
it 'renders a JSON API document' do
66+
get :index
67+
68+
expect(response.content_type).to eq('application/vnd.api+json')
69+
is_expected.to eq(serialized_user)
70+
end
1671
end
1772
end

spec/dummy/app/controllers/tweets_controller.rb

Lines changed: 0 additions & 49 deletions
This file was deleted.

spec/dummy/app/deserializable/deserializable_tweet.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

spec/dummy/app/models/tweet.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

spec/dummy/app/models/user.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.

spec/dummy/app/serializable/serializable_tweet.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

spec/dummy/app/serializable/serializable_user.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

spec/rails_helper.rb

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
require 'spec_helper'
77
require 'rspec/rails'
88
# Add additional requires below this line. Rails is not loaded until this point!
9-
require 'dry-validation'
109

1110
# Requires supporting ruby files with custom matchers and macros, etc, in
1211
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
@@ -56,40 +55,3 @@
5655
# arbitrary gems may also be filtered via:
5756
# config.filter_gems_from_backtrace("gem name")
5857
end
59-
60-
# Custom RSpec matchers
61-
module JSONAPI
62-
module RSpec
63-
class BeValidJsonapiMatcher
64-
def matches?(body)
65-
JSONAPI.parse_response!(JSON.parse(body))
66-
true
67-
end
68-
end
69-
70-
def be_valid_jsonapi
71-
BeValidJsonapiMatcher.new
72-
end
73-
74-
class MatchSchemaMatcher
75-
def matches?(body, &block)
76-
schema = Dry::Validation.Schema(&block)
77-
@body = JSON.parse(body) unless body.is_a?(Hash)
78-
@result = schema.call(@body.with_indifferent_access)
79-
@result.success?
80-
end
81-
82-
def failure_message
83-
"#{@body} #{@result.errors}"
84-
end
85-
end
86-
87-
def match_schema(&block)
88-
MatchSchemaMatcher.new(&block)
89-
end
90-
end
91-
end
92-
93-
RSpec.configure do |config|
94-
config.include JSONAPI::RSpec
95-
end

spec/railtie_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'rails_helper'
2+
3+
describe JSONAPI::Rails::Railtie do
4+
it 'registers the JSON API MIME type' do
5+
expect(Mime[:jsonapi]).to eq('application/vnd.api+json')
6+
end
7+
8+
it 'registers the params parser for the JSON API MIME type' do
9+
expect(::ActionDispatch::Request.parameter_parsers[:jsonapi]).not_to be_nil
10+
end
11+
end

spec/requests/crud_spec.rb

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)