1# frozen_string_literal: true
2
3module QA
4  module Page
5    module Component
6      module CiBadgeLink
7        extend QA::Page::PageConcern
8
9        COMPLETED_STATUSES = %w[passed failed canceled blocked skipped manual].freeze # excludes created, pending, running
10        INCOMPLETE_STATUSES = %w[pending created running].freeze
11
12        # e.g. def passed?(timeout: nil); status_badge == 'passed'; end
13        COMPLETED_STATUSES.map do |status|
14          define_method "#{status}?" do |timeout: nil|
15            timeout ? completed?(timeout: timeout) : completed?
16            status_badge == status
17          end
18
19          # has_passed? => passed?
20          # has_failed? => failed?
21          alias_method :"has_#{status}?", :"#{status}?"
22        end
23
24        # e.g. def pending?; status_badge == 'pending'; end
25        INCOMPLETE_STATUSES.map do |status|
26          define_method "#{status}?" do
27            status_badge == status
28          end
29        end
30
31        def self.included(base)
32          super
33
34          base.view 'app/assets/javascripts/vue_shared/components/ci_badge_link.vue' do
35            element :status_badge
36          end
37        end
38
39        def status_badge
40          find_element(:status_badge).text
41        end
42
43        private
44
45        def completed?(timeout: 60)
46          wait_until(reload: false, sleep_interval: 3.0, max_duration: timeout) do
47            COMPLETED_STATUSES.include?(status_badge)
48          end
49        end
50      end
51    end
52  end
53end
54