File: element_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (126 lines) | stat: -rw-r--r-- 4,026 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true

RSpec.describe QA::Page::Element do
  describe '#selector_css' do
    it 'transforms element name into QA-specific clickable css selector' do
      expect(described_class.new(:sign_in_button).selector_css)
        .to eq('[data-testid="sign_in_button"]')
    end
  end

  context 'when pattern is an expression' do
    subject { described_class.new(:something, /button 'Sign in'/) }

    it 'matches when there is a match' do
      expect(subject.matches?("button 'Sign in'")).to be true
    end

    it 'does not match if pattern is not present' do
      expect(subject.matches?("button 'Sign out'")).to be false
    end
  end

  context 'when pattern is a string' do
    subject { described_class.new(:something, 'button') }

    it 'matches when there is match' do
      expect(subject.matches?('some button in the view')).to be true
    end

    it 'does not match if pattern is not present' do
      expect(subject.matches?('text_field :name')).to be false
    end
  end

  context 'when pattern is not provided' do
    subject { described_class.new(:some_name) }

    it 'does not match if QA selector is not there' do
      expect(subject.matches?('some_name selector')).to be false
    end

    it 'matches when element name is specified' do
      expect(subject.matches?('data:{qa:{selector:"some_name"}}')).to be true
    end

    it 'matches when element name is specified (single quotes)' do
      expect(subject.matches?("data:{qa:{selector:'some_name'}}")).to be true
    end
  end

  describe 'attributes' do
    context 'element with no args' do
      subject { described_class.new(:something) }

      it 'has no attribute[pattern]' do
        expect(subject.attributes[:pattern]).to be(nil)
      end

      it 'is not required by default' do
        expect(subject.required?).to be false
      end
    end

    context 'element with a pattern' do
      subject { described_class.new(:something, /link_to 'something'/) }

      it 'has an attribute[pattern] of the pattern' do
        expect(subject.attributes[:pattern]).to eq(/link_to 'something'/)
      end

      it 'is not required by default' do
        expect(subject.required?).to be false
      end
    end

    context 'element with requirement; no pattern' do
      subject { described_class.new(:something, required: true) }

      it 'is required' do
        expect(subject.required?).to be true
      end
    end

    context 'element with requirement and pattern' do
      subject { described_class.new(:something, /link_to 'something_else_entirely'/, required: true) }

      it 'has an attribute[pattern] of the passed pattern' do
        expect(subject.attributes[:pattern]).to eq(/link_to 'something_else_entirely'/)
      end

      it 'is required' do
        expect(subject.required?).to be true
      end
    end
  end

  describe 'data-testid selectors' do
    subject { described_class.new(:my_element) }

    it 'does not translate to a deprecated qa selector' do
      expect(subject.selector_css).not_to include(%q([data-qa-selector="my_element"]))
    end

    it 'properly translates to a data-testid' do
      expect(subject.selector_css).to include(%q([data-testid="my_element"]))
    end

    context 'additional selectors' do
      let(:element) { described_class.new(:my_element, index: 3, another_match: 'something') }
      let(:required_element) { described_class.new(:my_element, required: true, index: 3) }

      it 'matches on additional data-qa properties translating snake_case to kebab-case' do
        expect(element.selector_css)
          .to include('[data-testid="my_element"][data-qa-index="3"][data-qa-another-match="something"]')
      end

      it 'doesnt conflict with element requirement' do
        expect(element).not_to be_required
        expect(element.selector_css).not_to include(%q(data-qa-required))

        expect(required_element).to be_required
        expect(required_element.selector_css).not_to include(%q(data-qa-required))
      end
    end
  end
end