Skip to content

Commit a0f8249

Browse files
committed
Support dasherized keys
1 parent 4f65115 commit a0f8249

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

lib/jsonapi_spec_helpers.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'json'
22
require 'jsonapi_spec_helpers/version'
33
require 'jsonapi_spec_helpers/helpers'
4+
require 'jsonapi_spec_helpers/string_helpers'
45
require 'jsonapi_spec_helpers/payload'
56
require 'jsonapi_spec_helpers/payload_sanitizer'
67
require 'jsonapi_spec_helpers/errors'
@@ -19,7 +20,7 @@ def self.load_payloads!
1920
Dir[Rails.root.join('spec/payloads/**/*.rb')].each { |f| require f }
2021
end
2122

22-
def assert_payload(name, record, json, &blk)
23+
def assert_payload(name, record, json, dasherized: false, &blk)
2324
unless payload = JsonapiSpecHelpers::Payload.registry[name]
2425
raise "No payloads registered for '#{name}'"
2526
end
@@ -32,10 +33,12 @@ def assert_payload(name, record, json, &blk)
3233
aggregate_failures "payload has correct key/values" do
3334
payload.keys.each_pair do |attribute, options|
3435
prc = options[:proc]
35-
if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
36+
if (expect(json).to have_payload_key(attribute, options[:allow_nil], dasherized)) == true
3637
unless options[:allow_nil]
3738
output = instance_exec(record, &prc)
38-
expect(json[attribute.to_s]).to match_payload(attribute, output)
39+
attribute = attribute.to_s
40+
attribute = StringHelpers.dasherize(attribute) if dasherized
41+
expect(json[attribute]).to match_payload(attribute, output)
3942

4043
if options[:type]
4144
expect(json[attribute.to_s]).to match_type(attribute, options[:type])
@@ -47,8 +50,10 @@ def assert_payload(name, record, json, &blk)
4750
payload.no_keys.each do |no_key|
4851
expect(json).to_not have_payload_key(no_key, {})
4952
end
50-
51-
unexpected_keys = json.keys - payload.keys.keys.map(&:to_s)
53+
unexpected_keys = json.keys - payload.keys.keys.map do |key|
54+
key = key.to_s
55+
key = StringHelpers.dasherize(key) if dasherized
56+
end
5257
unexpected_keys.reject! { |k| %w(id jsonapi_type).include?(k) }
5358
unexpected_keys.each do |key|
5459
expect(key).to be_not_in_payload

lib/jsonapi_spec_helpers/matchers.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rspec/matchers'
2+
require 'jsonapi_spec_helpers/string_helpers'
23

34
RSpec::Matchers.define :match_payload do |attribute, expected|
45
match do |actual|
@@ -28,10 +29,12 @@
2829
end
2930
end
3031

31-
RSpec::Matchers.define :have_payload_key do |expected, allow_nil|
32+
RSpec::Matchers.define :have_payload_key do |expected, allow_nil, dasherized|
3233
match do |json|
33-
@has_key = json.has_key?(expected.to_s)
34-
@has_value = !json[expected.to_s].nil?
34+
expected = expected.to_s
35+
expected = JsonapiSpecHelpers::StringHelpers.dasherize(expected) if dasherized
36+
@has_key = json.has_key?(expected)
37+
@has_value = !json[expected].nil?
3538

3639
if allow_nil
3740
@has_key
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module JsonapiSpecHelpers
2+
class StringHelpers
3+
class << self
4+
def dasherize(str)
5+
return str unless str.include?('_')
6+
7+
str.gsub('_','-')
8+
end
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)