require 'spec/spec_helper' describe Shop::Tags::Product do dataset :pages, :shop_config, :shop_products, :shop_product_attachments it 'should describe these tags' do Shop::Tags::Product.tags.sort.should == [ 'shop:products', 'shop:products:if_products', 'shop:products:unless_products', 'shop:products:each', 'shop:product', 'shop:product:id', 'shop:product:name', 'shop:product:price', 'shop:product:sku', 'shop:product:slug', 'shop:product:description', 'shop:product:link', 'shop:product:images', 'shop:product:images:if_images', 'shop:product:images:unless_images', 'shop:product:images:each', 'shop:product:images:image'].sort end before :all do @page = pages(:home) end before(:each) do @product = shop_products(:soft_bread) @products = [ shop_products(:soft_bread), shop_products(:crusty_bread) ] @image = images(:soft_bread_front) @images = [ images(:soft_bread_front), images(:soft_bread_back), images(:soft_bread_top) ] end describe '' do context 'no products exist' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { [] } end it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'products exist' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { @products } end it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end end describe '' do context 'success' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { @products } end it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'failure' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { [] } end it 'should not render' do tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end end describe '' do context 'success' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { [] } end it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'failure' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { @products } end it 'should not render' do tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end end describe '' do context 'success' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { @products } end it 'should not render' do tag = %{} exp = @products.map{ |p| p.id }.join('') @page.should render(tag).as(exp) end end context 'failure' do before :each do mock(Shop::Tags::Helpers).current_products(anything) { [] } end it 'should not render' do tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end end describe '' do context 'product exists in the context' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @products } end it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'product does not exist in the context' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { [] } end it 'should not render' do tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end context '#attributes' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end describe '' do it 'should render the product id' do tag = %{} exp = @product.id.to_s @page.should render(tag).as(exp) end end describe '' do it 'should render the product name' do tag = %{} exp = @product.name @page.should render(tag).as(exp) end end describe '' do it 'should render the product sku' do tag = %{} exp = @product.sku @page.should render(tag).as(exp) end end describe '' do it 'should render the product slug' do tag = %{} exp = @product.slug @page.should render(tag).as(exp) end end describe '' do it 'should render a textile filtered description' do tag = %{} exp = TextileFilter.filter(@product.description) @page.should render(tag).as(exp) end end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end context 'standalone' do it 'should render an anchor element' do tag = %{} exp = %{#{@product.name}} @page.should render(tag).as(exp) end it 'should assign attributes' do tag = %{} exp = %{#{@product.name}} @page.should render(tag).as(exp) end end context 'wrapped' do it 'should render an anchor element' do tag = %{title} exp = %{title} @page.should render(tag).as(exp) end end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } stub(@product).price { 1234.34567890 } end it 'should render a standard price' do tag = %{} exp = %{$1,234.35} @page.should render(tag).as(exp) end it 'should not be testing precision here' it 'should render a high precision price' do tag = %{} exp = %{$1,234.34567890} @page.should render(tag).as(exp) end it 'should render a custom format' do tag = %{} exp = %{%1+234-35} @page.should render(tag).as(exp) end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end context 'success' do it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'failure' do it 'should not render' do @product.images.delete_all tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end context 'success' do it 'should render' do @product.images.delete_all tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'failure' do it 'should not render' do tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end it 'should render' do tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end context 'success' do it 'should assign the local image for each' do tag = %{} exp = @product.attachments.map{ |i| i.id }.join('') @page.should render(tag).as(exp) end end context 'failure' do it 'should not render' do @product.images.delete_all tag = %{failure} exp = %{} @page.should render(tag).as(exp) end end end describe '' do before :each do mock(Shop::Tags::Helpers).current_product(anything) { @product } end context 'image exists' do it 'should expand' do mock(Shop::Tags::Helpers).current_image(anything) { @product.images.first } tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end context 'image does not exist' do it 'should expand' do mock(Shop::Tags::Helpers).current_image(anything) { nil } tag = %{success} exp = %{success} @page.should render(tag).as(exp) end end end end end