require 'rubygems'
require 'gorp/test'

class RjsRenderTest < Gorp::TestCase

  test "ability to use render with a partial in rjs" do

    desc 'Create a simple test application; run the provided functional tests.'
    rails 'testapp'
    ruby 'script/generate scaffold product title:string'
    rake 'db:migrate'
    rake 'test:functionals' do
      assert_select '.stdout', /7 tests, 10 assertions, 0 failures, 0 errors/
    end

    desc 'Create a test using XHR'
    edit 'test/functional/products_controller_test.rb' do
      msub /^()end/, "\n" + <<-EOF.unindent(6)
        test "should create line_item via XHR" do
          assert_difference('Product.count', +1) do
            xhr :post, :create, :product_title => 'Lorem Ipsum'
          end
        end
      EOF
    end

    desc 'Rerun the tests'
    rake 'test:functionals' do
      assert_select '.stdout', /8 tests, 11 assertions, 0 failures, 0 errors/
    end

    desc 'Add js to the response formats for create'
    edit 'app/controllers/products_controller.rb' do
      dcl 'create' do
        msub /^()\s+format.xml/, <<-EOF.unindent(2)
          format.js
        EOF
      end
    end

    desc 'Create a rjs file with a call to replace_html with a dummy id'
    edit 'app/views/products/create.js.rjs' do
      self.all = <<-EOF.unindent(8)
        page.replace_html('id', render(@product))
      EOF
    end

    desc 'create an empty partial'
    edit 'app/views/products/_product.html.erb' do
      self.all = <<-EOF.unindent(8)
        <!-- this page intentionally left blank -->
      EOF
    end

    desc 'Rerun functional tests'
    rake 'test:functionals' do
      assert_select '.stdout', /8 tests, 11 assertions, 0 failures, 0 errors/
    end

  end
end
