Skip to content

Instantly share code, notes, and snippets.

@Dkendal
Created May 26, 2014 23:34
Show Gist options
  • Save Dkendal/b4dd1cfe33f5a2f5f97a to your computer and use it in GitHub Desktop.
Save Dkendal/b4dd1cfe33f5a2f5f97a to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe DietSearcher, type: :model do
let(:params) do
{ 'per_page' => '',
'product_search_form' => { 'allergens_ids' => ['1'] } }
end
describe 'Spree searcher intergration', type: :controller do
let(:vegan_cookies) { create :wellness_product }
let(:cookies) { create :wellness_product, allergens: egg }
let(:egg) { create :allergen, id: 1 }
controller Spree::StoreController do
def index
@searcher = build_searcher(params)
@products = @searcher.retrieve_products
render text: 'sup'
end
end
it "it calls #{described_class}'s retrieve_products" do
get :index
end
context 'when a filter is applied' do
before { get :index, params }
let(:params) { {
per_page: 100,
product_search_form: filter_params } }
context "and it's an inclusionary filter" do
describe 'filtering for lifestyles' do
subject { assigns(:products) }
let!(:vegan) { create :lifestyle }
let!(:raw) { create :lifestyle }
let!(:filter_params) { { lifestyles_ids: [ vegan, raw ] } }
context 'when a product fits some, but not all of the lifestyles' do
let!(:vegan_cookies) { create :wellness_product, lifestyles: vegan }
it 'is not returned' do
expect(subject).to_not include vegan_cookies
end
end
context 'when a product fits all of the lifestyles' do
let!(:almonds) { create :wellness_product, lifestyles: [vegan, raw] }
it 'is returned' do
expect(subject).to include almonds
end
end
context 'when a product fits none of the lifestyles' do
let!(:steak) { create :wellness_product }
it 'is not returned' do
expect(subject).to_not include steak
end
end
end
end
context "and it's an exclusionary filter" do
describe 'filtering against allergens' do
subject { assigns(:products) }
let!(:shellfish) { create :allergen }
let!(:egg) { create :allergen }
let!(:filter_params) { { allergens_ids: [shellfish.id, egg.id] } }
context 'when a product has any of the allergens' do
let!(:crab_cake) { create :wellness_product, ingredient_allergens: shellfish }
it 'is not returned' do
is_expected.to_not include crab_cake
end
end
context 'when a product has all of the allergens' do
let!(:tempura) { create :wellness_product, ingredient_allergens: [egg, shellfish] }
it 'is not returned' do
is_expected.to_not include tempura
end
end
context 'when a a product has none of the allergen' do
let!(:bacon) { create :wellness_product }
it 'is returned' do
is_expected.to include bacon
end
end
end
end
end
context 'when no filter is applied' do
it 'returns all products' do
get :index
expect(assigns(:products)).to eq [ vegan_cookies, cookies ]
end
end
end
describe '.product_search_form' do
let(:params) do
{ 'product_search_form' => { 'allergens_ids' => ['1'] } }
end
let(:searcher) { described_class.new(params) }
subject { searcher.send :product_search_form }
it { is_expected.to be_a ProductSearchForm }
it "is initialized with the params hash" do
expect(subject.allergens_ids).to eq [ 1 ]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment