summaryrefslogtreecommitdiff
path: root/spec/controllers/workouts_controller_spec.rb
blob: dd0f9d7d11d903b0ecfaafd4bcf5c96693bdc95b (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
require "rails_helper"

describe WorkoutsController do
  let(:user) { create(:user) }

  before :each do
    http_login(user)
  end

  describe "#index" do
    include_context "stronglifts_program"
    let!(:workout_a) { create(:workout, user: user, routine: routine_a, occurred_at: 1.week.ago) }
    let!(:workout_b) { create(:workout, user: user, routine: routine_b, occurred_at: 1.day.ago) }

    it "loads all my workouts" do
      get :index
      expect(assigns(:workouts)).to match_array([workout_a, workout_b])
    end

    it "loads all works since a given time" do
      get :index, params: { since: 2.days.to_i }
      expect(assigns(:workouts)).to match_array([workout_b])
    end
  end

  describe "#new" do
    include_context "stronglifts_program"

    it "loads the next routine in the program" do
      create(:workout, user: user, routine: routine_a)

      get :new

      expect(assigns(:routine)).to eql(routine_b)
    end

    it "loads the first routine in the program" do
      get :new

      expect(assigns(:routine)).to eql(routine_a)
    end
  end

  describe "#create" do
    include_context "stronglifts_program"
    let(:body_weight) { rand(250.0).lbs }

    it "creates a new workout" do
      post :create, params: {
        workout: {
          routine_id: routine_b.id,
          body_weight: body_weight
        }
      }
      expect(user.reload.workouts.count).to eql(1)
      expect(user.last_routine).to eql(routine_b)
      expect(user.workouts.last.body_weight).to eql(body_weight)
      expect(response).to redirect_to(edit_workout_path(user.workouts.last))
    end

    it "creates the workout with the selected exercises" do
      post :create, params: {
        workout: {
          routine_id: routine_b.id,
          body_weight: body_weight,
          exercise_sets_attributes: [{
            exercise_id: squat.id,
            target_repetitions: 5,
            target_weight: 200,
            type: "WorkSet",
          }]
        }
      }

      expect(user.reload.workouts.count).to eql(1)
      expect(user.last_routine).to eql(routine_b)
      workout = user.workouts.last
      expect(workout.body_weight).to eql(body_weight)
      expect(workout.routine).to eql(routine_b)
      expect(workout.sets.count).to eql(1)
      expect(response).to redirect_to(edit_workout_path(user.workouts.last))
    end
  end

  describe "#edit" do
    let(:workout) { create(:workout, user: user) }

    it "loads the workouts" do
      get :edit, params: { id: workout.id }
      expect(assigns(:workout)).to eql(workout)
    end
  end
end