spec_app/spec/javascripts/up/proxy_spec.js.coffee in unpoly-rails-0.53.1 vs spec_app/spec/javascripts/up/proxy_spec.js.coffee in unpoly-rails-0.53.2
- old
+ new
@@ -971,27 +971,121 @@
describe 'unobtrusive behavior', ->
describe '[up-preload]', ->
- it 'preloads the link destination on mouseover, after a delay'
+ it 'preloads the link destination when hovering, after a delay', asyncSpec (next) ->
+ up.proxy.config.preloadDelay = 100
+ affix('.target').text('old text')
+
+ $link = affix('a[href="/foo"][up-target=".target"][up-preload]')
+ up.hello($link)
+
+ Trigger.hoverSequence($link)
+
+ next.after 50, =>
+ # It's still too early
+ expect(jasmine.Ajax.requests.count()).toEqual(0)
+
+ next.after 75, =>
+ expect(jasmine.Ajax.requests.count()).toEqual(1)
+ expect(@lastRequest().url).toMatchUrl('/foo')
+ expect(@lastRequest()).toHaveRequestMethod('GET')
+ expect(@lastRequest().requestHeaders['X-Up-Target']).toEqual('.target')
+
+ @respondWith """
+ <div class="target">
+ new text
+ </div>
+ """
+
+ next =>
+ # We only preloaded, so the target isn't replaced yet.
+ expect('.target').toHaveText('old text')
+
+ Trigger.clickSequence($link)
+
+ next =>
+ # No additional request has been sent since we already preloaded
+ expect(jasmine.Ajax.requests.count()).toEqual(1)
+
+ # The target is replaced instantly
+ expect('.target').toHaveText('new text')
+
+ it 'does not send a request if the user stops hovering before the delay is over', asyncSpec (next) ->
+ up.proxy.config.preloadDelay = 100
+
+ affix('.target').text('old text')
+
+ $link = affix('a[href="/foo"][up-target=".target"][up-preload]')
+ up.hello($link)
+
+ Trigger.hoverSequence($link)
+
+ next.after 40, =>
+ # It's still too early
+ expect(jasmine.Ajax.requests.count()).toEqual(0)
+
+ Trigger.unhoverSequence($link)
+
+ next.after 90, =>
+ expect(jasmine.Ajax.requests.count()).toEqual(0)
+
+ it 'does not cache a failed response', asyncSpec (next) ->
+ up.proxy.config.preloadDelay = 0
+
+ affix('.target').text('old text')
+
+ $link = affix('a[href="/foo"][up-target=".target"][up-preload]')
+ up.hello($link)
+
+ Trigger.hoverSequence($link)
+
+ next.after 2, =>
+ expect(jasmine.Ajax.requests.count()).toEqual(1)
+
+ @respondWith
+ status: 500
+ responseText: """
+ <div class="target">
+ new text
+ </div>
+ """
+
+ next =>
+ # We only preloaded, so the target isn't replaced yet.
+ expect('.target').toHaveText('old text')
+
+ Trigger.click($link)
+
+ next =>
+ # Since the preloading failed, we send another request
+ expect(jasmine.Ajax.requests.count()).toEqual(2)
+
+ # Since there isn't anyone who could handle the rejection inside
+ # the event handler, our handler mutes the rejection.
+ expect(window).not.toHaveUnhandledRejections()
+
it 'triggers a separate AJAX request when hovered multiple times and the cache expires between hovers', asyncSpec (next) ->
up.proxy.config.cacheExpiry = 50
up.proxy.config.preloadDelay = 0
+
$element = affix('a[href="/foo"][up-preload]')
- Trigger.mouseover($element)
+ up.hello($element)
+ Trigger.hoverSequence($element)
+
next.after 1, =>
expect(jasmine.Ajax.requests.count()).toEqual(1)
next.after 1, =>
- Trigger.mouseover($element)
+ Trigger.hoverSequence($element)
next.after 1, =>
expect(jasmine.Ajax.requests.count()).toEqual(1)
next.after 60, =>
- Trigger.mouseover($element)
+ Trigger.hoverSequence($element)
next.after 1, =>
expect(jasmine.Ajax.requests.count()).toEqual(2)