Skip to content

Instantly share code, notes, and snippets.

@kurisu
Created May 12, 2011 19:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurisu/969252 to your computer and use it in GitHub Desktop.
Save kurisu/969252 to your computer and use it in GitHub Desktop.
Automatic DOM Fixture Generation in Jasmine
<<-README
Put this here /spec/javascripts/support/jasmine_extensions
The observed and output folders are set up for Rails, but there's no reason
you couldn't change them to work with any DOM-generating back-end.
Also, we found it helpful to clean the html we wrote to these fixtures by
remove script tags and changing the body to a div, so it fit nicely into the
jasmine_content div.
Notice that all specs with "fixture" in their descriptions will be run when
new DOM fixtures are needed for Jasmine.
README
<<-LICENSE
The MIT License
Copyright (c) 2011 TrueCar, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
LICENSE
module Rack::Jasmine
class Runner
alias_method :original_call, :call
def call(env)
gen_fixtures_if_needed
original_call(env)
end
def gen_fixtures_if_needed
view_mtimes = mtimes_for_dir(views_dir)
fixture_mtimes = mtimes_for_dir(fixtures_dir)
if fixture_mtimes.empty? || view_mtimes.max > fixture_mtimes.min
delete_fixtures
generate_fixtures
else
puts "Fixture views have not changed"
end
end
def mtimes_for_dir(dir)
Dir.glob(File.join(dir, "/**/*")).map { |file| File.mtime(file).to_i }
end
def root_dir
File.join(File.dirname(__FILE__), '../../../..')
end
def views_dir
File.join(root_dir, '/app/views')
end
def specs_dir
File.join(root_dir, '/spec')
end
def fixtures_dir
File.join(root_dir, '/spec/javascripts/fixtures')
end
def delete_fixtures
puts "Removing old fixtures"
File.delete(*Dir.glob(File.join(fixtures_dir, "**/*")))
end
def generate_fixtures
puts "Generating fixtures"
system("rspec #{Dir.glob(File.join(specs_dir, "**/*spec.rb")).join(" ")} -e fixture --drb")
end
end
end
<<-README
Put this in your /spec/javascripts/support folder for automatic generation to
happen as-needed with each jasmine request.
README
<<-LICENSE
The MIT License
Copyright (c) 2011 TrueCar, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
LICENSE
Dir["#{File.dirname(__FILE__)}/jasmine_extensions/*.rb"].each do |filename|
require filename
end
<<-README
We put this in Rails root /spec/support so we could use it in all of our jasmine fixture building specs.
Just mix it in when running specs. For example, in spec_helper.rb, you could do this:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|filename| require filename}
For example:
describe "home" do
render_views
it "should generate a fixture" do
get :index
save_fixture("body", "home")
end
end
README
<<-LICENSE
The MIT License
Copyright (c) 2011 TrueCar, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
LICENSE
module SaveFixture
def save_fixture(selector, name)
html = html_for(selector)
path = File.join(Rails.root, 'spec/javascripts/fixtures')
file_path = File.join(path, "#{name}.html")
File.open(file_path, 'w') do |f|
f.puts(html)
end
end
def html_for(selector='body')
doc = Nokogiri::HTML(fixture_html).css(selector)
clean_html(doc)
end
def clean_html(doc)
doc.search('script').try(:remove)
doc.search('iframe').try(:remove)
doc.search('img').each do |image|
image['src'] = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
end
doc.to_s.gsub('<body', '<div').gsub('</body', '</div')
end
end
# Mix save_fixture in for use by controller and view specs
require 'nokogiri'
module RSpec::Rails::ControllerExampleGroup
include SaveFixture
def fixture_html
response.body
end
end
module RSpec::Rails::ViewExampleGroup
include SaveFixture
def fixture_html
rendered
end
def doc
@doc ||= Nokogiri::HTML(rendered)
end
end
@kurisu
Copy link
Author

kurisu commented May 12, 2011

UPDATE: Moved comments into README text in each file

@kurisu
Copy link
Author

kurisu commented May 12, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment