Skip to content

Commit

Permalink
[ci] Add test for flash_content helper
Browse files Browse the repository at this point in the history
We added test for flash_content helper and also included Haml::Helpers.
  • Loading branch information
David Kang committed Oct 18, 2017
1 parent 3faeb2e commit 3c23ca4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
20 changes: 20 additions & 0 deletions src/api/app/helpers/webui/flash_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Webui::FlashHelper
def flash_content(flash)
if flash.is_a?(Hash)
capture_haml do
haml_tag(:span, flash.delete(:title))
haml_tag :ul do
flash.each do |name, messages|
haml_tag(:li, name, class: 'no-bullet')
haml_tag :ul do
messages.each { |message| haml_tag(:li, message) }
end
end
end
end
else
body = flash.gsub(/\\n/, '')
sanitize body, tags: %w(a b ul li br u), attributes: %w(href title)
end
end
end
19 changes: 0 additions & 19 deletions src/api/app/helpers/webui/webui_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,23 +416,4 @@ def toggle_sliced_text(text, slice_length = 50, id = "toggle_sliced_text_#{Time.
end
short + long
end

def flash_content(flash)
if flash.is_a?(Hash)
capture_haml do
haml_tag(:span, flash.delete(:title))
haml_tag :ul do
flash.each do |name, messages|
haml_tag(:li, name, class: 'no-bullet')
haml_tag :ul do
messages.each { |message| haml_tag(:li, message) }
end
end
end
end
else
body = flash.gsub(/\\n/, '')
sanitize body, tags: %w(a b ul li br u), attributes: %w(href title)
end
end
end
40 changes: 40 additions & 0 deletions src/api/spec/helpers/webui/flash_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rails_helper'

RSpec.describe Webui::FlashHelper do
describe '#flash_content' do
let(:flash_with_hash) do
{
title: 'example',
'error 1' => ['error 1 content', 'error 1 content 2'],
'error 2' => ['error 2 content', 'error 2 content 2']
}
end

let(:result) do
<<-EOF.strip_heredoc
<span>example</span>
<ul>
<li class='no-bullet'>error 1</li>
<ul>
<li>error 1 content</li>
<li>error 1 content 2</li>
</ul>
<li class='no-bullet'>error 2</li>
<ul>
<li>error 2 content</li>
<li>error 2 content 2</li>
</ul>
</ul>
EOF
end

let(:flash) { "example: \\n<i>error 1</i> <b>content</b>" }

before do
init_haml_helpers
end

it { expect(flash_content(flash_with_hash)).to eq(result) }
it { expect(flash_content(flash)).to eq('example: error 1 <b>content</b>') }
end
end
2 changes: 2 additions & 0 deletions src/api/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
config.include Haml::Helpers

# load ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

Expand Down

0 comments on commit 3c23ca4

Please sign in to comment.