aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/public_body_heading_spec.rb
blob: be3e7c7d29341e3207dad96a51139afb5eb1352f (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
# == Schema Information
#
# Table name: public_body_headings
#
#  id            :integer          not null, primary key
#  display_order :integer
#

require 'spec_helper'

describe PublicBodyHeading do

    context 'when validating' do

        it 'should require a name' do
            heading = PublicBodyHeading.new
            heading.should_not be_valid
            heading.errors[:name].should == ["Name can't be blank"]
        end

        it 'should require a unique name' do
            heading = FactoryGirl.create(:public_body_heading)
            new_heading = PublicBodyHeading.new(:name => heading.name)
            new_heading.should_not be_valid
            new_heading.errors[:name].should == ["Name is already taken"]
        end

        it 'should set a default display order based on the next available display order' do
            heading = PublicBodyHeading.new
            heading.valid?
            heading.display_order.should == PublicBodyHeading.next_display_order
        end

        it 'validates the translations' do
            heading = FactoryGirl.build(:public_body_heading)
            translation = heading.translations.build
            expect(heading).to_not be_valid
        end

    end

    context 'when setting a display order' do

        it 'should return 0 if there are no public body headings' do
            PublicBodyHeading.next_display_order.should == 0
        end

        it 'should return one more than the highest display order if there are public body headings' do
            heading = FactoryGirl.create(:public_body_heading)
            PublicBodyHeading.next_display_order.should == 1
        end
    end

    describe :save do

      it 'saves translations' do
          heading = FactoryGirl.build(:public_body_heading)
          heading.translations_attributes = { :es => { :locale => 'es',
                                                       :name => 'El Heading' } }

          heading.save
          expect(PublicBodyHeading.find(heading.id).translations.size).to eq(2)
      end

    end

    describe :translations_attributes= do

        context 'translation_attrs is a Hash' do

            it 'does not persist translations' do
                heading = FactoryGirl.create(:public_body_heading)
                heading.translations_attributes = { :es => { :locale => 'es',
                                                             :name => 'El Heading' } }

                expect(PublicBodyHeading.find(heading.id).translations.size).to eq(1)
            end

            it 'creates a new translation' do
                heading = FactoryGirl.create(:public_body_heading)
                heading.translations_attributes = { :es => { :locale => 'es',
                                                             :name => 'El Heading' } }
                heading.save
                heading.reload
                expect(heading.name(:es)).to eq('El Heading')
            end

            it 'updates an existing translation' do
                heading = FactoryGirl.create(:public_body_heading)
                heading.translations_attributes = { 'es' => { :locale => 'es',
                                                              :name => 'Name' } }
                heading.save

                heading.translations_attributes = { 'es' => { :id => heading.translation_for(:es).id,
                                                              :locale => 'es',
                                                              :name => 'Renamed' } }
                heading.save
                expect(heading.name(:es)).to eq('Renamed')
            end

            it 'updates an existing translation and creates a new translation' do
                heading = FactoryGirl.create(:public_body_heading)
                heading.translations.create(:locale => 'es',
                                             :name => 'Los Heading')

                expect(heading.translations.size).to eq(2)

                heading.translations_attributes = {
                    'es' => { :id => heading.translation_for(:es).id,
                              :locale => 'es',
                              :name => 'Renamed' },
                    'fr' => { :locale => 'fr',
                              :name => 'Le Heading' }
                }

                expect(heading.translations.size).to eq(3)
                I18n.with_locale(:es) { expect(heading.name).to eq('Renamed') }
                I18n.with_locale(:fr) { expect(heading.name).to eq('Le Heading') }
            end

            it 'skips empty translations' do
                heading = FactoryGirl.create(:public_body_heading)
                heading.translations.create(:locale => 'es',
                                             :name => 'Los Heading')

                expect(heading.translations.size).to eq(2)

                heading.translations_attributes = {
                    'es' => { :id => heading.translation_for(:es).id,
                              :locale => 'es',
                              :name => 'Renamed' },
                    'fr' => { :locale => 'fr' }
                }

                expect(heading.translations.size).to eq(2)
            end
        end
    end
end

describe PublicBodyHeading::Translation do

  it 'requires a locale' do
    translation = PublicBodyHeading::Translation.new
    translation.valid?
    expect(translation.errors[:locale]).to eq(["can't be blank"])
  end

  it 'is valid if all required attributes are assigned' do
    translation = PublicBodyHeading::Translation.new(:locale => I18n.default_locale)
    expect(translation).to be_valid
  end

end