Skip to content

Commit

Permalink
the remote url is now updated on any history method invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mann committed May 6, 2015
1 parent fcfac0e commit fb14967
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/js/api/cypress/cy/commands/navigation.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
$Cypress.register "Navigation", (Cypress, _, $) ->

overrideRemoteLocationGetters = (cy, contentWindow) ->
navigated = ->
cy.$remoteIframe.trigger("history:event")

location = (attr) ->
cy.sync.location(attr, {log: false})

Cypress.Location.override(contentWindow, location)
Cypress.Location.override(contentWindow, location, navigated)

Cypress.Cy.extend
onBeforeLoad: (contentWindow) ->
Expand Down
15 changes: 14 additions & 1 deletion app/js/api/cypress/location.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,20 @@ $Cypress.Location = do ($Cypress, _, Uri) ->
}

## override pathname + query here
@override = (win, location) ->
@override = (win, location, navigated) ->
## history does not fire events until a click or back has happened
## so we have to know when they do manually
_.each ["back", "forward", "go", "pushState", "replaceState"], (attr) ->
## dont use underscore wrap here because its potentially very
## confusing for users and this manually override is faster anyway
return if not orig = win.history?[attr]

win.history[attr] = ->
orig.apply(@, arguments)

## let our function know we've navigated
navigated()

_.each ["hash", "host", "hostname", "origin", "pathname", "port", "protocol", "search"], (attr) ->
try
Object.defineProperty win.location, attr, {
Expand Down
4 changes: 4 additions & 0 deletions app/js/backbone/apps/test_iframe/show/show_view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@

@$remote = $("<iframe />", remoteOpts).appendTo(@ui.size)

## when apps go through the history object
## and change the url, update it now
@$remote.on "history:event", @updateRemoteUrl

## when the remote iframe visits an external URL
## we want to update our header's input
@$remote.on "visit:start", (e, url) =>
Expand Down
16 changes: 16 additions & 0 deletions spec/client/api/cy/navigation_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe "$Cypress.Cy Navigation Commands", ->
it "search", ->
@eq "search", "?foo=bar"

describe "history method overrides", ->
beforeEach ->
@cy
.visit("fixtures/html/sinon.html")
.window().as("win")
.then ->
@trigger = @sandbox.stub @cy.$remoteIframe, "trigger"

_.each ["back", "forward", "go", "pushState", "replaceState"], (attr) =>
it "fires 'history:event' on attr: '#{attr}'", ->
if attr is "go"
arg = -1

@win.history[attr](arg)
expect(@trigger).to.be.calledWith "history:event"

describe "visit:start", ->
beforeEach ->
trigger = @sandbox.stub $.fn, "trigger"
Expand Down

0 comments on commit fb14967

Please sign in to comment.