master
 1require "rails_helper"
 2
 3module Api
 4  module V2
 5    describe CakesController do
 6      render_views
 7
 8      describe "#index" do
 9        let!(:cakes) { create(:category, slug: "cakes") }
10        let!(:cookies_category) { create(:category, slug: "cookies") }
11        let!(:cake) { create(:published_cake, name: "cake", category: cakes) }
12        let!(:cookie) do
13          create(:published_cake, name: "cookie", category: cookies_category)
14        end
15        let!(:unpublished_cake) do
16          create(:cake, name: "unpublished", category: cakes)
17        end
18
19        it "returns all published cakes" do
20          get :index, xhr: true
21          expect(assigns(:cakes)).to match_array([cake, cookie])
22        end
23
24        it "returns all cakes in the category" do
25          get :index, params: { category: cookie.category.slug }, xhr: true
26          expect(assigns(:cakes)).to match_array([cookie])
27        end
28
29        it "returns all cakes matching the search query" do
30          get :index, params: { q: cake.name[0..2] }, xhr: true
31          expect(assigns(:cakes)).to match_array([cake])
32        end
33
34        it "returns all cakes tagged with the tag" do
35          cake.tag_list = "cakes"
36          cake.save!
37
38          get :index, params: { tags: "cakes" }, xhr: true
39          expect(assigns(:cakes)).to match_array([cake])
40        end
41      end
42
43      describe "#show" do
44        let!(:cake) { create(:published_cake) }
45
46        it "loads the cake" do
47          get :show, params: { id: cake.id }, xhr: true
48          expect(assigns(:cake)).to eql(cake)
49        end
50      end
51    end
52  end
53end