Skip to content

Commit fb1a81b

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

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

lib/jsonapi_spec_helpers.rb

+9-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,9 @@ 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+
dasherized ? StringHelpers.dasherize(key) : key.to_s
55+
end
5256
unexpected_keys.reject! { |k| %w(id jsonapi_type).include?(k) }
5357
unexpected_keys.each do |key|
5458
expect(key).to be_not_in_payload

lib/jsonapi_spec_helpers/matchers.rb

+5-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,11 @@
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 = dasherized ? JsonapiSpecHelpers::StringHelpers.dasherize(expected) : expected.to_s
35+
@has_key = json.has_key?(expected)
36+
@has_value = !json[expected].nil?
3537

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

spec/assert_payload_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,40 @@
6161
end
6262
end
6363

64+
context 'when dasherized: true' do
65+
before do
66+
JsonapiSpecHelpers::Payload.register(:post_with_dasherized_attribute) do
67+
key(:dasherized_attribute)
68+
end
69+
end
70+
71+
let(:dasherized_value) { 'some value' }
72+
73+
let(:post_record) do
74+
attrs = {
75+
id: 1,
76+
dasherized_attribute: dasherized_value
77+
}
78+
double(attrs).as_null_object
79+
end
80+
81+
let(:json) do
82+
{
83+
'data' => {
84+
'type' => 'posts',
85+
'id' => '1',
86+
'attributes' => {
87+
'dasherized-attribute' => dasherized_value
88+
}
89+
}
90+
}
91+
end
92+
93+
it 'passes assertion' do
94+
assert_payload(:post_with_dasherized_attribute, post_record, json_item, dasherized: true)
95+
end
96+
end
97+
6498
context 'when json value matches payload value, but wrong type' do
6599
before do
66100
json['data']['attributes']['views'] = '100'

spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
require 'action_dispatch'
33
require 'jsonapi_spec_helpers'
44
require 'pry'
5+
RSpec.configure do |c|
6+
c.filter_run focus: true
7+
c.run_all_when_everything_filtered = true
8+
end

0 commit comments

Comments
 (0)