summaryrefslogtreecommitdiff
path: root/qa/spec/page/base_spec.rb
blob: 5234587614966794ef2740c7918f1826f87ae2c2 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true

RSpec.describe QA::Page::Base do
  describe 'page helpers' do
    it 'exposes helpful page helpers' do
      expect(subject).to respond_to :refresh, :wait_until, :scroll_to
    end
  end

  describe '.view', 'DSL for defining view partials' do
    subject do
      Class.new(described_class) do
        view 'path/to/some/view.html.haml' do
          element :something, 'string pattern' # rubocop:disable QA/ElementWithPattern
          element :something_else, /regexp pattern/ # rubocop:disable QA/ElementWithPattern
        end

        view 'path/to/some/_partial.html.haml' do
          element :another_element, 'string pattern' # rubocop:disable QA/ElementWithPattern
        end
      end
    end

    it 'makes it possible to define page views' do
      expect(subject.views.size).to eq 2
      expect(subject.views).to all(be_an_instance_of(QA::Page::View))
    end

    it 'populates views objects with data about elements' do
      expect(subject.elements.size).to eq 3
      expect(subject.elements).to all(be_an_instance_of(QA::Page::Element))
      expect(subject.elements.map(&:name))
        .to eq [:something, :something_else, :another_element]
    end
  end

  describe '.errors' do
    let(:view) { double('view') }

    context 'when page has views and elements defined' do
      before do
        allow(described_class).to receive(:views)
          .and_return([view])

        allow(view).to receive(:errors).and_return(['some error'])
      end

      it 'iterates views composite and returns errors' do
        expect(described_class.errors).to eq ['some error']
      end
    end

    context 'when page has no views and elements defined' do
      before do
        allow(described_class).to receive(:views).and_return([])
      end

      it 'appends an error about missing views / elements block' do
        expect(described_class.errors)
          .to include 'Page class does not have views / elements defined!'
      end
    end
  end

  describe '#wait_until' do
    subject { Class.new(described_class).new }

    context 'when the condition is true' do
      it 'does not refresh' do
        expect(subject).not_to receive(:refresh)

        subject.wait_until(max_duration: 0.01, raise_on_failure: false) { true }
      end

      it 'returns true' do
        expect(subject.wait_until(max_duration: 0.1, raise_on_failure: false) { true }).to be_truthy
      end
    end

    context 'when the condition is false' do
      it 'refreshes' do
        expect(subject).to receive(:refresh).at_least(:once)

        subject.wait_until(max_duration: 0.01, raise_on_failure: false) { false }
      end

      it 'returns false' do
        allow(subject).to receive(:refresh)

        expect(subject.wait_until(max_duration: 0.01, raise_on_failure: false) { false }).to be_falsey
      end
    end
  end

  describe '#all_elements' do
    before do
      allow(subject).to receive(:all)
    end

    it 'raises an error if count or minimum are not specified' do
      expect { subject.all_elements(:foo) }.to raise_error ArgumentError
    end

    it 'does not raise an error if :minimum, :maximum, :count, or :between is specified' do
      [:minimum, :maximum, :count, :between].each do |param|
        expect { subject.all_elements(:foo, param => 1) }.not_to raise_error
      end
    end
  end

  context 'elements' do
    subject do
      Class.new(described_class) do
        view 'path/to/some/view.html.haml' do
          element :something, required: true
          element :something_else
        end
      end
    end

    describe '#elements' do
      it 'returns all elements' do
        expect(subject.elements.size).to eq(2)
      end
    end

    describe '#required_elements' do
      it 'returns only required elements' do
        expect(subject.required_elements.size).to eq(1)
      end
    end

    describe '#visible?', 'Page is currently visible' do
      let(:page) { subject.new }

      context 'with elements' do
        context 'on the page' do
          before do
            # required elements not there, meaning not on page
            allow(page).to receive(:has_no_element?).and_return(false)
          end

          it 'is visible' do
            expect(page).to be_visible
          end
        end

        context 'not on the page' do
          before do
            # required elements are not on the page
            allow(page).to receive(:has_no_element?).and_return(true)
          end

          it 'is not visible' do
            expect(page).not_to be_visible
          end
        end

        it 'does not raise error if page has elements' do
          expect { page.visible? }.not_to raise_error
        end
      end

      context 'no elements' do
        subject do
          Class.new(described_class) do
            view 'path/to/some/view.html.haml' do
              element :something
              element :something_else
            end
          end
        end

        let(:page) { subject.new }

        it 'raises error if page has no required elements' do
          expect { page.visible? }.to raise_error(described_class::NoRequiredElementsError)
        end
      end
    end
  end
end