Skip to content

Modify have_meta to include an exactly chain #22

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 2 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Available matchers:
* `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
* `expect(document).to have_meta`
* `expect(document).to have_meta('foo' => 'bar')`
* `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
* `expect(document).to have_jsonapi_object`
* `expect(document).to have_jsonapi_object('version' => '1.0')`

Expand Down
10 changes: 9 additions & 1 deletion lib/jsonapi/rspec/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ module Meta
::RSpec::Matchers.define :have_meta do |val|
match do |actual|
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
return false unless actual.key?('meta')
return true unless val

val = JSONAPI::RSpec.as_indifferent_hash(val)
return false unless val <= actual['meta']

!@exactly || (@exactly && val.size == actual['meta'].size)
end

actual.key?('meta') && (!val || actual['meta'] == val)
chain :exactly do
@exactly = true
end
end
end
Expand Down
29 changes: 23 additions & 6 deletions spec/jsonapi/meta_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec, '#have_meta' do
let(:doc) do
{
'meta' => {
'one' => 'I',
'two' => 'II',
'three' => 'III'
}
}
end
context 'when providing no value' do
it 'succeeds when meta is present' do
expect('meta' => {}).to have_meta
expect(doc).to have_meta
end

it 'fails when meta is absent' do
Expand All @@ -17,16 +26,24 @@
after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false }

it do
expect('meta' => { 'foo' => 'bar' }).to have_meta(foo: :bar)
expect(doc).to have_meta(one: 'I')
end
end

it 'succeeds when meta matches' do
expect('meta' => { foo: 'bar' }).to have_meta(foo: 'bar')
it 'succeeds when meta includes the value' do
expect(doc).to have_meta('one' => 'I')
end

it 'fails when meta does not include the value' do
expect(doc).not_to have_meta('one' => 'II')
end

it 'succeeds when meta exactly matches the value' do
expect(doc).to have_meta({ 'one' => 'I', 'two' => 'II', 'three' => 'III' }).exactly
end

it 'fails when meta mismatches' do
expect('meta' => { foo: 'bar' }).not_to have_meta(bar: 'baz')
it 'succeeds when meta does not exactly match the value' do
expect(doc).not_to have_meta({ 'one' => 'foo', 'two' => 'II', 'three' => 'III' }).exactly
end

it 'fails when meta is absent' do
Expand Down