Welcome to mirror list, hosted at ThFree Co, Russian Federation.

accessibility_reports_spec.rb « reports « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c35b2a34cf0381d77b6024b58419582780c71a9 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Reports::AccessibilityReports do
  let(:accessibility_report) { described_class.new }
  let(:url) { 'https://gitlab.com' }
  let(:data) do
    [
      {
        "code": "WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent",
        "type": "error",
        "typeCode": 1,
        "message": "Anchor element found with a valid href attribute, but no link content has been supplied.",
        "context": %{<a href="/customers/worldline"><svg viewBox="0 0 509 89" xmln...</a>},
        "selector": "html > body > div:nth-child(9) > div:nth-child(2) > a:nth-child(17)",
        "runner": "htmlcs",
        "runnerExtras": {}
      },
      {
        "code": "WCAG2AA.Principle4.Guideline4_1.4_1_2.H91.A.NoContent",
        "type": "error",
        "typeCode": 1,
        "message": "Anchor element found with a valid href attribute, but no link content has been supplied.",
        "context": %{<a href="/customers/equinix"><svg xmlns="http://www.w3.org/...</a>},
        "selector": "html > body > div:nth-child(9) > div:nth-child(2) > a:nth-child(18)",
        "runner": "htmlcs",
        "runnerExtras": {}
      }
    ]
  end

  describe '#scans_count' do
    subject { accessibility_report.scans_count }

    context 'when data has errors' do
      let(:different_url) { 'https://about.gitlab.com' }

      before do
        accessibility_report.add_url(url, data)
        accessibility_report.add_url(different_url, data)
      end

      it 'returns the scans_count' do
        expect(subject).to eq(2)
      end
    end

    context 'when data has no errors' do
      before do
        accessibility_report.add_url(url, [])
      end

      it 'returns the scans_count' do
        expect(subject).to eq(1)
      end
    end

    context 'when data has no url' do
      before do
        accessibility_report.add_url("", [])
      end

      it 'returns the scans_count' do
        expect(subject).to eq(0)
      end
    end
  end

  describe '#passes_count' do
    subject { accessibility_report.passes_count }

    context 'when data has errors' do
      before do
        accessibility_report.add_url(url, data)
      end

      it 'returns the passes_count' do
        expect(subject).to eq(0)
      end
    end

    context 'when data has no errors' do
      before do
        accessibility_report.add_url(url, [])
      end

      it 'returns the passes_count' do
        expect(subject).to eq(1)
      end
    end

    context 'when data has no url' do
      before do
        accessibility_report.add_url("", [])
      end

      it 'returns the scans_count' do
        expect(subject).to eq(0)
      end
    end
  end

  describe '#errors_count' do
    subject { accessibility_report.errors_count }

    context 'when data has errors' do
      let(:different_url) { 'https://about.gitlab.com' }

      before do
        accessibility_report.add_url(url, data)
        accessibility_report.add_url(different_url, data)
      end

      it 'returns the errors_count' do
        expect(subject).to eq(4)
      end
    end

    context 'when data has no errors' do
      before do
        accessibility_report.add_url(url, [])
      end

      it 'returns the errors_count' do
        expect(subject).to eq(0)
      end
    end

    context 'when data has no url' do
      before do
        accessibility_report.add_url("", [])
      end

      it 'returns the errors_count' do
        expect(subject).to eq(0)
      end
    end
  end

  describe '#add_url' do
    subject { accessibility_report.add_url(url, data) }

    context 'when data has errors' do
      it 'adds urls and data to accessibility report' do
        expect { subject }.not_to raise_error

        expect(accessibility_report.urls.keys).to eq([url])
        expect(accessibility_report.urls.values.flatten.size).to eq(2)
      end
    end

    context 'when data does not have errors' do
      let(:data) { [] }

      it 'adds data to accessibility report' do
        expect { subject }.not_to raise_error

        expect(accessibility_report.urls.keys).to eq([url])
        expect(accessibility_report.urls.values.flatten.size).to eq(0)
      end
    end

    context 'when url does not exist' do
      let(:url) { '' }
      let(:data) { [{ message: "Protocol error (Page.navigate): Cannot navigate to invalid URL" }] }

      it 'sets error_message and decreases total' do
        expect { subject }.not_to raise_error

        expect(accessibility_report.scans_count).to eq(0)
        expect(accessibility_report.error_message).to eq('Empty URL detected in gl-accessibility.json')
      end
    end
  end

  describe '#set_error_message' do
    let(:set_accessibility_error) { accessibility_report.set_error_message('error') }

    context 'when error is nil' do
      it 'returns the error' do
        expect(set_accessibility_error).to eq('error')
      end

      it 'sets the error' do
        set_accessibility_error

        expect(accessibility_report.error_message).to eq('error')
      end
    end

    context 'when a error has already been set' do
      before do
        accessibility_report.set_error_message('old error')
      end

      it 'overwrites the existing message' do
        expect { set_accessibility_error }.to change(accessibility_report, :error_message).from('old error').to('error')
      end
    end
  end

  describe '#all_errors' do
    subject { accessibility_report.all_errors }

    context 'when data has errors' do
      before do
        accessibility_report.add_url(url, data)
      end

      it 'returns all errors' do
        expect(subject.size).to eq(2)
      end
    end

    context 'when data has no errors' do
      before do
        accessibility_report.add_url(url, [])
      end

      it 'returns an empty array' do
        expect(subject).to eq([])
      end
    end

    context 'when accessibility report has no data' do
      it 'returns an empty array' do
        expect(subject).to eq([])
      end
    end
  end
end