Skip to content

Commit

Permalink
Wait for script[type=module]
Browse files Browse the repository at this point in the history
  • Loading branch information
triskweline committed Feb 12, 2024
1 parent bbc17ac commit 84695c6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
7 changes: 1 addition & 6 deletions lib/capybara-lockstep/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ window.CapybaraLockstep = (function() {
}

function isRemoteScript(element) {
if (element.tagName === 'SCRIPT') {
let src = element.getAttribute('src')
let type = element.getAttribute('type')

return src && (!type || /javascript/i.test(type))
}
return element.matches('script[src]') && !hasLocalSource(element)
}

function isRemoteImage(element) {
Expand Down
88 changes: 74 additions & 14 deletions spec/features/synchronization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,26 +255,86 @@

end

it 'waits until a dynamically inserted script has loaded' do
App.start_html = <<~HTML
<a href="#" onclick="script = document.createElement('script'); script.src = '/next'; document.body.append(script)">label</a>
HTML
describe 'dynamically loaded scripts' do

wall = Wall.new
App.next_action = -> do
wall.block
content_type 'text/javascript'
'document.body.style.backgroundColor = "blue"'
it 'waits until a <script> has loaded' do
App.start_html = <<~HTML
<a href="#" onclick="
let script = document.createElement('script');
script.src = '/next';
document.body.append(script);
">label</a>
HTML

wall = Wall.new
App.next_action = -> do
wall.block
content_type 'text/javascript'
'document.body.style.backgroundColor = "blue"'
end

visit '/start'

command = ObservableCommand.new { page.find('a').click }
expect(command).to run_into_wall(wall)

wall.release

wait(0.5.seconds).for(command).to be_finished
end

visit '/start'
it 'waits until a <script type="module"> has loaded' do
App.start_html = <<~HTML
<a href="#" onclick="
let script = document.createElement('script');
script.type = 'module';
script.src = '/next';
document.body.append(script);
">label</a>
HTML

command = ObservableCommand.new { page.find('a').click }
expect(command).to run_into_wall(wall)
wall = Wall.new
App.next_action = -> do
wall.block
content_type 'text/javascript'
'document.body.style.backgroundColor = "blue"'
end

wall.release
visit '/start'

command = ObservableCommand.new { page.find('a').click }
expect(command).to run_into_wall(wall)

wall.release

wait(0.5.seconds).for(command).to be_finished
end

it 'does not wait forever for an inline script' do
App.start_html = <<~HTML
<a href="#" onclick="
let script = document.createElement('script');
script.innerText = 'window.EFFECT = 123';
document.body.append(script);
">label</a>
HTML

wall = Wall.new
App.next_action = -> do
wall.block
content_type 'text/javascript'
'document.body.style.backgroundColor = "blue"'
end

visit '/start'

command = ObservableCommand.new { page.find('a').click }
command.execute
wait(0.1.seconds).for(command).to be_finished

expect(evaluate_script('EFFECT')).to eq(123)
end

wait(0.5.seconds).for(command).to be_finished
end

it 'does not close an alert that was opened on click' do
Expand Down

0 comments on commit 84695c6

Please sign in to comment.