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

require 'spec_helper'

describe PublicBodyHeading do

    context 'when loading the data' do

        before do
            PublicBodyCategory.add(:en, [
                  "Local and regional",
                      [ "local_council", "Local councils", "a local council" ],
                  "Miscellaneous",
                      [ "other", "Miscellaneous", "miscellaneous" ],])
        end

        it 'should use the display_order field to preserve the original data order' do
            headings = PublicBodyHeading.all
            headings[0].name.should eq 'Local and regional'
            headings[0].display_order.should eq 0
            headings[1].name.should eq 'Miscellaneous'
            headings[1].display_order.should eq 1
        end

    end

    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
    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
end