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
137
|
require "rails_helper"
describe GymsController do
let(:user) { create(:user) }
let(:user_session) { create(:user_session, location: portland, user: user) }
let(:portland) { create(:portland) }
before :each do
http_login(user, user_session)
end
describe "#index" do
let!(:sait) { create(:portland_gym, name: "sait") }
let!(:world_health) { create(:portland_gym, name: "world health") }
it "returns a list of gyms" do
get :index
expect(assigns(:gyms)).to match_array([sait, world_health])
expect(response).to be_ok
end
it "returns matching results" do
get :index, params: { q: "sait" }
expect(assigns(:gyms)).to match_array([sait])
expect(response).to be_ok
end
it "returns matches from yelp" do
gym = build(:gym)
yelp = double
allow(Search).to receive(:yelp).and_return(yelp)
allow(yelp).to receive(:for).and_return(Kaminari.paginate_array([gym]))
get :index, params: { q: "sait", source: "yelp" }
expect(assigns(:gyms)).to match_array([gym])
expect(response).to be_ok
end
end
describe "#new" do
it "loads the new page" do
get :new
expect(response).to be_ok
expect(assigns(:gym)).to be_instance_of(Gym)
expect(assigns(:gym).location).to be_present
end
it "loads the available countries" do
get :new
expect(assigns(:countries).count).to eql(248)
expect(assigns(:countries)).to include(%w{Canada CA})
end
end
describe "#create" do
context "valid params" do
before :each do
VCR.use_cassette("geo-location-sait") do
post :create, params: {
gym: {
name: "SAIT",
location_attributes: {
address: "1301 16 Ave NW",
city: "Calgary",
region: "AB",
country: "CA",
postal_code: "T2M 0L4",
}
}
}
end
end
it "redirects to the listing page" do
expect(response).to redirect_to(gyms_path(q: "SAIT"))
end
it "creates a new gym" do
expect(Gym.count).to eql(1)
gym = Gym.last
expect(gym.name).to eql("SAIT")
expect(gym.location).to be_present
expect(gym.location.address).to eql("1301 16 Ave NW")
expect(gym.location.city).to eql("Calgary")
expect(gym.location.region).to eql("AB")
expect(gym.location.country).to eql("CA")
expect(gym.location.postal_code).to eql("T2M 0L4")
end
end
context "with the yelp id" do
render_views
let(:yelp_id) { "sait-campus-centre-calgary" }
let(:gym) { build(:gym, yelp_id: yelp_id) }
before :each do
allow(Gym).to receive(:create_from_yelp!).
with(yelp_id).
and_return(gym)
end
it "returns a json response" do
post :create, params: { yelp_id: yelp_id }, xhr: true
json = JSON.parse(response.body)
expect(json["yelp_id"]).to eql(yelp_id)
expect(json["name"]).to eql(gym.name)
expect(json["full_address"]).to eql(gym.full_address)
end
end
context "invalid params" do
before :each do
post :create, params: { gym: { name: "" } }
end
it "displays an error" do
expect(flash[:error]).to eql(assigns(:gym).errors.full_messages)
end
it "renders the form with the original values entered" do
expect(response).to render_template(:new)
expect(assigns(:gym)).to be_present
end
end
end
describe "#show" do
it "loads the gym" do
gym = create(:gym)
get :show, params: { id: gym.id }
expect(assigns(:gym)).to eql(gym)
end
end
end
|