Skip to content

Instantly share code, notes, and snippets.

@cyrilchampier
Created December 11, 2017 16:21
Show Gist options
  • Save cyrilchampier/a9fae7aa5d38559ec81194a9347e1c9f to your computer and use it in GitHub Desktop.
Save cyrilchampier/a9fae7aa5d38559ec81194a9347e1c9f to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# 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
def initialize(app)
@app = app
end
def call(env)
@status, @headers, @body = @app.call(env)
return [@status, @headers, @body] unless html?
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['Content-Type'] =~ /html/
end
def inject(fragment)
disable_animations = <<~EOF
<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>
* {
-o-transition: .0s !important;
-moz-transition: .0s !important;
-ms-transition: .0s !important;
-webkit-transition: .0s !important;
transition: .0s !important;
-o-transform: .0s !important;
-moz-transform: .0s !important;
-ms-transform: .0s !important;
-webkit-transform: .0s !important;
transform: .0s !important;
-webkit-animation: .0s !important;
-moz-animation: .0s !important;
-o-animation: .0s !important;
-ms-animation: .0s !important;
animation: .0s !important;
}
</style>
EOF
fragment.gsub('</body>', disable_animations + '</body>')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment