Permalink
Browse files

Render articles on the index page

  • Loading branch information...
avdgaag committed Mar 24, 2015
1 parent 7cd9b21 commit bff69a67dbda82da510fa2af747f44046f034ead
@@ -2,7 +2,14 @@ module Web::Controllers::Home
class Index
include Web::Action
expose :articles
def initialize(repository: Demo::ArticleRepository)
@repository = repository
end
def call(params)
@articles = @repository.all
end
end
end
@@ -1,3 +1,17 @@
<h2>All articles</h2>
<p class="placeholder">There are no articles yet.</p>
<% if articles.any? %>
<% articles.each do |article| %>
<div class="article">
<h3 class="article__title"><%= article.title %></h3>
<div clas="article__body">
<%= article.body %>
</div>
<footer class="article__footer">
<p>Posted on <%= article.created_at %></p>
</footer>
</div>
<% end %>
<% else %>
<p class="placeholder">There are no articles yet.</p>
<% end %>
@@ -0,0 +1,19 @@
require_relative '../../../spec_helper'
module Web::Controllers::Home
describe Index do
let(:repository) { OpenStruct.new(all: []) }
let(:action) { Web::Controllers::Home::Index.new(repository: repository) }
let(:params) { Hash[] }
it 'is successful' do
response = action.call(params)
assert_equal 200, response[0], 'expected HTTP status 200'
end
it 'exposes articles to the view' do
action.call({})
assert_same repository.all, action.exposures[:articles], 'expected exposure to be result of repository.all'
end
end
end
@@ -1,13 +1,39 @@
require_relative '../../features_helper'
describe 'Homepage' do
before do
Demo::ArticleRepository.clear
end
it 'links to the home page' do
visit '/'
assert page.has_link?('a', href: '/')
end
it 'shows a placeholder when there are no articles yet' do
visit '/'
assert page.has_css?('.placeholder', text: 'There are no articles yet.')
describe 'when there are no articles' do
it 'shows a placeholder' do
visit '/'
assert page.has_css?('.placeholder', text: 'There are no articles yet.')
end
end
describe 'when there are articles' do
before do
3.times do |i|
Demo::ArticleRepository.create(
Demo::Article.new(title: "Article #{i}", body: 'Lorem ipsum')
)
end
end
it 'hides the placeholder' do
visit '/'
assert page.has_no_css?('.placeholder'), 'there should be no placeholder'
end
it 'shows all articles on the page' do
visit '/'
assert page.has_css?('.article', count: 3), 'there should be a .article element for every article'
end
end
end
@@ -0,0 +1,13 @@
require_relative '../../../spec_helper'
module Web::Views::Home
describe Index do
let(:exposures) { Hash[articles: []] }
let(:template) { Lotus::View::Template.new('apps/web/templates/home/index.html.erb') }
let(:view) { Web::Views::Home::Index.new(template, exposures) }
it 'exposes #articles' do
assert_same exposures[:articles], view.articles, 'articles should have been exposed'
end
end
end

0 comments on commit bff69a6

Please sign in to comment.