Skip to content

Commit b6085bc

Browse files
sa73917stas
authored andcommitted
Updated tests for jsonapi_indifferent_hash. Closes #18
1 parent 1abbf3a commit b6085bc

File tree

7 files changed

+78
-40
lines changed

7 files changed

+78
-40
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ Style/FrozenStringLiteralComment:
66

77
Style/Documentation:
88
Enabled: false
9+
10+
Metrics/BlockLength:
11+
Exclude:
12+
- 'spec/*/*_spec.rb'
13+
14+
Style/IfUnlessModifier:
15+
Enabled: false

lib/jsonapi/rspec/jsonapi_object.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module JsonapiObject
44
::RSpec::Matchers.define :have_jsonapi_object do |val|
55
match do |actual|
66
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7-
actual.key?('jsonapi') &&
8-
(!val || actual['jsonapi'] == val)
7+
val = JSONAPI::RSpec.as_indifferent_hash(val)
8+
9+
actual.key?('jsonapi') && (!val || actual['jsonapi'] == val)
910
end
1011
end
1112
end

lib/jsonapi/rspec/meta.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module Meta
44
::RSpec::Matchers.define :have_meta do |val|
55
match do |actual|
66
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7-
actual.key?('meta') &&
8-
(!val || actual['meta'] == val)
7+
val = JSONAPI::RSpec.as_indifferent_hash(val)
8+
9+
actual.key?('meta') && (!val || actual['meta'] == val)
910
end
1011
end
1112
end

lib/jsonapi/rspec/relationships.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Relationships
1111

1212
chain :with_data do |val|
1313
@data_set = true
14-
@data_val = val
14+
@data_val = JSONAPI::RSpec.as_indifferent_hash(val)
1515
end
1616

1717
failure_message do |actual|
@@ -29,7 +29,7 @@ module Relationships
2929
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
3030
return false unless actual.key?('relationships')
3131

32-
rels.all? { |rel| actual['relationships'].key?(rel) }
32+
rels.map(&:to_s).all? { |rel| actual['relationships'].key?(rel) }
3333
end
3434
end
3535
end

spec/jsonapi/jsonapi_object_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
end
1313

1414
context 'when providing a value' do
15+
context 'with jsonapi indifferent hash enabled' do
16+
before(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = true }
17+
after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false }
18+
19+
it do
20+
expect('jsonapi' => { 'version' => '1.0' })
21+
.to have_jsonapi_object(version: '1.0')
22+
end
23+
end
24+
1525
it 'succeeds when jsonapi object matches' do
1626
expect('jsonapi' => { 'version' => '1.0' })
1727
.to have_jsonapi_object('version' => '1.0')

spec/jsonapi/meta_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
end
1313

1414
context 'when providing a value' do
15+
context 'with jsonapi indifferent hash enabled' do
16+
before(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = true }
17+
after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false }
18+
19+
it do
20+
expect('meta' => { 'foo' => 'bar' }).to have_meta(foo: :bar)
21+
end
22+
end
23+
1524
it 'succeeds when meta matches' do
1625
expect('meta' => { foo: 'bar' }).to have_meta(foo: 'bar')
1726
end

spec/jsonapi/relationships_spec.rb

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,60 @@
1-
RSpec.describe JSONAPI::RSpec do
2-
json_doc =
1+
require 'spec_helper'
2+
3+
RSpec.describe JSONAPI::RSpec, '#have_relationship(s)' do
4+
let(:doc) do
35
{
46
'relationships' => {
5-
'posts' => {
6-
'data' => {
7-
'id' => '1',
8-
'type' => 'posts'
9-
}
7+
'user' => {
8+
'data' => { 'id' => '1', 'type' => 'user' }
109
},
1110
'comments' => {
12-
'data' => [{
13-
'id' => '1',
14-
'type' => 'posts'
15-
}, {
16-
'id' => '2',
17-
'type' => 'hides'
18-
}]
11+
'data' => [
12+
{ 'id' => '1', 'type' => 'comment' },
13+
{ 'id' => '2', 'type' => 'comment' }
14+
]
1915
}
2016
}
2117
}
18+
end
2219

23-
describe '#have_relationship' do
24-
context 'when relationships is present' do
25-
it { expect(json_doc).to have_relationship('posts') }
26-
it { expect(json_doc).not_to have_relationship('mails') }
27-
it { expect(json_doc).to have_relationship('posts').with_data({ 'id' => '1', 'type' => 'posts' }) }
28-
it do
29-
expect(json_doc).to have_relationship('comments').with_data(
30-
[{ 'id' => '1', 'type' => 'posts' }, { 'id' => '2', 'type' => 'hides' }]
31-
)
32-
end
33-
end
20+
it { expect(doc).not_to have_relationships('user', 'comments', 'authors') }
21+
it { expect(doc).to have_relationships('user', 'comments') }
3422

35-
context 'when relationships is not present' do
36-
it { expect({}).not_to have_relationship('posts') }
37-
end
23+
it { expect(doc).not_to have_relationship('authors') }
24+
it { expect(doc).to have_relationship('user') }
25+
26+
it do
27+
expect(doc).to have_relationship('user').with_data(
28+
{ 'id' => '1', 'type' => 'user' }
29+
)
3830
end
3931

40-
describe '#have_relationships' do
41-
context 'when relationships is present' do
42-
it { expect(json_doc).to have_relationships('posts', 'comments') }
43-
it { expect(json_doc).not_to have_relationships('posts', 'comments', 'mails') }
32+
it do
33+
expect(doc).to have_relationship('comments').with_data(
34+
[
35+
{ 'id' => '1', 'type' => 'comment' },
36+
{ 'id' => '2', 'type' => 'comment' }
37+
]
38+
)
39+
end
40+
41+
context 'with jsonapi indifferent hash enabled' do
42+
before(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = true }
43+
after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false }
44+
45+
it { expect(doc).to have_relationships(:user, :comments) }
46+
47+
it do
48+
expect(doc).to have_relationship('user').with_data(id: '1', type: :user)
4449
end
4550

46-
context 'when relationships is not present' do
47-
it { expect({}).not_to have_relationships('posts', 'comments') }
51+
it do
52+
expect(doc).to have_relationship('comments').with_data(
53+
[
54+
{ id: '1', type: 'comment' },
55+
{ id: '2', type: 'comment' }
56+
]
57+
)
4858
end
4959
end
5060
end

0 commit comments

Comments
 (0)