Skip to content

Commit

Permalink
Extract state selector to cell. [#47]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Apr 12, 2020
1 parent 198fd67 commit c41273a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 20 deletions.
7 changes: 7 additions & 0 deletions app/cells/state_selector/show.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= form_with url: url, local: true, class: 'state-selector' do |form|
%p
= label_tag :states, _('Choose one or more states:')
- locale_state_options = states_for_menu.sort_by {|state| _(state.name)}.map {|state| [_(state.name), state.abbr]}
= select_tag :states, options_for_select(locale_state_options, @states&.map(&:abbr)), multiple: true
%p
= submit_tag _('Go')
22 changes: 22 additions & 0 deletions app/cells/state_selector_cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class StateSelectorCell < Cell::ViewModel
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormOptionsHelper

attr_reader :states, :url

def initialize(states, options)
super
@states = states
@url = options[:url]
end

def show
render
end

private

def states_for_menu
@states_for_menu ||= State.all
end
end
3 changes: 0 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
module ApplicationHelper
def states
State.all
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%h1
= link_to '/' do
= h(_ '%{span}COVID-19%{_span} Tracking Charts by U.S. State') % {span: '<span class="accent">'.html_safe, _span: '</span>'.html_safe}
%nav= render 'shared/state_selector', states: states # TODO: consider Cells or something here
%nav= cell(:state_selector, @states, url: choose_states_path)

%main
- if content_for :page_title
Expand Down
7 changes: 0 additions & 7 deletions app/views/shared/_state_selector.html.haml

This file was deleted.

71 changes: 71 additions & 0 deletions spec/cells/state_selector_cell_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'rails_helper'

RSpec.describe StateSelectorCell, type: :cell do
let(:states) { [] }
let(:url) { nil }
let(:options) { {url: url} }

subject { described_class.new states, options }

context 'constructor' do
it { is_expected.to be_a_kind_of Cell::ViewModel }

it 'includes form helpers' do
['FormHelper', 'FormOptionsHelper'].each do |helper|
expect(subject).to be_a_kind_of "ActionView::Helpers::#{helper}".constantize
end
end
end

context '#states' do
let(:states) { State.all.sample rand(2..5) }

it 'returns the given states' do
expect(subject.states).to be == states
end
end

context '#url' do
let(:url) { File.join *Faker::Lorem.words(rand 2..5) }

it 'returns the given URL' do
expect(subject.url).to be == url
end
end

context '#show' do
controller Class.new(ApplicationController) # see https://github.com/trailblazer/rspec-cells#url-helpers

let(:url) { File.join *Faker::Lorem.words(rand 2..5) }
let(:menu) { 'select[multiple][name="states[]"]' }

subject { Capybara.string cell(described_class, states, options).call }

it 'renders a form that will submit to the given URL' do
expect(subject).to have_selector "form[action='#{url}']"
end

it 'renders a multi-select with all the states as options, sorted by nam,e' do
subject.find menu do |menu|
selector = './/' + State.all.sort_by(&:name).map {|state| "option[@value='#{state.abbr}'][text()='#{state.name}']"}.join('/following-sibling::')
expect(menu).to have_xpath selector
end
end

context 'states given' do
let(:states) { State.all.sample rand(2..5) }

it 'marks all the states given as selected' do
subject.find(menu) do |menu|
expect(menu.all('option[selected]').map &:text).to match_array states.map(&:name)
end
end
end

context 'no states given' do
it 'marks no states as selected' do
expect(subject.find menu).not_to have_css 'option[selected]'
end
end
end
end
9 changes: 0 additions & 9 deletions spec/helpers/application_helper_spec.rb

This file was deleted.

0 comments on commit c41273a

Please sign in to comment.