orderly 0.1.0 → 0.1.1

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: cea3ece86863bb26014c5444e2ef64b1cbae500fe21e72cd9acade819b2787be
4
- data.tar.gz: 234a6c96a889e2b8ba46aa804601edc53f07cdbdc9eaf8ab8b82ca96b7e254ba
3
+ metadata.gz: 9b7a52ca99c21471d257d31243449fe036db51c784d0dfd22b01ff96397fe00d
4
+ data.tar.gz: 9abdc383f871e3c53d8af0c3949f9f5de2762f03b41c37697579d542c93805e3
5
5
  SHA512:
6
- metadata.gz: 7ebadf197892f14c1be4097868cbc029327f49dd92af25f302be83e6a18bb06fc8c12d019c5273940bfd37dd548ce048d1b7c5e3fd00d28345994e904a3881eb
7
- data.tar.gz: c77f6fe4aa0e51972dc1226519e3979bc8c26c527ff307ba1e7a8c0f1d3e4be5b2b8888e1926f302b5d5169deb6e4d1e092aca967090035bbdadd02a10f443cc
6
+ metadata.gz: 58409a784670ceed76d638d35465cc1f2c5ecf83e48955772347b889ea5a73034d348bda45fb1f12b75b8f16282039ff99286041f7b898c10f153d1ef4a0f73c
7
+ data.tar.gz: d13c0ed1d8fbef1701afaf853f58545db4ff3cc930109c4c11917d4132cc624862de3edcaccba62e20500a9a0a63adf7a54b052994d5ff9f3bdb10c53d152890
data/README.md CHANGED
@@ -20,19 +20,44 @@ Or install it yourself as:
20
20
 
21
21
  In an rspec request spec, do
22
22
 
23
- this.should appear_before(that)
24
- # or
25
- expect(this).to appear_before(that)
23
+ ```ruby
24
+ let(:this) { "<li>Andrea</li>" }
25
+ let(:that) { "<li>Luis</li>" }
26
26
 
27
- or, to assert that something does not appear before
27
+ expect(this).to appear_before(that)
28
28
 
29
- this.should_not appear_before(that)
30
- # or
31
- expect(this).to_not appear_before(that)
29
+ # or use capybara elements
30
+ this = find('.this')
31
+ that = find('.that')
32
32
 
33
- Error handling in place for cases where this or that does not appear on the page.
33
+ expect(this).to appear_before(that)
34
+ ```
35
+
36
+ To assert that something does not appear before
37
+
38
+ ```ruby
39
+ expect(this).to_not appear_before(that)
40
+ ```
41
+
42
+ By default, `appear_before` matches against HTML. If you want to only compare text, use the `only_text`-option:
43
+
44
+ ```ruby
45
+ # <dl>
46
+ # <dt>First name:</dt>
47
+ # <dd>Andrea</dt>
48
+ #
49
+ # <dt>Last name:</dt>
50
+ # <dd>Robbinovich</dt>
51
+ # </dl>
52
+
53
+ expect("First name: Andrea").to appear_before("Last name: Robbinovich", only_text: true)
54
+ ```
34
55
 
35
56
  ## Changelog
57
+ ### 0.1.1 (2020-02-20)
58
+ - Add support for `only_text`-option
59
+ - Add support for using capybara nodes
60
+
36
61
  ### 0.1.0 (2019-08-08)
37
62
  - Add support for capybara `within`-blocks
38
63
 
@@ -2,12 +2,13 @@ require "orderly/version"
2
2
  require "rspec/expectations"
3
3
 
4
4
  module Orderly
5
- RSpec::Matchers.define :appear_before do |later_content|
5
+ RSpec::Matchers.define :appear_before do |later_content, only_text: false|
6
6
  match do |earlier_content|
7
7
  begin
8
8
  node = page.respond_to?(:current_scope) ? page.current_scope : page.send(:current_node)
9
- html = html_for_node(node)
10
- html.index(earlier_content) < html.index(later_content)
9
+ data = only_text ? text_for_node(node) : html_for_node(node)
10
+
11
+ data.index(extract_content(earlier_content)) < data.index(extract_content(later_content))
11
12
  rescue ArgumentError
12
13
  raise "Could not locate later content on page: #{later_content}"
13
14
  rescue NoMethodError
@@ -17,12 +18,26 @@ module Orderly
17
18
 
18
19
  def html_for_node(node)
19
20
  if node.is_a?(Capybara::Node::Document)
20
- page.body
21
- elsif node.native.respond_to?(:inner_html)
22
- node.native.inner_html
21
+ html_for_node(node.find(:xpath, '//body'))
22
+ elsif node.native.respond_to?(:to_html)
23
+ node.native.to_html
23
24
  else
24
- page.driver.evaluate_script("arguments[0].innerHTML", node.native)
25
+ node.evaluate_script("this.outerHTML")
25
26
  end
26
27
  end
28
+
29
+ def extract_content(node)
30
+ if node.is_a?(String)
31
+ node
32
+ else
33
+ html_for_node(node)
34
+ end
35
+ end
36
+
37
+ def text_for_node(node)
38
+ # NOTE: we need to normalize spaces due to differences between rack-test
39
+ # and headless chrome.
40
+ node.text.gsub(/[[:space:]]+/, ' ').strip
41
+ end
27
42
  end
28
43
  end
@@ -1,3 +1,3 @@
1
1
  module Orderly
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
@@ -50,5 +50,49 @@ describe Orderly do
50
50
  end
51
51
  end
52
52
  end
53
+
54
+ context "when only_text option is passed" do
55
+ context "when that is equal to option from HTML" do
56
+ let(:this) { "ability" }
57
+ let(:that) { "option" }
58
+
59
+ it "asserts this is before that" do
60
+ page.visit "/options"
61
+
62
+ expect(this).to appear_before(that, only_text: true)
63
+ end
64
+ end
65
+
66
+ context "when text is split in HTML" do
67
+ let(:this) { "First name: Andrea" }
68
+ let(:that) { "Last name: Robbinovich" }
69
+
70
+ it "asserts this is before that" do
71
+ page.visit "/description-list"
72
+
73
+ expect(this).to appear_before(that, only_text: true)
74
+ end
75
+ end
76
+ end
77
+
78
+ context "when a capybara element is passed" do
79
+ it "asserts that the first capybara element appears before the second" do
80
+ page.visit "/elements-with-ids"
81
+ div1 = page.find("#div-1")
82
+ div2 = page.find("#div-2")
83
+
84
+ expect(div1).to appear_before(div2)
85
+ end
86
+ end
87
+
88
+ context "when the page returns tags without closing forward slashes" do
89
+ it "normalizes those tags" do
90
+ page.visit "/response-with-unclosed-tag"
91
+ img1 = page.find("#img-1")
92
+ img2 = page.find("#img-2")
93
+
94
+ expect(img1).to appear_before(img2)
95
+ end
96
+ end
53
97
  end
54
98
  end
@@ -25,12 +25,48 @@ class TestApp < Sinatra::Base
25
25
  end
26
26
 
27
27
  get '/thatthisthatthis' do
28
- "#{that}
28
+ <<-HTML
29
+ #{that}
29
30
  <div class='outer-div'>
30
31
  #{this}
31
32
  <div class='inner-div'>
32
33
  #{that + this}
33
34
  </div>
34
- </div>"
35
+ </div>
36
+ HTML
37
+ end
38
+
39
+ get '/options' do
40
+ <<-HTML
41
+ <select>
42
+ <option>ability</option>
43
+ <option>option</option>
44
+ </select>
45
+ HTML
46
+ end
47
+
48
+ get '/description-list' do
49
+ <<-HTML
50
+ <dl>
51
+ <dt>First name:</dt>
52
+ <dd>Andrea</dt>
53
+ <dt>Last name:</dt>
54
+ <dd>Robbinovich</dt>
55
+ </dl>
56
+ HTML
57
+ end
58
+
59
+ get '/elements-with-ids' do
60
+ <<-HTML
61
+ <div id="div-1">Foo</div>
62
+ <div id="div-2">Bar</div>
63
+ HTML
64
+ end
65
+
66
+ get '/response-with-unclosed-tag' do
67
+ <<-HTML
68
+ <img id="img-1" src="https://i.picsum.photos/id/1082/200/300.jpg">
69
+ <img id="img-2" src="https://i.picsum.photos/id/1082/200/300.jpg" />
70
+ HTML
35
71
  end
36
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orderly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Gesimondo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2020-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  requirements: []
155
- rubygems_version: 3.0.4
155
+ rubygems_version: 3.1.2
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: Assert this is before that