~cytrogen/masto-fe

ref: 4adb12ca9157ae0efc0b813df3a09ef8f6d579de masto-fe/spec/validators/language_validator_spec.rb -rw-r--r-- 1.5 KiB
4adb12ca — Claire Fix test failures due to different default settings in glitch-soc 2 years ago
                                                                                
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
# frozen_string_literal: true

require 'rails_helper'

describe LanguageValidator do
  let(:record_class) do
    Class.new do
      include ActiveModel::Validations
      attr_accessor :locale

      validates :locale, language: true
    end
  end
  let(:record) { record_class.new }

  describe '#validate_each' do
    context 'with a nil value' do
      it 'does not add errors' do
        record.locale = nil

        expect(record).to be_valid
        expect(record.errors).to be_empty
      end
    end

    context 'with an array of values' do
      it 'does not add errors with array of existing locales' do
        record.locale = %w(en fr)

        expect(record).to be_valid
        expect(record.errors).to be_empty
      end

      it 'adds errors with array having some non-existing locales' do
        record.locale = %w(en fr missing)

        expect(record).to_not be_valid
        expect(record.errors.first.attribute).to eq(:locale)
        expect(record.errors.first.type).to eq(:invalid)
      end
    end

    context 'with a locale string' do
      it 'does not add errors when string is an existing locale' do
        record.locale = 'en'

        expect(record).to be_valid
        expect(record.errors).to be_empty
      end

      it 'adds errors when string is non-existing locale' do
        record.locale = 'missing'

        expect(record).to_not be_valid
        expect(record.errors.first.attribute).to eq(:locale)
        expect(record.errors.first.type).to eq(:invalid)
      end
    end
  end
end