rspec-request_snapshot 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63e2665c7ef3ad56f77f9cdf3d037e1c889a15218ffa8350929ba3fe77c63335
4
- data.tar.gz: ab9c6146284efd2db0b2f9821160ccebc83a75ad40b1c1826e1c1c3b0c103573
3
+ metadata.gz: ea875af5bb2032c8034482032dc737d7ec60b55e2f977551adddee0d0bb5f0cf
4
+ data.tar.gz: 946666e33db659d180bd8762d8cbfc1400f1bcd28c25725a3cc13d639486385e
5
5
  SHA512:
6
- metadata.gz: e9a7b6a9592f44ca0cab09167e15aaa90436935aab9f16989fe68488edebb0f22fd945a83c47bc8ccdba1b8c8c6733f1ae40fa337cd82f9572d308e0b7fd8939
7
- data.tar.gz: 5af511e46ac7d7968970b4738e39a5d1657652492b9239a85136db6c5f705862bec92ed7a1b5211eb4ae3696f6c00986e2fa74248190acba7488ca0569f50170
6
+ metadata.gz: b78cc8aa841430a3d9134b263bd092e31ddadb9b91fee63683dadf9ed856730ecae3d4844d2bcec0d230c2e2e3559f9617d1940c8435925fc0221bcf7614f05b
7
+ data.tar.gz: 1b327bc6fec76fda28c437a6093e2d43fb025af9361dde518ee8e11f1a7967da1cd88b889fa71a4d76e0df4e75f93642d7b327e62186ed0c1402a35dde7751f9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-request_snapshot (0.2.0)
4
+ rspec-request_snapshot (0.3.0)
5
5
  rspec (~> 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -56,14 +56,34 @@ expect(response.body).to match_snapshot("api/resources_index")
56
56
 
57
57
  # Using plain text instead of parsing JSON
58
58
  expect(response.body).to match_snapshot("api/resources_index", format: :text)
59
+ ```
60
+
61
+ #### Matcher options
62
+
63
+ ##### dynamic_attributes
59
64
 
65
+ Using `dynamic_attributes` inline allows to ignore attributes for a specific snapshot.
66
+ This is useful to ignore changing attributes, like `id` or `created_at`.
67
+ Notice that **all** nodes matching those (nested or not) will be ignored. Usage:
68
+
69
+ ```ruby
60
70
  # Defining specific test dynamic attributes
61
71
  expect(response.body).to match_snapshot("api/resources_index", dynamic_attributes: %w(confirmed_at relation_id))
72
+ ```
73
+
74
+ ##### ignore_order
75
+
76
+ It is possible to use the `ignore_order` inline option to mark which array nodes are unsorted and that elements position
77
+ should not be taken into consideration.
62
78
 
79
+ ```ruby
63
80
  # Ignoring order for certain arrays (this will ignore the ordering for the countries array inside the json response)
64
81
  expect(response.body).to match_snapshot("api/resources_index", ignore_order: %w(countries))
65
82
  ```
66
83
 
84
+ **Note:** If you are using `ignore_order` for arrays of objects/hashes (ie: `[{name: "name", value: "value"}, ...]`),
85
+ it won't perform well depending on the array and hash size (number of keys)
86
+
67
87
  ## Development
68
88
 
69
89
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -8,5 +8,9 @@ module Rspec::RequestSnapshot::Handlers
8
8
  @dynamic_attributes ||= RSpec.configuration.request_snapshots_dynamic_attributes |
9
9
  Array(@options[:dynamic_attributes])
10
10
  end
11
+
12
+ def ignore_order
13
+ @ignore_order ||= @options[:ignore_order] || []
14
+ end
11
15
  end
12
16
  end
@@ -1,22 +1,10 @@
1
1
  class Rspec::RequestSnapshot::Handlers::JSON < Rspec::RequestSnapshot::Handlers::Base
2
2
  def compare(actual, expected)
3
- equal = true
4
-
5
- actual.each do |key, value|
6
- if actual[key].is_a?(Hash)
7
- equal &= compare_hash(key, actual, expected)
8
- elsif actual[key].is_a?(Array)
9
- equal &= compare_array(key, actual, expected)
10
- else
11
- equal &= compare_value(key, actual, expected)
12
- end
13
- end
14
-
15
- equal
3
+ actual == expected
16
4
  end
17
5
 
18
6
  def comparable(str)
19
- JSON.parse(str)
7
+ deep_transform_values(JSON.parse(str).dup)
20
8
  end
21
9
 
22
10
  def writable(str)
@@ -25,21 +13,25 @@ class Rspec::RequestSnapshot::Handlers::JSON < Rspec::RequestSnapshot::Handlers:
25
13
 
26
14
  private
27
15
 
28
- def compare_hash(key, actual, expected)
29
- actual[key].is_a?(Hash) && expected[key].is_a?(Hash) && compare(actual[key], expected[key])
30
- end
16
+ def deep_transform_values(hash)
17
+ hash.each do |key, value|
18
+ if dynamic_attributes.include?(key)
19
+ hash[key] = "REPLACED"
20
+ end
31
21
 
32
- def compare_array(key, actual, expected)
33
- if actual[key].is_a?(Array) && expected[key].is_a?(Array)
34
- if @options[:ignore_order] && @options[:ignore_order].include?(key)
35
- actual[key].sort == expected[key].sort
36
- else
37
- actual[key] == expected[key]
22
+ if hash[key].is_a?(Hash)
23
+ deep_transform_values(hash[key])
38
24
  end
39
- end
40
- end
41
25
 
42
- def compare_value(key, actual ,expected)
43
- dynamic_attributes.include?(key) ? true : actual[key] == expected[key]
26
+ if hash[key].is_a?(Array)
27
+ hash[key].each do |value|
28
+ deep_transform_values(value) if value.is_a?(Hash)
29
+ end
30
+
31
+ if ignore_order.include?(key)
32
+ hash[key].first.is_a?(Hash) ? hash[key].sort_by! { |e| e.keys.map { |k| e[k] } } : hash[key].sort!
33
+ end
34
+ end
35
+ end
44
36
  end
45
37
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module RequestSnapshot
3
- VERSION = "0.2.0".freeze
3
+ VERSION = "0.3.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-request_snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Campos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-07 00:00:00.000000000 Z
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec