spec/page_spec.rb in nesta-0.9.13 vs spec/page_spec.rb in nesta-0.10.0
- old
+ new
@@ -1,293 +1,354 @@
require File.expand_path('spec_helper', File.dirname(__FILE__))
require File.expand_path('model_factory', File.dirname(__FILE__))
-describe "page with keyword and description", :shared => true do
+shared_examples_for "page with keyword and description" do
it "should set the keywords meta tag" do
do_get
- body.should have_tag("meta[@name=keywords][@content='#{@keywords}']")
+ assert_xpath "//meta[@name='keywords'][@content='#{@keywords}']"
end
it "should set description meta tag" do
do_get
- body.should have_tag("meta[@name=description][@content='#{@description}']")
+ assert_xpath "//meta[@name='description'][@content='#{@description}']"
end
end
-describe "page that can display menus", :shared => true do
+shared_examples_for "page that can display menus" do
it "should not display menu by default" do
do_get
- body.should_not have_tag("#sidebar ul.menu")
+ last_response.should be_ok
+ assert_not_selector "#sidebar ul.menu"
end
describe "and simple menu configured" do
before(:each) do
create_menu(@category.path)
end
it "should link to top level menu items" do
do_get
- body.should have_tag(
- "ul.menu a[@href$=#{@category.abspath}]", Regexp.new(@category.heading))
+ link_text = @category.link_text
+ href = @category.abspath
+ assert_selector "ul.menu a[@href$='#{href}']:contains('#{link_text}')"
end
end
-
+
describe "and nested menu configured" do
before(:each) do
- @level2 = create_category(:path => "level-2", :heading => "Level 2")
- @level3 = create_category(:path => "level-3", :heading => "Level 3")
+ @level2 = create_category(path: "level-2", heading: "Level 2",
+ metadata: {'link text' => "Level 2 link"})
+ @level3 = create_category(path: "level-3", heading: "Level 3")
text = <<-EOF
#{@category.abspath}
#{@level2.abspath}
#{@level3.abspath}
EOF
create_menu(text)
end
it "should display first level of nested sub menus" do
do_get
- body.should have_tag("ul.menu li ul li a", Regexp.new(@level2.heading))
+ assert_selector "ul.menu li ul li a:contains('#{@level2.link_text}')"
end
it "should not display nested menus to arbitrary depth" do
do_get
- body.should_not have_tag("ul.menu li ul li ul")
+ last_response.should be_ok
+ assert_not_selector "ul.menu li ul li ul"
end
end
+
+ describe "and menu links to home page" do
+ before(:each) do
+ text = <<-EOF
+/
+ #{@category.abspath}
+EOF
+ create_menu(text)
+ template_path = File.expand_path(
+ 'templates', File.dirname(File.dirname(__FILE__)))
+ @default_homepage_content = File.read(File.join(template_path,
+ 'index.haml'))
+ end
+
+ it "should use 'Home' as the home page link if not otherwise specified" do
+ create_page(
+ path: 'index',
+ ext: :haml,
+ content: @default_homepage_content)
+ do_get
+ assert_selector "ul.menu a[@href='/']:contains('Home')"
+ end
+
+ it "should use the heading if it exists" do
+ create_page(
+ path: 'index',
+ ext: :haml,
+ heading: 'My heading',
+ content: @default_homepage_content)
+ do_get
+ assert_selector "ul.menu a[@href='/']:contains('My heading')"
+ end
+
+ it "should use the link text if specified" do
+ create_page(
+ path: 'index',
+ ext: :haml,
+ heading: 'My heading',
+ content: @default_homepage_content,
+ metadata: {'link text'=>'My link text'})
+ do_get
+ assert_selector "ul.menu a[@href='/']:contains('My link text')"
+ end
+ end
end
describe "The layout" do
include ModelFactory
include RequestSpecHelper
-
+
it "should not include GA JavaScript by default" do
stub_configuration
get "/"
- body.should_not have_tag("script", /'_setAccount', 'UA-1234'/)
+ assert_not_selector "script", content: "'_setAccount', 'UA-1234'"
end
-
+
it "should include GA JavaScript if configured" do
- stub_config_key('google_analytics_code', 'UA-1234', :rack_env => true)
+ stub_config_key('google_analytics_code', 'UA-1234', rack_env: true)
stub_configuration
- get '/'
- body.should have_tag('script', /'_setAccount', 'UA-1234'/)
+ get "/"
+ assert_selector 'script', content: "'_setAccount', 'UA-1234'"
end
end
describe "The home page" do
include ModelFactory
include RequestSpecHelper
-
+
before(:each) do
stub_configuration
template_path = File.expand_path(
'templates', File.dirname(File.dirname(__FILE__)))
create_category(
- :path => 'index',
- :ext => :haml,
- :heading => 'Home',
- :content => File.read(File.join(template_path, 'index.haml'))
+ path: 'index',
+ ext: :haml,
+ heading: 'Home',
+ content: File.read(File.join(template_path, 'index.haml'))
)
create_category
end
-
+
after(:each) do
remove_temp_directory
Nesta::FileModel.purge_cache
end
def do_get
get "/"
end
-
+
describe "when categories exist" do
before(:each) do
@category = create_category
end
-
+
it_should_behave_like "page that can display menus"
end
-
+
it "should render successfully" do
do_get
last_response.should be_ok
end
-
+
it "should display site title in hgroup tag" do
- pending "Hpricot doesn't support HTML5"
- body.should have_tag('hgroup h1', /My blog/)
+ do_get
+ assert_selector 'hgroup h1', content: "My blog"
end
-
+
it "should display site subtitle in hgroup tag" do
- pending "Hpricot doesn't support HTML5"
do_get
- body.should have_tag('hgroup h2', /about stuff/)
+ assert_selector 'hgroup h2', content: "about stuff"
end
-
+
describe "when articles have no summary" do
before(:each) do
create_article
do_get
end
-
+
it "should display full content of article" do
- body.should have_tag("p", "Content goes here")
+ assert_selector "p", content: "Content goes here"
end
-
+
it "should not display read more link" do
- body.should_not have_tag("a", /continue/i)
+ last_response.should be_ok
+ assert_not_selector "a", content: 'continue'
end
end
describe "when articles have metadata" do
before(:each) do
@summary = 'Multiline\n\nsummary'
@read_more = "Continue at your leisure"
- @article = create_article(:metadata => {
+ @article = create_article(metadata: {
"summary" => @summary,
"read more" => @read_more
})
do_get
end
-
+
it "should display link to article in h2 tag" do
- body.should have_tag(
- "h1 a[@href$=#{@article.abspath}]", /^\s*#{@article.heading}$/)
+ link_text = @article.link_text
+ href = @article.abspath
+ assert_selector "h1 a[@href$='#{href}']:contains('#{link_text}')"
end
-
+
it "should display article summary if available" do
- body.should have_tag('p', @summary.split('\n\n').first)
+ assert_selector 'p', content: @summary.split('\n\n').first
end
-
+
it "should display read more link" do
- body.should have_tag("a[@href$=#{@article.abspath}]", @read_more)
+ assert_selector "a[@href$='#{@article.abspath}']", content: @read_more
end
end
end
describe "An article" do
include ModelFactory
include RequestSpecHelper
-
+
before(:each) do
stub_configuration
@date = '07 September 2009'
@keywords = 'things, stuff'
@description = 'Page about stuff'
@summary = 'Multiline\n\nsummary'
- @article = create_article(:metadata => {
+ @link_text = 'Link to page about stuff'
+ @article = create_article(metadata: {
'date' => @date.gsub('September', 'Sep'),
'description' => @description,
'keywords' => @keywords,
- 'summary' => @summary
+ 'summary' => @summary,
+ 'link text' => @link_text
})
end
-
+
after(:each) do
remove_temp_directory
Nesta::FileModel.purge_cache
end
-
+
def do_get
get @article.abspath
end
it_should_behave_like "page with keyword and description"
describe "when categories exist" do
before(:each) do
@category = create_category
end
-
+
it_should_behave_like "page that can display menus"
end
it "should render successfully" do
do_get
last_response.should be_ok
end
it "should display the heading" do
do_get
- body.should have_tag('h1', 'My article')
+ assert_selector 'h1', content: 'My article'
end
- it "should use heading for title tag" do
+ it "should use link text for title tag" do
do_get
- body.should have_tag('title', 'My article - My blog')
+ assert_selector 'title', content: @link_text
end
it "should display the date" do
do_get
- body.should have_tag('time', @date)
+ assert_selector 'time', content: @date
end
it "should display the content" do
do_get
- body.should have_tag('p', 'Content goes here')
+ assert_selector 'p', content: 'Content goes here'
end
-
+
describe "that is assigned to categories" do
before(:each) do
- create_category(:heading => 'Apple', :path => 'the-apple')
- @category = create_category(:heading => 'Banana', :path => 'banana')
+ create_category(heading: 'Apple', path: 'the-apple')
+ @category = create_category(heading: 'Banana', path: 'banana')
@article = create_article(
- :path => "#{@category.path}/article",
- :metadata => { 'categories' => 'banana, the-apple' }
+ path: "#{@category.path}/article",
+ metadata: { 'categories' => 'banana, the-apple' }
)
end
-
+
it "should render successfully" do
do_get
last_response.should be_ok
end
-
+
it "should link to each category" do
- pending "Hpricot doesn't support HTML5"
do_get
- body.should have_tag("nav.categories") do |categories|
- categories.should have_tag("a[@href=/banana]", "Banana")
- categories.should have_tag("a[@href=/the-apple]", "Apple")
- end
+ assert_selector "p.meta a[href='/banana']", content: "Banana"
+ assert_selector "p.meta a[href='/the-apple']", content: "Apple"
end
it "should link to a category in breadcrumb" do
- pending "Hpricot doesn't support HTML5"
do_get
- body.should have_tag(
- "nav.breadcrumb/a[@href=#{@category.abspath}]", @category.heading)
+ href = @category.abspath
+ link_text = @category.link_text
+ assert_selector "nav.breadcrumb a[href='#{href}']", content: link_text
end
-
- it "should contain category name in page title" do
+
+ it "should not include Disqus comments by default" do
do_get
- body.should_not have_tag("title", /My blog/)
- body.should have_tag("title", /- #{@category.heading}$/)
+ last_response.should be_ok
+ assert_not_selector '#disqus_thread'
end
end
+
+ describe "that is configured to show Disqus comments" do
+ before(:each) do
+ stub_config_key("disqus_short_name", "mysite")
+ @category = create_category
+ end
+
+ it "should display Disqus comments" do
+ do_get
+ assert_selector '#disqus_thread'
+ assert_selector 'script[@src*="mysite.disqus.com/embed.js"]'
+ end
+ end
end
describe "A page" do
include ModelFactory
include RequestSpecHelper
-
+
before(:each) do
stub_configuration
end
after(:each) do
remove_temp_directory
Nesta::FileModel.purge_cache
end
-
+
def do_get
get @category.abspath
end
describe "that doesn't exist" do
it "should render the 404 page" do
get "/no-such-page"
- last_response.should_not be_ok
+ last_response.should be_not_found
end
end
describe "that has meta data" do
before(:each) do
@@ -295,12 +356,12 @@
@content = "Page content"
@description = "Page about stuff"
@keywords = "things, stuff"
@articles_heading = "Posts about this stuff"
@category = create_category(
- :content => "# My category\n\n#{@content}",
- :metadata => {
+ content: "# My category\n\n#{@content}",
+ metadata: {
'title' => @title,
'description' => @description,
'keywords' => @keywords,
'articles heading' => @articles_heading
}
@@ -322,91 +383,75 @@
last_response.should be_ok
end
it "should display the heading" do
do_get
- body.should have_tag('h1', @category.heading)
+ assert_selector 'h1', content: @category.heading
end
it "should use title metadata to set heading" do
do_get
- body.should have_tag('title', @title)
+ assert_selector 'title', content: @title
end
it "should display the content" do
do_get
- body.should have_tag("p", @content)
+ assert_selector "p", content: @content
end
describe "with associated pages" do
before(:each) do
@category1 = create_category(
- :path => 'category1',
- :heading => 'Category 1',
- :metadata => {
+ path: 'category1',
+ heading: 'Category 1',
+ metadata: {
'categories' => 'category-prefix/my-category:-1'
}
)
@category2 = create_category(
- :path => 'category2',
- :heading => 'Category 2',
- :metadata => {
+ path: 'category2',
+ heading: 'Category 2',
+ metadata: {
'categories' => 'category-prefix/my-category:1'
}
)
end
it "should list highest priority pages at the top" do
do_get
- body.should have_tag('li:nth-child(1) h1 a', 'Category 2')
- body.should have_tag('li:nth-child(2) h1 a', 'Category 1')
+ assert_selector 'li:nth-child(1) h1 a', content: 'Category 2'
+ assert_selector 'li:nth-child(2) h1 a', content: 'Category 1'
end
end
describe "with associated articles" do
before(:each) do
@article = create_article(
- :path => "another-page",
- :heading => "Categorised",
- :metadata => { :categories => @category.path },
- :content => "Article content"
+ path: "another-page",
+ heading: "Categorised",
+ metadata: { categories: @category.path,
+ 'link text' => 'Categorised link'},
+ content: "Article content"
)
@article2 = create_article(
- :path => "second-article", :heading => "Second article")
+ path: "second-article", heading: "Second article")
end
it "should display links to articles" do
do_get
- body.should have_tag(
- "h1 a[@href$='#{@article.abspath}']", /^\s*#{@article.heading}$/)
- body.should_not have_tag("h3", @article2.heading)
+ href = @article.abspath
+ link_text = @article.link_text
+ assert_selector "h1 a[@href$='#{href}']", content: link_text
+ assert_not_selector "h3", content: @article2.link_text
end
it "should display the article heading" do
do_get
- body.should have_tag('h1', @articles_heading)
+ assert_selector 'h1', content: @articles_heading
end
end
-
- it "should not include Disqus comments by default" do
- do_get
- body.should_not have_tag('#disqus_thread')
- end
end
-
- describe "that is configured to show Disqus comments" do
- before(:each) do
- stub_config_key("disqus_short_name", "mysite")
- @category = create_category
- end
-
- it "should display Disqus comments" do
- do_get
- body.should have_tag('#disqus_thread')
- body.should have_tag('script[@src*="mysite.disqus.com/embed.js"]')
- end
- end
end
describe "A Haml page" do
include ModelFactory
include RequestSpecHelper
@@ -420,33 +465,34 @@
Nesta::FileModel.purge_cache
end
it "should be able to access helper methods" do
create_page(
- :path => "a-page",
- :ext => :haml,
- :content => "%div= format_date(Date.new(2010, 11, 23))"
+ path: "a-page",
+ ext: :haml,
+ content: "%div= format_date(Date.new(2010, 11, 23))",
+ heading: "A Page"
)
get "/a-page"
- body.should have_tag("div", "23 November 2010")
+ assert_selector "div", content: "23 November 2010"
end
it "should access helpers when rendering articles on a category page" do
category = create_page(
- :path => "a-page",
- :heading => "First heading",
- :content => "Blah blah"
+ path: "a-page",
+ heading: "First heading",
+ content: "Blah blah"
)
create_article(
- :path => "an-article",
- :ext => :haml,
- :heading => "First heading",
- :metadata => { :categories => category.path },
- :content => "%h1 Second heading\n\n%div= format_date(Date.new(2010, 11, 23))"
+ path: "an-article",
+ ext: :haml,
+ heading: "First heading",
+ metadata: { categories: category.path },
+ content: "%h1 Second heading\n\n%div= format_date(Date.new(2010, 11, 23))"
)
get "/a-page"
- body.should have_tag("div", "23 November 2010")
+ assert_selector "div", content: "23 November 2010"
end
end
describe "attachments" do
include ModelFactory
@@ -459,26 +505,26 @@
after(:each) do
remove_temp_directory
Nesta::FileModel.purge_cache
end
-
+
describe "in the attachments folder" do
before(:each) do
path = File.join(Nesta::Config.attachment_path, 'test.txt')
File.open(path, 'w') { |file| file.write("I'm a test attachment") }
end
-
+
it "should be served successfully" do
get "/attachments/test.txt"
last_response.should be_ok
end
-
+
it "should be sent to the client" do
get "/attachments/test.txt"
body.should include("I'm a test attachment")
end
-
+
it "should set the appropriate MIME type" do
get "/attachments/test.txt"
last_response.headers["Content-Type"].should =~ Regexp.new("^text/plain")
end
end