Skip to content

Support dasherized keys #24

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions lib/jsonapi_spec_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'json'
require 'jsonapi_spec_helpers/version'
require 'jsonapi_spec_helpers/helpers'
require 'jsonapi_spec_helpers/string_helpers'
require 'jsonapi_spec_helpers/payload'
require 'jsonapi_spec_helpers/payload_sanitizer'
require 'jsonapi_spec_helpers/errors'
Expand All @@ -19,7 +20,7 @@ def self.load_payloads!
Dir[Rails.root.join('spec/payloads/**/*.rb')].each { |f| require f }
end

def assert_payload(name, record, json, &blk)
def assert_payload(name, record, json, dasherized: false, &blk)
unless payload = JsonapiSpecHelpers::Payload.registry[name]
raise "No payloads registered for '#{name}'"
end
Expand All @@ -32,10 +33,12 @@ def assert_payload(name, record, json, &blk)
aggregate_failures "payload has correct key/values" do
payload.keys.each_pair do |attribute, options|
prc = options[:proc]
if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
if (expect(json).to have_payload_key(attribute, options[:allow_nil], dasherized)) == true
unless options[:allow_nil]
output = instance_exec(record, &prc)
expect(json[attribute.to_s]).to match_payload(attribute, output)
attribute = attribute.to_s
attribute = StringHelpers.dasherize(attribute) if dasherized
expect(json[attribute]).to match_payload(attribute, output)

if options[:type]
expect(json[attribute.to_s]).to match_type(attribute, options[:type])
Expand All @@ -47,8 +50,9 @@ def assert_payload(name, record, json, &blk)
payload.no_keys.each do |no_key|
expect(json).to_not have_payload_key(no_key, {})
end

unexpected_keys = json.keys - payload.keys.keys.map(&:to_s)
unexpected_keys = json.keys - payload.keys.keys.map do |key|
dasherized ? StringHelpers.dasherize(key) : key.to_s
end
unexpected_keys.reject! { |k| %w(id jsonapi_type).include?(k) }
unexpected_keys.each do |key|
expect(key).to be_not_in_payload
Expand Down
8 changes: 5 additions & 3 deletions lib/jsonapi_spec_helpers/matchers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rspec/matchers'
require 'jsonapi_spec_helpers/string_helpers'

RSpec::Matchers.define :match_payload do |attribute, expected|
match do |actual|
Expand Down Expand Up @@ -28,10 +29,11 @@
end
end

RSpec::Matchers.define :have_payload_key do |expected, allow_nil|
RSpec::Matchers.define :have_payload_key do |expected, allow_nil, dasherized|
match do |json|
@has_key = json.has_key?(expected.to_s)
@has_value = !json[expected.to_s].nil?
expected = dasherized ? JsonapiSpecHelpers::StringHelpers.dasherize(expected) : expected.to_s
@has_key = json.has_key?(expected)
@has_value = !json[expected].nil?

if allow_nil
@has_key
Expand Down
11 changes: 11 additions & 0 deletions lib/jsonapi_spec_helpers/string_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module JsonapiSpecHelpers
class StringHelpers
class << self
def dasherize(attribute)
return attribute.to_s unless attribute.to_s.include?('_')

attribute.to_s.gsub('_','-')
end
end
end
end
34 changes: 34 additions & 0 deletions spec/assert_payload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,40 @@
end
end

context 'when dasherized: true' do
before do
JsonapiSpecHelpers::Payload.register(:post_with_dasherized_attribute) do
key(:dasherized_attribute)
end
end

let(:dasherized_value) { 'some value' }

let(:post_record) do
attrs = {
id: 1,
dasherized_attribute: dasherized_value
}
double(attrs).as_null_object
end

let(:json) do
{
'data' => {
'type' => 'posts',
'id' => '1',
'attributes' => {
'dasherized-attribute' => dasherized_value
}
}
}
end

it 'passes assertion' do
assert_payload(:post_with_dasherized_attribute, post_record, json_item, dasherized: true)
end
end

context 'when json value matches payload value, but wrong type' do
before do
json['data']['attributes']['views'] = '100'
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
require 'action_dispatch'
require 'jsonapi_spec_helpers'
require 'pry'
RSpec.configure do |c|
c.filter_run focus: true
c.run_all_when_everything_filtered = true
end