Skip to content

Commit

Permalink
Test the helper
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeclaghorn committed Aug 29, 2018
1 parent 1c3e902 commit c0a75ab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
7 changes: 7 additions & 0 deletions app/helpers/google_sign_in/button_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GoogleSignIn::ButtonHelper
def google_sign_in_button(text = nil, proceed_to:, **options, &block)
form_with url: google_sign_in.authorization_path do
hidden_field_tag(:proceed_to, proceed_to, id: nil) + button_tag(text, name: nil, **options, &block)
end
end
end
5 changes: 2 additions & 3 deletions lib/google_sign_in/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class Engine < ::Rails::Engine
end
end

initializer 'google_sign_in.helper' do
initializer 'google_sign_in.helpers' do
ActiveSupport.on_load :action_controller do
require 'google_sign_in/helper'
ActionController::Base.helper GoogleSignIn::Helper
ActionController::Base.helper GoogleSignIn::Engine.helpers
end
end

Expand Down
9 changes: 0 additions & 9 deletions lib/google_sign_in/helper.rb

This file was deleted.

23 changes: 23 additions & 0 deletions test/helpers/button_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class GoogleSignIn::ButtonHelperTest < ActionView::TestCase
test "generating a login button with text content" do
assert_dom_equal <<-HTML, google_sign_in_button("Log in with Google", proceed_to: 'https://www.example.com/login')
<form action="/google_sign_in/authorization" accept-charset="UTF-8" data-remote="true" method="post">
<input name="utf8" type="hidden" value="&#x2713;" />
<input name="proceed_to" type="hidden" value="https://www.example.com/login" />
<button type="submit">Log in with Google</button>
</form>
HTML
end

test "generating a login button with HTML content" do
assert_dom_equal <<-HTML, google_sign_in_button(proceed_to: 'https://www.example.com/login') { image_tag('google.png') }
<form action="/google_sign_in/authorization" accept-charset="UTF-8" data-remote="true" method="post">
<input name="utf8" type="hidden" value="&#x2713;" />
<input name="proceed_to" type="hidden" value="https://www.example.com/login" />
<button type="submit"><img src="/images/google.png"></button>
</form>
HTML
end
end
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@
require 'rails/test_help'
require 'webmock/minitest'
require 'byebug'

class ActionView::TestCase
private
def assert_dom_equal(expected, actual, message = nil)
super expected.remove(/(\A|\n)\s*/), actual, message
end
end

0 comments on commit c0a75ab

Please sign in to comment.