diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 19c3464..2ad1126 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,22 +4,22 @@ :user end - # ------------------------------------------------- + # --------------------------------------------------------------------------- def resource @resource ||= User.new end - # ------------------------------------------------- + # --------------------------------------------------------------------------- def devise_mapping @devise_mapping ||= Devise.mappings[:user] end - # ------------------------------------------------- + # --------------------------------------------------------------------------- def javascript(*files) content_for(:head) { javascript_include_tag(*files) } end - # ------------------------------------------------- + # --------------------------------------------------------------------------- def link_to_add_object(name, f, association, css_class, i) new_object = f.object.class.reflect_on_association(association).klass.new fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| @@ -30,7 +30,7 @@ link_to_function(name, "add_object(this, \"#{association}\", \"#{escape_javascript(fields)}\")", :class => css_class) end - # ------------------------------------------------- + # --------------------------------------------------------------------------- def link_to_function(name, *args, &block) html_options = args.extract_options!.symbolize_keys @@ -41,6 +41,7 @@ content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick)) end + # --------------------------------------------------------------------------- def hash_to_json_object(obj_name, hash) "".html_safe end diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index 1f128ba..5298b43 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -2,24 +2,52 @@ class ApplicationHelperTest < ActionView::TestCase + def setup + # initialize the ActionView Output so that we have access to its functions (e.g. content_for) + @view_flow = ActionView::OutputFlow.new + + content_for(:head) do + "Testing".html_safe + end + end + # ----------------------------------------------------------------------- test "resource_name should return :user" do - + assert_equal :user, resource_name end # ----------------------------------------------------------------------- test "resource should return contents of instance variable @resource OR a new User" do + # If @resource is not set then we should receive a new User + assert resource.is_a?(User), "Expected resource() to return a new User" + assert_equal nil, resource.id, "Expected resource() to return a User with an Id" + # If @resource is set then we should receive that object + @resource = Organisation.first + assert resource.is_a?(Organisation), "Expected resource() to return @resource" + assert_equal @resource.id, resource.id, "Expected resource() to return the first Organisation" end # ----------------------------------------------------------------------- test "devise_mapping should return the mappings registered for Devise" do - + # If @devise_mappings is not set we should get the mappings for :user + assert_equal Devise.mappings[:user], devise_mapping, "Expected devise_mapping() to return the correct default" + + # If @devise_mapping is set the we should receive it + @devise_mapping = {foo: 'bar'} + assert_equal @devise_mapping, devise_mapping, "Expected devise_mapping() to return @devise_mapping" end # ----------------------------------------------------------------------- - test "javascript should return a list of the javascript files used in the application" do + test "javascript should add JS script tags to the HTML head content for each of the files provided" do + current = content_for(:head) + javascript('abc.js') + assert content_for(:head).include?("#{javascript_include_tag('abc.js')}"), "Expected the abc.js script to be added to the HTML head content but got: #{content_for(:head)}" + + javascript('zyx.js', 'wvu.js') + assert content_for(:head).include?("#{javascript_include_tag('wvu.js')}"), "Expected the wvu.js script to be added to the HTML head content but got: #{content_for(:head)}" + assert content_for(:head).include?("#{javascript_include_tag('zyx.js')}"), "Expected the zyx.js script to be added to the HTML head content but got: #{content_for(:head)}" end # -----------------------------------------------------------------------