master
  1require "rails_helper"
  2
  3describe GymsController do
  4  let(:user) { create(:user) }
  5  let(:user_session) { create(:user_session, location: portland, user: user) }
  6  let(:portland) { create(:portland) }
  7
  8  before :each do
  9    http_login(user, user_session)
 10  end
 11
 12  describe "#index" do
 13    let!(:sait) { create(:portland_gym, name: "sait") }
 14    let!(:world_health) { create(:portland_gym, name: "world health") }
 15
 16    it "returns a list of gyms" do
 17      get :index
 18
 19      expect(assigns(:gyms)).to match_array([sait, world_health])
 20      expect(response).to be_ok
 21    end
 22
 23    it "returns matching results" do
 24      get :index, params: { q: "sait" }
 25
 26      expect(assigns(:gyms)).to match_array([sait])
 27      expect(response).to be_ok
 28    end
 29
 30    it "returns matches from yelp" do
 31      gym = build(:gym)
 32      yelp = double
 33      allow(Search).to receive(:yelp).and_return(yelp)
 34      allow(yelp).to receive(:for).and_return(Kaminari.paginate_array([gym]))
 35      get :index, params: { q: "sait", source: "yelp" }
 36
 37      expect(assigns(:gyms)).to match_array([gym])
 38      expect(response).to be_ok
 39    end
 40  end
 41
 42  describe "#new" do
 43    it "loads the new page" do
 44      get :new
 45      expect(response).to be_ok
 46      expect(assigns(:gym)).to be_instance_of(Gym)
 47      expect(assigns(:gym).location).to be_present
 48    end
 49
 50    it "loads the available countries" do
 51      get :new
 52      expect(assigns(:countries).count).to eql(248)
 53      expect(assigns(:countries)).to include(%w{Canada CA})
 54    end
 55  end
 56
 57  describe "#create" do
 58    context "valid params" do
 59      before :each do
 60        VCR.use_cassette("geo-location-sait") do
 61          post :create, params: {
 62            gym: {
 63              name: "SAIT",
 64              location_attributes: {
 65                address: "1301 16 Ave NW",
 66                city: "Calgary",
 67                region: "AB",
 68                country: "CA",
 69                postal_code: "T2M 0L4",
 70              }
 71            }
 72          }
 73        end
 74      end
 75
 76      it "redirects to the listing page" do
 77        expect(response).to redirect_to(gyms_path(q: "SAIT"))
 78      end
 79
 80      it "creates a new gym" do
 81        expect(Gym.count).to eql(1)
 82        gym = Gym.last
 83        expect(gym.name).to eql("SAIT")
 84        expect(gym.location).to be_present
 85        expect(gym.location.address).to eql("1301 16 Ave NW")
 86        expect(gym.location.city).to eql("Calgary")
 87        expect(gym.location.region).to eql("AB")
 88        expect(gym.location.country).to eql("CA")
 89        expect(gym.location.postal_code).to eql("T2M 0L4")
 90      end
 91    end
 92
 93    context "with the yelp id" do
 94      render_views
 95
 96      let(:yelp_id) { "sait-campus-centre-calgary" }
 97      let(:gym) { build(:gym, yelp_id: yelp_id) }
 98
 99      before :each do
100        allow(Gym).to receive(:create_from_yelp!).
101          with(yelp_id).
102          and_return(gym)
103      end
104
105      it "returns a json response" do
106        post :create, params: { yelp_id: yelp_id }, xhr: true
107        json = JSON.parse(response.body)
108        expect(json["yelp_id"]).to eql(yelp_id)
109        expect(json["name"]).to eql(gym.name)
110        expect(json["full_address"]).to eql(gym.full_address)
111      end
112    end
113
114    context "invalid params" do
115      before :each do
116        post :create, params: { gym: { name: "" } }
117      end
118
119      it "displays an error" do
120        expect(flash[:error]).to eql(assigns(:gym).errors.full_messages)
121      end
122
123      it "renders the form with the original values entered" do
124        expect(response).to render_template(:new)
125        expect(assigns(:gym)).to be_present
126      end
127    end
128  end
129
130  describe "#show" do
131    it "loads the gym" do
132      gym = create(:gym)
133      get :show, params: { id: gym.id }
134      expect(assigns(:gym)).to eql(gym)
135    end
136  end
137end