Skip to content

Instantly share code, notes, and snippets.

@cyrilchampier
Created February 28, 2018 17:09
Show Gist options
  • Save cyrilchampier/fafec1a78959e9f69e71e2c546f54191 to your computer and use it in GitHub Desktop.
Save cyrilchampier/fafec1a78959e9f69e71e2c546f54191 to your computer and use it in GitHub Desktop.
# Disable CSS3 and jQuery animations in test mode for speed, consistency and avoiding timing issues.
# Usage for Rails:
# in config/environments/test.rb
# config.middleware.use Rack::NoAnimations
class Build::Middleware::NoAnimations
DISABLE_ANIMATIONS_HTML = <<~HTML
<script type="text/javascript">
if (typeof window.jQuery !== 'undefined') {
window.jQuery(() => {
window.jQuery.support.transition = false;
if (typeof window.jQuery.fx !== 'undefined') {
window.jQuery.fx.off = true;
}
});
}
</script>
<style>
* {
-webkit-transition: .0s !important;
transition: .0s !important;
-webkit-transform: .0s !important;
transform: .0s !important;
-webkit-animation: .0s !important;
animation: .0s !important;
}
</style>
HTML
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
return status, headers, body unless html?(headers)
response = Rack::Response.new([], status, headers)
body.each { |fragment| response.write inject(fragment) }
body.close if body.respond_to?(:close)
response.finish
end
private
def html?(headers)
headers['Content-Type'] =~ /html/
end
def inject(fragment)
fragment.gsub('</body>', DISABLE_ANIMATIONS_HTML + '</body>')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment