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

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

  before :each do
    api_login(user)
  end

  describe "#index" do
    it "returns each workout" do
      workout = create(:workout, user: user)

      get :index, format: :json

      expect(response).to have_http_status(:ok)
      json = JSON.parse(response.body, symbolize_names: true)
      expect(json[:workouts].count).to eql(1)
    end
  end

  describe "#new" do
    include_context "stronglifts_program"

    it 'loads the next workout' do
      get :new, format: :json

      expect(response).to have_http_status(:ok)
      json = JSON.parse(response.body, symbolize_names: true)

      expect(json[:body_weight]).to eql({ amount: 0.0, unit: 'lbs' })
      expect(json[:routine]).to eql({ id: routine_a.id, name: routine_a.name })
      expect(json[:exercises]).to match_array([
        { id: barbell_row.id, name: barbell_row.name },
        { id: bench_press.id, name: bench_press.name, },
        { id: squat.id, name: squat.name, },
      ])
      expect(json[:sets]).to match_array([
        { id: nil, exercise_id: squat.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: squat.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: squat.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: squat.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: squat.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: bench_press.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: bench_press.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: bench_press.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: bench_press.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: bench_press.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: barbell_row.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: barbell_row.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: barbell_row.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: barbell_row.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
        { id: nil, exercise_id: barbell_row.id, type: 'WorkSet', target_weight: 45.lbs.to_h, target_repetitions: 5, actual_repetitions: nil, target_duration: nil, actual_duration: nil },
      ])
    end
  end

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

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

      expect(response).to have_http_status(:created)
      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)
    end
  end
end