# encoding: utf-8 require 'spec_helper' def em(start, stop) Prismic::Fragments::StructuredText::Span::Em.new(start, stop) end def strong(start, stop) Prismic::Fragments::StructuredText::Span::Strong.new(start, stop) end describe 'WebLink' do before do @web_link = Prismic::Fragments::WebLink.new('my_url') end describe 'as_html' do it "returns an HTML element" do Nokogiri::XML(@web_link.as_html).child.name.should == 'a' end it "returns a HTML element with an href attribute" do Nokogiri::XML(@web_link.as_html).child.has_attribute?('href').should be_true end it "returns a HTML element with an href attribute pointing to the url" do Nokogiri::XML(@web_link.as_html).child.attribute('href').value.should == 'my_url' end it "returns a HTML element whose content is the link" do Nokogiri::XML(@web_link.as_html).child.content.should == 'my_url' end end describe 'as_text' do it 'raises an NotImplementedError' do expect { @web_link.as_text }.to raise_error NotImplementedError end end describe 'url' do before do @link_resolver = Prismic.link_resolver("master"){|doc_link| "http://localhost/#{doc_link.id}" } end it 'works in a unified way' do @web_link.url(@link_resolver).should == 'my_url' end end end describe 'DocumentLink' do before do @document_link = Prismic::Fragments::DocumentLink.new("UdUjvt_mqVNObPeO", nil, "product", ["Macaron"], "dark-chocolate-macaron", "en-us", {}, false) end describe 'url' do before do @link_resolver = Prismic.link_resolver("master"){|doc_link| "http://localhost/#{doc_link.id}" } end it 'works in a unified way' do @document_link.url(@link_resolver).should == 'http://localhost/UdUjvt_mqVNObPeO' end end describe 'lang' do it 'is available' do @document_link.lang.should == 'en-us' end end end describe 'ImageLink' do before do @image_link = Prismic::Fragments::ImageLink.new('my_url') end describe 'as_html' do it "returns an HTML element" do Nokogiri::XML(@image_link.as_html).child.name.should == 'a' end it "returns a HTML element with an href attribute" do Nokogiri::XML(@image_link.as_html).child.has_attribute?('href').should be_true end it "returns a HTML element with an href attribute pointing to the url" do Nokogiri::XML(@image_link.as_html).child.attribute('href').value.should == 'my_url' end it "returns a HTML element whose content is the link" do Nokogiri::XML(@image_link.as_html).child.content.should == 'my_url' end end describe 'as_text' do it 'raises an NotImplementedError' do expect { @image_link.as_text }.to raise_error NotImplementedError end end describe 'url' do before do @link_resolver = Prismic.link_resolver('master'){|doc_link| "http://localhost/#{doc_link.id}" } end it 'works in a unified way' do @image_link.url(@link_resolver).should == 'my_url' end end end describe 'FileLink' do describe 'in structured texts' do before do @raw_json_structured_text = File.read("#{File.dirname(__FILE__)}/responses_mocks/structured_text_linkfile.json") @json_structured_text = JSON.load(@raw_json_structured_text) @structured_text = Prismic::JsonParser.structured_text_parser(@json_structured_text) end it 'serializes well into HTML' do @structured_text.as_html(nil).should == "

2012 Annual Report

\n\n"\ "

2012 Annual Budget

\n\n"\ "

2015 Vision & Strategic Plan

" end end end describe 'Span' do describe 'in structured texts when end is at the end of line' do before do @raw_json_structured_text = File.read("#{File.dirname(__FILE__)}/responses_mocks/structured_text_with_tricky_spans.json") @json_structured_text = JSON.load(@raw_json_structured_text) @structured_text = Prismic::JsonParser.structured_text_parser(@json_structured_text) end it 'serializes well into HTML' do @structured_text.as_html(nil).should == "

Powering Through 2013

\n\n"\ "

Online Resources:

\n\n"\ "" end end describe 'in structured texts when multiple spans' do before do @raw_json_structured_text = File.read("#{File.dirname(__FILE__)}/responses_mocks/structured_text_paragraph.json") @json_structured_text = JSON.load(@raw_json_structured_text) @structured_text = Prismic::JsonParser.structured_text_parser(@json_structured_text) end it 'serializes well into HTML' do @structured_text.as_html(nil).should == '

Experience the ultimate vanilla experience.
'\ 'Our vanilla Macarons are made with our very own (in-house) pure extract of Madagascar vanilla, and subtly dusted with our own vanilla sugar (which we make from real vanilla beans).

' end end describe 'in structured texts when image with link' do before do @raw_json_structured_text = File.read("#{File.dirname(__FILE__)}/responses_mocks/structured_text_image_with_link.json") @json_structured_text = JSON.load(@raw_json_structured_text) @structured_text = Prismic::JsonParser.structured_text_parser(@json_structured_text) end it 'serializes well into HTML' do @structured_text.as_html(nil).should == '

' end end end describe 'Text' do before do @text = Prismic::Fragments::Text.new('my_value') end describe 'as_html' do it "returns a HTML element" do Nokogiri::XML(@text.as_html).child.name.should == 'span' end it "returns a HTML element with the 'text' class" do Nokogiri::XML(@text.as_html).child.attribute('class').value.split.should include 'text' end it "returns a HTML element whose content is the value" do Nokogiri::XML(@text.as_html).child.content.should == 'my_value' end it "espaces HTML content" do @text = Prismic::Fragments::Text.new('&my #abcde') @text.as_html.should =~ /^<[^>]+>&my <value> #abcde<[^>]+>$/ end end describe 'as_text' do it 'return the value' do @text.as_text.should == 'my_value' end end end describe 'Select' do before do @select = Prismic::Fragments::Select.new('my_value') end describe 'as_html' do it "returns a HTML element" do Nokogiri::XML(@select.as_html).child.name.should == 'span' end it "returns a HTML element with the 'text' class" do Nokogiri::XML(@select.as_html).child.attribute('class').value.split.should include 'text' end it "returns a HTML element whose content is the value" do Nokogiri::XML(@select.as_html).child.content.should == 'my_value' end it "escapes HTML" do @select = Prismic::Fragments::Select.new('&my #abcde') @select.as_html(nil).should =~ %r{^<[^>]+>&my <value> #abcde<[^>]+>$} end end describe 'as_text' do it 'raises an NotImplementedError' do expect { @select.as_text }.to raise_error NotImplementedError end end end describe 'Date' do before do @date = Prismic::Fragments::Date.new(Time.new(2013, 8, 7, 11, 13, 7, '+02:00')) end describe 'as_html' do it "returns a