diff --git a/Gemfile.lock b/Gemfile.lock index 0e6a6ef..067e3a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rspec-json_api (1.2.1) + rspec-json_api (1.2.2) activesupport (>= 6.1.4.1) rails (>= 6.1.4.1) rspec-rails (>= 5.0.2) diff --git a/lib/extentions/array.rb b/lib/extentions/array.rb new file mode 100644 index 0000000..c91a77e --- /dev/null +++ b/lib/extentions/array.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class Array + def deep_sort + map { |element| element.is_a?(Array) ? element.deep_sort : element } + .sort_by { |el| el.is_a?(Array) ? el.first.to_s : el.to_s } + end +end \ No newline at end of file diff --git a/lib/rspec/json_api.rb b/lib/rspec/json_api.rb index 564522a..ff9d3cc 100644 --- a/lib/rspec/json_api.rb +++ b/lib/rspec/json_api.rb @@ -11,6 +11,7 @@ # Load extentions require "extentions/hash" +require "extentions/array" # Load matchers require "rspec/json_api/matchers" diff --git a/lib/rspec/json_api/matchers/match_json_schema.rb b/lib/rspec/json_api/matchers/match_json_schema.rb index 30694d7..b25af87 100644 --- a/lib/rspec/json_api/matchers/match_json_schema.rb +++ b/lib/rspec/json_api/matchers/match_json_schema.rb @@ -21,7 +21,7 @@ def matches?(actual) RSpec::JsonApi::CompareArray.compare(actual, expected) else # Compare actual and expected schema - return false unless actual.deep_keys == expected.deep_keys + return false unless actual.deep_keys.deep_sort == expected.deep_keys.deep_sort RSpec::JsonApi::CompareHash.compare(actual, expected) end diff --git a/lib/rspec/json_api/version.rb b/lib/rspec/json_api/version.rb index 6dd3d97..c626d88 100644 --- a/lib/rspec/json_api/version.rb +++ b/lib/rspec/json_api/version.rb @@ -2,6 +2,6 @@ module RSpec module JsonApi - VERSION = "1.2.1" + VERSION = "1.2.2" end end diff --git a/spec/rspec/json_api/matchers/match_json_schema_spec.rb b/spec/rspec/json_api/matchers/match_json_schema_spec.rb index bd78690..ceb572e 100644 --- a/spec/rspec/json_api/matchers/match_json_schema_spec.rb +++ b/spec/rspec/json_api/matchers/match_json_schema_spec.rb @@ -217,6 +217,52 @@ end end + context 'when keys are in arbitrary order' do + let(:expected) do + { + id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f", + name: "Caroline Mayer", + age: 25, + children: [ + { + id: "8eccff73-f134-42f2-aed4-751d1f4ebd4a", + name: "Webster Medina", + age: 2 + }, + { + id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b", + name: "Roy Mcdaniel", + age: 3 + } + ] + } + end + + context "when correct match" do + let(:actual) do + { + name: "Caroline Mayer", + age: 25, + id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f", + children: [ + { + name: "Webster Medina", + age: 2, + id: "8eccff73-f134-42f2-aed4-751d1f4ebd4a", + }, + { + id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b", + name: "Roy Mcdaniel", + age: 3 + } + ] + }.to_json + end + + include_examples "correct-match" + end + end + context "when data typed given" do let(:expected) do {