File: css_builder_spec.rb

package info (click to toggle)
ruby-capybara 3.36.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,380 kB
  • sloc: ruby: 23,399; javascript: 748; makefile: 11
file content (101 lines) | stat: -rw-r--r-- 3,718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

require 'spec_helper'

# rubocop:disable RSpec/InstanceVariable
RSpec.describe Capybara::Selector::CSSBuilder do
  let :builder do
    ::Capybara::Selector::CSSBuilder.new(@css)
  end

  context 'add_attribute_conditions' do
    it 'adds a single string condition to a single selector' do
      @css = 'div'
      selector = builder.add_attribute_conditions(random: 'abc')
      expect(selector).to eq %(div[random='abc'])
    end

    it 'adds multiple string conditions to a single selector' do
      @css = 'div'
      selector = builder.add_attribute_conditions(random: 'abc', other: 'def')
      expect(selector).to eq %(div[random='abc'][other='def'])
    end

    it 'adds a single string condition to a multiple selector' do
      @css = 'div, ul'
      selector = builder.add_attribute_conditions(random: 'abc')
      expect(selector).to eq %(div[random='abc'], ul[random='abc'])
    end

    it 'adds multiple string conditions to a multiple selector' do
      @css = 'div, ul'
      selector = builder.add_attribute_conditions(random: 'abc', other: 'def')
      expect(selector).to eq %(div[random='abc'][other='def'], ul[random='abc'][other='def'])
    end

    it 'adds simple regexp conditions to a single selector' do
      @css = 'div'
      selector = builder.add_attribute_conditions(random: /abc/, other: /def/)
      expect(selector).to eq %(div[random*='abc'][other*='def'])
    end

    it 'adds wildcard regexp conditions to a single selector' do
      @css = 'div'
      selector = builder.add_attribute_conditions(random: /abc.*def/, other: /def.*ghi/)
      expect(selector).to eq %(div[random*='abc'][random*='def'][other*='def'][other*='ghi'])
    end

    it 'adds alternated regexp conditions to a single selector' do
      @css = 'div'
      selector = builder.add_attribute_conditions(random: /abc|def/, other: /def|ghi/)
      expect(selector).to eq %(div[random*='abc'][other*='def'], div[random*='abc'][other*='ghi'], div[random*='def'][other*='def'], div[random*='def'][other*='ghi'])
    end

    it 'adds alternated regexp conditions to a multiple selector' do
      @css = 'div,ul'
      selector = builder.add_attribute_conditions(other: /def.*ghi|jkl/)
      expect(selector).to eq %(div[other*='def'][other*='ghi'], div[other*='jkl'], ul[other*='def'][other*='ghi'], ul[other*='jkl'])
    end

    it "returns original selector when regexp can't be substringed" do
      @css = 'div'
      selector = builder.add_attribute_conditions(other: /.+/)
      expect(selector).to eq 'div'
    end

    context ':class' do
      it 'handles string with CSS .' do
        @css = 'a'
        selector = builder.add_attribute_conditions(class: 'my_class')
        expect(selector).to eq 'a.my_class'
      end

      it 'handles negated string with CSS .' do
        @css = 'a'
        selector = builder.add_attribute_conditions(class: '!my_class')
        expect(selector).to eq 'a:not(.my_class)'
      end

      it 'handles array of string with CSS .' do
        @css = 'a'
        selector = builder.add_attribute_conditions(class: %w[my_class my_other_class])
        expect(selector).to eq 'a.my_class.my_other_class'
      end

      it 'handles array of string with CSS . when negated included' do
        @css = 'a'
        selector = builder.add_attribute_conditions(class: %w[my_class !my_other_class])
        expect(selector).to eq 'a.my_class:not(.my_other_class)'
      end
    end

    context ':id' do
      it 'handles string with CSS #' do
        @css = 'ul'
        selector = builder.add_attribute_conditions(id: 'my_id')
        expect(selector).to eq 'ul#my_id'
      end
    end
  end
end
# rubocop:enable RSpec/InstanceVariable