aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/routes_controller_spec.rb
blob: 59020914dc9405467a379f9fadc3f2ec43e40df2 (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
RSpec.describe RoutesController, type: :controller do
  login_user

  let(:route) { create(:route) }

  it { is_expected.to be_kind_of(ChouetteController) }

  shared_examples_for "redirected to referential_line_path(referential,line)" do
    it "should redirect_to referential_line_path(referential,line)" do
      # expect(response).to redirect_to( referential_line_path(referential,route.line) )
    end
  end

  shared_examples_for "line and referential linked" do
    it "assigns route.line as @line" do
      expect(assigns[:line]).to eq(route.line)
    end

    it "assigns referential as @referential" do
      expect(assigns[:referential]).to eq(referential)
    end
  end

  shared_examples_for "route, line and referential linked" do
    it "assigns route as @route" do
      expect(assigns[:route]).to eq(route)
    end
    it_behaves_like "line and referential linked"
  end

  describe "GET /index" do
    before(:each) do
      get :index, line_id: route.line_id,
          referential_id: referential.id
    end

    it_behaves_like "line and referential linked"
    it_behaves_like "redirected to referential_line_path(referential,line)"
  end

  describe "POST /create" do
    before(:each) do
      post :create, line_id: route.line_id,
          referential_id: referential.id,
          route: { name: "changed", published_name: "published_name"}

    end
    it_behaves_like "line and referential linked"
    it_behaves_like "redirected to referential_line_path(referential,line)"
    it "sets metadata" do
      expect(Chouette::Route.last.metadata.creator_username).to eq @user.username
    end
  end

  describe "PUT /update" do
    before(:each) do
      put :update, id: route.id, line_id: route.line_id,
          referential_id: referential.id,
          route: route.attributes
    end

    it_behaves_like "route, line and referential linked"
    it_behaves_like "redirected to referential_line_path(referential,line)"
    it "sets metadata" do
      expect(Chouette::Route.last.metadata.modifier_username).to eq @user.username
    end
  end

  describe "GET /show" do
    before(:each) do
      get :show, id: route.id,
          line_id: route.line_id,
          referential_id: referential.id
    end

    it_behaves_like "route, line and referential linked"
  end

  describe "POST /duplicate" do
    let!( :route_prime ){ route }

    it "creates a new route" do
      expect do
        post :duplicate,
          referential_id: route.line.line_referential_id,
          line_id: route.line_id,
          id: route.id
      end.to change { Chouette::Route.count }.by(1)

      expect(Chouette::Route.last.name).to eq(I18n.t('activerecord.copy', name: route.name))
      expect(Chouette::Route.last.published_name).to eq(route.published_name)
      expect(Chouette::Route.last.stop_area_ids).to eq route.stop_area_ids
    end

    context "when opposite = true" do
      before do
        @positions = Hash[*route.stop_points.map{|sp| [sp.id, sp.position]}.flatten]
      end
      it "creates a new route on the opposite way " do
        expect do
          post :duplicate,
            referential_id: route.line.line_referential_id,
            line_id: route.line_id,
            id: route.id,
            opposite: TRUE
        end.to change { Chouette::Route.count }.by(1)

        expect(Chouette::Route.last.name).to eq(I18n.t('routes.opposite', name: route.name))
        expect(Chouette::Route.last.published_name).to eq(Chouette::Route.last.name)
        expect(Chouette::Route.last.opposite_route).to eq(route)
        expect(Chouette::Route.last.stop_area_ids).to eq route.stop_area_ids.reverse
        route.reload.stop_points.each do |sp|
          expect(sp.position).to eq @positions[sp.id]
        end
      end
    end

    context "on a duplicated route" do
      let!(:duplicated){ route.duplicate }
      it "creates a new route on the opposite way " do
        expect do
          post :duplicate,
            referential_id: duplicated.line.line_referential_id,
            line_id: duplicated.line_id,
            id: duplicated.id,
            opposite: TRUE
        end.to change { Chouette::Route.count }.by(1)

        expect(Chouette::Route.last.name).to eq(I18n.t('routes.opposite', name: duplicated.name))
        expect(Chouette::Route.last.published_name).to eq(Chouette::Route.last.name)
        expect(Chouette::Route.last.opposite_route).to eq(duplicated)
        expect(Chouette::Route.last.stop_area_ids).to eq duplicated.stop_area_ids.reverse
      end
    end
  end
end