Skip to content

Commit

Permalink
🛠️ redirects no longer override partialReplace: false to accomodate f…
Browse files Browse the repository at this point in the history
…or code which expects synchronous partial replaces (#179)
  • Loading branch information
Mallory Allen authored Sep 28, 2017
1 parent 50e0a49 commit b4c58d9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 41 deletions.
4 changes: 3 additions & 1 deletion dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
name: turbograft
up:
- ruby: 2.3.1
- ruby:
version: 2.3.3
rubygems: 2.6.13
- bundler
commands:
test: bundle exec rake test
Expand Down
12 changes: 3 additions & 9 deletions lib/assets/javascripts/turbograft/response.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class TurboGraft.Response
constructor: (@xhr, intendedURL) ->
if intendedURL && intendedURL.withoutHash() != @xhr.responseURL
@redirectedTo = @xhr.responseURL
redirectedTo = @xhr.responseURL
else
@redirectedTo = @xhr.getResponseHeader('X-XHR-Redirected-To')
redirectedTo = @xhr.getResponseHeader('X-XHR-Redirected-To')

@finalURL = @redirectedTo || intendedURL
@finalURL = redirectedTo || intendedURL

valid: -> @hasRenderableHttpStatus() && @hasValidContent()

Expand All @@ -23,12 +23,6 @@ class TurboGraft.Response
else
throw new Error("Error encountered for XHR Response: #{this}")

redirectedToNewUrl: () ->
Boolean(
@redirectedTo &&
@redirectedTo != TurboGraft.location()
)

toString: () ->
"URL: #{@xhr.responseURL}, " +
"ReadyState: #{@xhr.readyState}, " +
Expand Down
2 changes: 1 addition & 1 deletion lib/assets/javascripts/turbograft/turbolinks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class window.Turbolinks
options.partialReplace ||
options.onlyKeys?.length ||
options.exceptKeys?.length
) && !response.redirectedToNewUrl()
)

@fullPageNavigate: (url) ->
if url?
Expand Down
30 changes: 0 additions & 30 deletions test/javascripts/response_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,3 @@ describe 'TurboGraft.Response', ->
responseForFixture { fixture: 'serverError' }, (response) ->
assert.equal(response.document(), undefined)
done()

describe 'redirectedTo', ->
it 'returns the responseURL if intendedURL is present and responseURL is different from passed in url', ->
responseForFixture { fixture: 'noScriptsOrLinkInHead', intendedURL: 'test-url' }, (response) ->
assert.equal(response.redirectedTo, 'noScriptsOrLinkInHead')
done()

it 'returns the value of the X-XHR-Redirected-To header when present', (done) ->
responseForFixture { fixture: 'xhrRedirectedToHeader' }, (response) ->
assert.equal(response.redirectedTo, ROUTES['xhrRedirectedToHeader'][1]['X-XHR-Redirected-To'])
done()

describe 'redirectedToNewUrl', ->
beforeEach ->
sandbox.stub(TurboGraft, 'location', -> 'test-location')

it 'returns false when no redirect header is present', (done) ->
responseForFixture { fixture: 'noScriptsOrLnkInHead' }, (response) ->
assert(!response.redirectedToNewUrl(), 'response should report that it was redirected to a new url when it has no redirect header')
done()

it 'returns false when a redirect header is present but matches location.href', (done) ->
responseForFixture { fixture: 'xhrRedirectedToHeader' }, (response) ->
assert.equal(response.redirectedToNewUrl(), false)
done()

it 'returns true when a redirect header is present and does not match location.href', (done) ->
responseForFixture { fixture: 'otherXhrRedirectedToHeader' }, (response) ->
assert.equal(response.redirectedToNewUrl(), true)
done()
24 changes: 24 additions & 0 deletions test/javascripts/turbolinks_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ describe 'Turbolinks', ->
assert.calledOnce(resetScrollStub)
done()

describe 'loadPage', () ->
PAGE_CONTENT = 'Oh hi, mark!'

it 'is synchronous when a partial replace', ->
startFromFixture('noScriptsOrLinkInHead')
xhr = new sinon.FakeXMLHttpRequest()
xhr.open('POST', '/my/endpoint', true)
xhr.respond(200, {'Content-Type':'text/html'}, PAGE_CONTENT)

Turbolinks.loadPage(null, xhr, {partialReplace: true})

assert.include(testDocument.body.textContent, PAGE_CONTENT)

it 'is asynchronous when not a partial replace', (done) ->
startFromFixture('noScriptsOrLinkInHead')
xhr = new sinon.FakeXMLHttpRequest()
xhr.open('POST', '/my/endpoint', true)
xhr.respond(200, {'Content-Type':'text/html'}, PAGE_CONTENT)

Turbolinks.loadPage(null, xhr).then () ->
assert.include(testDocument.body.textContent, PAGE_CONTENT)
done()
assert.notInclude(testDocument.body.textContent, PAGE_CONTENT)

describe 'head asset tracking', ->
it 'refreshes page when moving from a page with tracked assets to a page with none', (done) ->
startFromFixture('singleScriptInHead')
Expand Down

0 comments on commit b4c58d9

Please sign in to comment.