summaryrefslogtreecommitdiff
path: root/spec/models/creation/repository_spec.rb
blob: bd09b0fc4a49a0d8335d32147a6b8651f1459e40 (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
require "rails_helper"

describe Creation::Repository do
  describe "#tagged" do
    let(:user) { create(:user) }
    let(:tag) { "cake" }
    let(:tagged_cake) { create(:creation, photos_count: 1) }
    let(:untagged_cake) { create(:creation) }

    before :each do
      user.tag(tagged_cake, with: tag, on: :tags)
    end

    it "returns each cake that is tagged" do
      expect(subject.tagged(tag)).to include(tagged_cake)
    end

    it "does not return cakes that are not tagged" do
      expect(subject.tagged(tag)).to_not include(untagged_cake)
    end
  end

  describe "#published" do
    let!(:user){ create(:user) }
    let!(:published_cake){ create(:creation, user: user) }

    before :each do
      published_cake.photos.create(image: "example.png", image_processing: nil)
    end

    let(:results) { subject.published }

    it "returns cakes that do not have photos that are processing" do
      expect(results.count).to eql(1)
      expect(results).to match_array([published_cake])
    end
  end

  describe "#search" do
    let(:cake) { create(:creation, name: "Cake") }
    let(:cup_cake) { create(:creation, name: "Cup Cake") }

    it "returns cakes with a matching name" do
      expect(subject.search("cake")).to match_array([cake])
    end
  end

  context "#search_with" do
    let(:cake_category) { create(:category) }
    let(:cookie_category) { create(:category) }
    let!(:cake) { create(:cake, name: "cake", category: cake_category) }
    let!(:cookie) { create(:cake, name: "cookie", category: cookie_category) }

    before :each do
      cake.photos << create(:photo)
      cookie.photos << create(:photo)
    end

    it "returns all cakes in a specific category" do
      cakes = subject.search_with(category: cake_category.slug)
      expect(cakes).to match_array([cake])
    end

    it "returns all cakes that match the search query" do
      cakes = subject.search_with(q: cake.name[0..6])
      expect(cakes).to match_array([cake])
    end
  end
end