From 245a5ea218284936133e1714beeeff994ec903aa Mon Sep 17 00:00:00 2001 From: ollizeyen Date: Mon, 6 Sep 2021 12:03:56 +0200 Subject: [PATCH] Support dasherized keys --- lib/jsonapi_spec_helpers.rb | 14 +++++---- lib/jsonapi_spec_helpers/matchers.rb | 8 +++-- lib/jsonapi_spec_helpers/string_helpers.rb | 11 +++++++ spec/assert_payload_spec.rb | 34 ++++++++++++++++++++++ spec/spec_helper.rb | 4 +++ 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 lib/jsonapi_spec_helpers/string_helpers.rb diff --git a/lib/jsonapi_spec_helpers.rb b/lib/jsonapi_spec_helpers.rb index 25982c4..b99bc17 100644 --- a/lib/jsonapi_spec_helpers.rb +++ b/lib/jsonapi_spec_helpers.rb @@ -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' @@ -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 @@ -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]) @@ -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 diff --git a/lib/jsonapi_spec_helpers/matchers.rb b/lib/jsonapi_spec_helpers/matchers.rb index 79538de..1dda7a3 100644 --- a/lib/jsonapi_spec_helpers/matchers.rb +++ b/lib/jsonapi_spec_helpers/matchers.rb @@ -1,4 +1,5 @@ require 'rspec/matchers' +require 'jsonapi_spec_helpers/string_helpers' RSpec::Matchers.define :match_payload do |attribute, expected| match do |actual| @@ -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 diff --git a/lib/jsonapi_spec_helpers/string_helpers.rb b/lib/jsonapi_spec_helpers/string_helpers.rb new file mode 100644 index 0000000..2533a5d --- /dev/null +++ b/lib/jsonapi_spec_helpers/string_helpers.rb @@ -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 diff --git a/spec/assert_payload_spec.rb b/spec/assert_payload_spec.rb index 8030ccc..21ae86d 100644 --- a/spec/assert_payload_spec.rb +++ b/spec/assert_payload_spec.rb @@ -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' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3cf8a49..52a654e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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