master
 1require "rails_helper"
 2
 3describe Api::WorkoutsController do
 4  render_views
 5  let(:user) { create(:user) }
 6
 7  before :each do
 8    api_login(user)
 9  end
10
11  describe "#index" do
12    it "returns each workout" do
13      workout = create(:workout, user: user)
14
15      get :index, format: :json
16
17      expect(response).to have_http_status(:ok)
18      json = JSON.parse(response.body, symbolize_names: true)
19      expect(json[:workouts].count).to eql(1)
20    end
21  end
22
23  describe "#new" do
24    include_context "stronglifts_program"
25
26    it 'loads the next workout' do
27      get :new, format: :json
28
29      expect(response).to have_http_status(:ok)
30      json = JSON.parse(response.body, symbolize_names: true)
31
32      expect(json[:body_weight]).to eql({ amount: 0.0, unit: 'lbs' })
33      expect(json[:routine]).to eql({ id: routine_a.id, name: routine_a.name })
34      expect(json[:exercises]).to match_array([
35        { id: barbell_row.id, name: barbell_row.name },
36        { id: bench_press.id, name: bench_press.name, },
37        { id: squat.id, name: squat.name, },
38      ])
39      expect(json[:sets]).to match_array([
40        { 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 },
41        { 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 },
42        { 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 },
43        { 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 },
44        { 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 },
45        { 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 },
46        { 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 },
47        { 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 },
48        { 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 },
49        { 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 },
50        { 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 },
51        { 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 },
52        { 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 },
53        { 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 },
54        { 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 },
55      ])
56    end
57  end
58
59  describe "#create" do
60    include_context "stronglifts_program"
61    let(:body_weight) { rand(250.0).lbs }
62
63    it "creates the workout with the selected exercises" do
64      post :create, params: {
65        workout: {
66          routine_id: routine_b.id,
67          body_weight: { amount: body_weight.amount, unit: body_weight.unit },
68          exercise_sets_attributes: [{
69            exercise_id: squat.id,
70            target_repetitions: 5,
71            target_weight: { amount: 200, unit: 'lbs' },
72            type: "WorkSet",
73          }]
74        }
75      }, format: :json
76
77      expect(response).to have_http_status(:created)
78      expect(user.reload.workouts.count).to eql(1)
79      expect(user.last_routine).to eql(routine_b)
80      workout = user.workouts.last
81      expect(workout.body_weight).to eql(body_weight)
82      expect(workout.routine).to eql(routine_b)
83      expect(workout.sets.count).to eql(1)
84    end
85  end
86end