Created
May 12, 2011 19:25
-
-
Save kurisu/969252 to your computer and use it in GitHub Desktop.
Automatic DOM Fixture Generation in Jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<-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 Jasmine | |
class RunAdapter | |
alias_method :original_run, :run | |
def run(focused_suite = nil) | |
gen_fixtures_if_needed | |
original_run(focused_suite) | |
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 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 spec/**/*spec.rb -e fixture --drb') | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<-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: | |
it "should generate a fixture" do | |
get :index | |
save_fixture('#zip_code_input', 'widget_zip_code_input') | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UPDATE: Moved comments into README text in each file