Skip to content

Commit

Permalink
Merge pull request #2440 from kuei0221/fix/js_animation_not_disable_i…
Browse files Browse the repository at this point in the history
…f_load_jquery_in_body

Fix js animation not disable if load jquery in body
  • Loading branch information
twalpole authored Jan 23, 2021
2 parents a08e047 + f95e581 commit 958a179
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/capybara/server/animation_disabler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def html_content?
end

def insert_disable(html)
html.sub(%r{(</head>)}, "#{disable_markup}\\1")
html.sub(%r{(</body>)}, "#{disable_markup}\\1")
end

DISABLE_MARKUP_TEMPLATE = <<~HTML
<script defer="defer">
<script>
//<![CDATA[
(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);
//]]>
Expand Down
24 changes: 24 additions & 0 deletions lib/capybara/spec/views/with_jquery_animation.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>with_jquery_animation</title>
<style>
body {
height: 2000px;
}
</style>
</head>

<body id="with_animation">
<a href="#" id='scroll'>scroll top 500</a>

<script src="/jquery.js" type="text/javascript" charset="utf-8"></script>
<script type='text/javascript'>
$('#scroll').click(function(e){
e.preventDefault();
$('html, body').animate({ scrollTop: 500 }, 'slow');
});
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions spec/shared_selenium_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@
JS
expect(scroll_y).to eq 500
end

it 'should scroll the page instantly without jquery animation', requires: [:js] do
@animation_session.visit('with_jquery_animation')
@animation_session.click_link('scroll top 500')
scroll_y = @animation_session.evaluate_script('window.scrollY')
expect(scroll_y).to eq 500
end
end

context 'when set to `false`' do
Expand All @@ -433,6 +440,13 @@
# measured over 0.5 seconds: 0, 75, 282, 478, 500
expect(scroll_y).to be < 500
end

it 'should scroll the page with jquery animation', requires: [:js] do
@animation_session.visit('with_jquery_animation')
@animation_session.click_link('scroll top 500')
scroll_y = @animation_session.evaluate_script('window.scrollY')
expect(scroll_y).to be < 500
end
end

context 'if we pass in css that matches elements' do
Expand Down

0 comments on commit 958a179

Please sign in to comment.