rspec-html 0.2.8 → 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a271ab933a02e1c5456ea472551e09d194132d79d9f47924cc8aa9cc4ae9c832
4
- data.tar.gz: 230f1ae1e54404f1766b909074bea3d0b83d748f8a57a7df9c497a60137c4fc4
3
+ metadata.gz: 8052637b48c07f5312c3c0f6c4f3c42b790691758ab66b92240509bc19512b78
4
+ data.tar.gz: 28332c0112b53c97613bb24f387d6b9cef099527d363416856a44eab9e9f7260
5
5
  SHA512:
6
- metadata.gz: ad8a71ee657b000eb1b334d365da8f723ec0466c69aa7580b8104c87aae23066ca703cdbbea3717ac3b422fb57b59c8784ecef4a8eeb3366a0f719bd5f2808eb
7
- data.tar.gz: a1eeede78f3c7bddc433c2162c4355779c55bd9c6c0ce984f42ad347eaef3e8c5cc22e7b78dbe29450a353c8acd679a24acaccd4a6add6981223ba90eaf45901
6
+ metadata.gz: aa6583f785fd6e9da9e0a6c6d2a06e4f262e749e75d99e7a808fb937c3d08c882366424ffffcaf70e7d19de44027064435148ac9a8c9b3d4607ca67f218e5377
7
+ data.tar.gz: 419571c56771c6c5b46a7197ce3f8f3cb866c00e0db54201c1106e9f2105099c78461feb719b81ddf5d924b6098859f57e5c4107140583805ba84b3c6555a2d5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-html (0.2.8)
4
+ rspec-html (0.2.11)
5
5
  nokogiri (~> 1.10)
6
6
  rspec (~> 3.0)
7
7
 
@@ -14,7 +14,7 @@ GEM
14
14
  diff-lcs (1.5.0)
15
15
  i18n (1.10.0)
16
16
  concurrent-ruby (~> 1.0)
17
- nokogiri (1.13.5-x86_64-linux)
17
+ nokogiri (1.13.6-x86_64-linux)
18
18
  racc (~> 1.4)
19
19
  paint (2.2.1)
20
20
  parallel (1.22.1)
data/README.md CHANGED
@@ -7,7 +7,7 @@ _RSpec::HTML_ provides a simple object interface to HTML responses from [_RSpec
7
7
  Add the gem to your `Gemfile`:
8
8
 
9
9
  ```ruby
10
- gem 'rspec-html', '~> 0.2.8'
10
+ gem 'rspec-html', '~> 0.2.11'
11
11
  ```
12
12
 
13
13
  And rebuild your bundle:
@@ -41,6 +41,11 @@ it 'says hello' do
41
41
  end
42
42
  ```
43
43
 
44
+ This method can also be used for _ActionMailer_ emails:
45
+ ```ruby
46
+ before { parse_html(ActionMailer::Base.deliveries.last.body.decoded) }
47
+ ```
48
+
44
49
  To navigate the _DOM_ by a sequence of tag names use chained method calls on the `document` object:
45
50
 
46
51
  #### Tag Traversal
@@ -63,6 +68,17 @@ expect(document.body.div(id: 'my-div').span(class: 'my-class my-other-class')).t
63
68
 
64
69
  Classes can be provided in any order, i.e. `'my-class my-other-class'` is equivalent to `'my-other-class my-class'`.
65
70
 
71
+ #### Simple CSS Matching
72
+ To use a simple CSS selector when no other attributes are needed, pass a string to the tag method:
73
+ ```ruby
74
+ expect(document.body.div('#my-id.my-class1.my-class2')).to contain_text 'some text'
75
+ ```
76
+
77
+ This is effectively shorthand for:
78
+ ```ruby
79
+ expect(document.body.div(id: 'my-id', class: 'my-class1 my-class2')).to contain_text 'some text'
80
+ ```
81
+
66
82
  #### Text Matching
67
83
  To select an element that includes a given text string (i.e. excluding mark-up) use the `text` option:
68
84
  ```ruby
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module HTML
5
- VERSION = '0.2.8'
5
+ VERSION = '0.2.11'
6
6
  end
7
7
  end
@@ -8,7 +8,10 @@ module RSpecHTML
8
8
  extend Forwardable
9
9
 
10
10
  def_delegators :@search,
11
- :has_css?, :has_xpath?, :include?, :text, :truncated_text, :size, :length, :css, :xpath, :[]
11
+ :has_css?, :has_xpath?, :include?,
12
+ :siblings, :text, :truncated_text,
13
+ :size, :length, :[],
14
+ :css, :xpath
12
15
 
13
16
  def initialize(element, name, options: {}, siblings: [])
14
17
  @name = name
@@ -10,10 +10,28 @@ module RSpecHTML
10
10
 
11
11
  def match(actual)
12
12
  @rspec_actual = actual&.text
13
- return (actual&.text || '').include?(@expected.to_s) unless @expected.is_a?(Regexp)
13
+ return regexp_match?(actual) || regexp_siblings_match?(actual) if @expected.is_a?(Regexp)
14
14
 
15
+ string_match?(actual) || string_siblings_match?(actual)
16
+ end
17
+
18
+ private
19
+
20
+ def regexp_match?(actual)
15
21
  @expected.match(actual&.text || '')
16
22
  end
23
+
24
+ def regexp_siblings_match?(actual)
25
+ actual.siblings.any? { |sibling| @expected.match(sibling&.text || '') }
26
+ end
27
+
28
+ def string_match?(actual)
29
+ (actual&.text || '').include?(@expected.to_s)
30
+ end
31
+
32
+ def string_siblings_match?(actual)
33
+ actual.siblings.any? { |sibling| (sibling&.text || '').include?(@expected.to_s) }
34
+ end
17
35
  end
18
36
  end
19
37
  end
@@ -12,6 +12,7 @@ module RSpecHTML
12
12
  name = @tag.to_s.downcase
13
13
  return '#document' if name == 'document'
14
14
  return name if name == 'document'
15
+ return "<#{name}#{@options} />" if @options.is_a?(String)
15
16
  return "<#{name}#{formatted_attributes} />" unless @options&.key?(:text)
16
17
 
17
18
  "<#{name}#{formatted_attributes}>#{@options[:text]}</#{name}>"
@@ -2,7 +2,10 @@
2
2
 
3
3
  module RSpecHTML
4
4
  # Provides element/attribute/text searching for HTML entities
5
+ # rubocop:disable Metrics/ClassLength
5
6
  class Search
7
+ attr_reader :siblings
8
+
6
9
  def initialize(element, siblings)
7
10
  @element = element
8
11
  @siblings = siblings
@@ -56,7 +59,7 @@ module RSpecHTML
56
59
  def size
57
60
  return @element.size if @element.respond_to?(:size)
58
61
 
59
- @siblings.size
62
+ @siblings&.size || 0
60
63
  end
61
64
  alias length size
62
65
 
@@ -95,17 +98,23 @@ module RSpecHTML
95
98
  end
96
99
 
97
100
  def where(tag, query, all: false)
98
- matched = if query[:class]
99
- where_class(tag, query[:class]) & where_xpath(tag, query.merge(class: nil))
100
- else
101
- where_xpath(tag, query)
102
- end
101
+ matched = matched_from_query(tag, query)
103
102
  return nil unless matched || all
104
103
  return matched&.first unless all
105
104
 
106
105
  matched
107
106
  end
108
107
 
108
+ def matched_from_query(tag, query)
109
+ if query.is_a?(String)
110
+ @element&.css("#{tag}#{query}")
111
+ elsif query[:class]
112
+ where_class(tag, query[:class]) & where_xpath(tag, query.merge(class: nil))
113
+ else
114
+ where_xpath(tag, query)
115
+ end
116
+ end
117
+
109
118
  def where_xpath(tag, query)
110
119
  conditions = "[#{where_conditions(query)}]" unless query.compact.empty?
111
120
  @element&.xpath("//#{tag}#{conditions}")
@@ -132,4 +141,5 @@ module RSpecHTML
132
141
  @element&.css(tag.to_s)
133
142
  end
134
143
  end
144
+ # rubocop:enable Metrics/ClassLength
135
145
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Farrell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-08 00:00:00.000000000 Z
11
+ date: 2022-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri