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

color_validator_spec.rb « validators « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c1339caffbb71c840f48c426238e8d295790740 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ColorValidator do
  using RSpec::Parameterized::TableSyntax

  subject do
    Class.new do
      include ActiveModel::Model
      include ActiveModel::Validations
      attr_accessor :color

      validates :color, color: true
    end.new
  end

  where(:color, :is_valid) do
    '#000abc'    | true
    '#aaa'       | true
    '#BBB'       | true
    '#cCc'       | true
    '#ffff'      | false
    '#000111222' | false
    'invalid'    | false
    'red'        | false
    '000'        | false
    nil          | true # use presence to validate non-nil
    ''           | false
    Time.current | false
    ::Gitlab::Color.of(:red) | true
  end

  with_them do
    it 'only accepts valid colors' do
      subject.color = color

      expect(subject.valid?).to eq(is_valid)
    end
  end

  it 'fails fast for long invalid string' do
    subject.color = '#' + ('0' * 50_000) + 'xxx'

    expect do
      Timeout.timeout(5.seconds) { subject.valid? }
    end.not_to raise_error
  end

  context 'when color must be present' do
    subject do
      Class.new do
        include ActiveModel::Model
        include ActiveModel::Validations
        attr_accessor :color

        validates :color, color: true, presence: true
      end.new
    end

    it 'rejects nil' do
      subject.color = nil

      expect(subject).not_to be_valid
    end
  end
end