spec_app/spec/javascripts/up/link_spec.js.coffee in upjs-rails-0.17.0 vs spec_app/spec/javascripts/up/link_spec.js.coffee in upjs-rails-0.18.0
- old
+ new
@@ -3,13 +3,13 @@
u = up.util
describe 'Javascript functions', ->
describe 'up.follow', ->
-
- if up.browser.canPushState()
+ describeCapability 'canPushState', ->
+
it 'loads the given link via AJAX and replaces the response in the given target', (done) ->
affix('.before').text('old-before')
affix('.middle').text('old-middle')
affix('.after').text('old-after')
$link = affix('a[href="/path"][up-target=".middle"]')
@@ -30,11 +30,11 @@
it 'uses the method from a data-method attribute', ->
$link = affix('a[href="/path"][data-method="PUT"]')
up.follow($link)
request = @lastRequest()
- expect(request.method).toBe('PUT')
+ expect(request).toHaveRequestMethod('PUT')
it 'allows to refer to the link itself as "&" in the CSS selector', ->
$container = affix('div')
$link1 = $('<a id="first" href="/path" up-target="&">first-link</a>').appendTo($container)
$link2 = $('<a id="second" href="/path" up-target="&">second-link</a>').appendTo($container)
@@ -140,11 +140,30 @@
#
# it "doesn't make a request and reveals the target container"
#
# it "doesn't make a request and reveals the target of a #hash in the URL"
- else
+ describe 'with { confirm } option', ->
+
+ it 'follows the link after the user OKs a confirmation dialog', ->
+ deferred = $.Deferred()
+ spyOn(up.browser, 'confirm').and.returnValue(deferred)
+ spyOn(up, 'replace')
+ $link = affix('a[href="/danger"][up-target=".middle"]')
+ up.follow($link, confirm: 'Do you really want to go there?')
+ expect(up.browser.confirm).toHaveBeenCalledWith('Do you really want to go there?')
+ expect(up.replace).not.toHaveBeenCalled()
+ deferred.resolve()
+ expect(up.replace).toHaveBeenCalled()
+
+ it 'does not show a confirmation dialog if the option is not a present string', ->
+ spyOn(up, 'replace')
+ $link = affix('a[href="/danger"][up-target=".middle"]')
+ up.follow($link, confirm: '')
+ expect(up.replace).toHaveBeenCalled()
+
+ describeFallback 'canPushState', ->
it 'follows the given link', ->
$link = affix('a[href="/path"]')
spyOn(up.browser, 'loadPage')
up.follow($link)
@@ -170,28 +189,30 @@
up.hello($form)
followSpy = up.link.knife.mock('follow')
$form.click()
expect(followSpy).not.toHaveBeenCalled()
- it 'adds a history entry', ->
- affix('.target')
- $link = affix('a[href="/path"][up-target=".target"]')
- $link.click()
- @respondWith('<div class="target">new text</div>')
- expect($('.target')).toHaveText('new text')
- expect(location.pathname).toEqual('/path')
+ describeCapability 'canPushState', ->
- it 'respects a X-Up-Location header that the server sends in case of a redirect', ->
- affix('.target')
- $link = affix('a[href="/path"][up-target=".target"]')
- $link.click()
- @respondWith
- responseText: '<div class="target">new text</div>'
- responseHeaders: { 'X-Up-Location': '/other/path' }
- expect($('.target')).toHaveText('new text')
- expect(location.pathname).toEqual('/other/path')
+ it 'adds a history entry', ->
+ affix('.target')
+ $link = affix('a[href="/path"][up-target=".target"]')
+ $link.click()
+ @respondWith('<div class="target">new text</div>')
+ expect($('.target')).toHaveText('new text')
+ expect(location.pathname).toEqual('/path')
+ it 'respects a X-Up-Location header that the server sends in case of a redirect', ->
+ affix('.target')
+ $link = affix('a[href="/path"][up-target=".target"]')
+ $link.click()
+ @respondWith
+ responseText: '<div class="target">new text</div>'
+ responseHeaders: { 'X-Up-Location': '/other/path' }
+ expect($('.target')).toHaveText('new text')
+ expect(location.pathname).toEqual('/other/path')
+
it 'does not add a history entry when an up-history attribute is set to "false"', ->
oldPathname = location.pathname
affix('.target')
$link = affix('a[href="/path"][up-target=".target"][up-history="false"]')
$link.click()
@@ -210,25 +231,27 @@
it "calls up.follow with the clicked link", ->
Trigger.click(@$link)
expect(@followSpy).toHaveBeenCalledWith(@$link)
- it 'does nothing if the right mouse button is used', ->
- Trigger.click(@$link, button: 2)
- expect(@followSpy).not.toHaveBeenCalled()
+ # IE does not call Javascript and always performs the default action on right clicks
+ unless navigator.userAgent.match(/Trident/)
+ it 'does nothing if the right mouse button is used', ->
+ Trigger.click(@$link, button: 2)
+ expect(@followSpy).not.toHaveBeenCalled()
it 'does nothing if shift is pressed during the click', ->
Trigger.click(@$link, shiftKey: true)
expect(@followSpy).not.toHaveBeenCalled()
it 'does nothing if ctrl is pressed during the click', ->
Trigger.click(@$link, ctrlKey: true)
expect(@followSpy).not.toHaveBeenCalled()
-# it 'does nothing if meta is pressed during the click', ->
-# Trigger.click(@$link, metaKey: true)
-# expect(@followSpy).not.toHaveBeenCalled()
+ it 'does nothing if meta is pressed during the click', ->
+ Trigger.click(@$link, metaKey: true)
+ expect(@followSpy).not.toHaveBeenCalled()
describe 'with [up-instant] modifier', ->
beforeEach ->
@$link.attr('up-instant', '')
@@ -243,13 +266,15 @@
it 'does nothing on click', ->
Trigger.click(@$link)
expect(@followSpy).not.toHaveBeenCalled()
- it 'does nothing if the right mouse button is pressed down', ->
- Trigger.mousedown(@$link, button: 2)
- expect(@followSpy).not.toHaveBeenCalled()
+ # IE does not call Javascript and always performs the default action on right clicks
+ unless navigator.userAgent.match(/Trident/)
+ it 'does nothing if the right mouse button is pressed down', ->
+ Trigger.mousedown(@$link, button: 2)
+ expect(@followSpy).not.toHaveBeenCalled()
it 'does nothing if shift is pressed during mousedown', ->
Trigger.mousedown(@$link, shiftKey: true)
expect(@followSpy).not.toHaveBeenCalled()
@@ -273,15 +298,43 @@
it "renames a contained link's href attribute to up-href so the container is considered a link", ->
$area = affix('div[up-expand] a[up-follow][href="/path"]')
up.hello($area)
expect($area.attr('up-href')).toEqual('/path')
+ it 'copies attributes from the first link if there are multiple links', ->
+ $area = affix('div[up-expand]')
+ $link1 = $area.affix('a[href="/path1"]')
+ $link2 = $area.affix('a[href="/path2"]')
+ up.hello($area)
+ expect($area.attr('up-href')).toEqual('/path1')
+
it "copies an contained non-link element with up-href attribute", ->
$area = affix('div[up-expand] span[up-follow][up-href="/path"]')
up.hello($area)
expect($area.attr('up-href')).toEqual('/path')
it 'adds an up-follow attribute if the contained link has neither up-follow nor up-target attributes', ->
$area = affix('div[up-expand] a[href="/path"]')
up.hello($area)
expect($area.attr('up-follow')).toEqual('')
+ describe 'with a CSS selector in the property value', ->
+
+ it "expands the contained link that matches the selector", ->
+ $area = affix('div[up-expand=".second"]')
+ $link1 = $area.affix('a.first[href="/path1"]')
+ $link2 = $area.affix('a.second[href="/path2"]')
+ up.hello($area)
+ expect($area.attr('up-href')).toEqual('/path2')
+
+ it 'does nothing if no contained link matches the selector', ->
+ $area = affix('div[up-expand=".foo"]')
+ $link = $area.affix('a[href="/path1"]')
+ up.hello($area)
+ expect($area.attr('up-href')).toBeUndefined()
+
+ it 'does not match an element that is not a descendant', ->
+ $area = affix('div[up-expand=".second"]')
+ $link1 = $area.affix('a.first[href="/path1"]')
+ $link2 = affix('a.second[href="/path2"]') # not a child of $area
+ up.hello($area)
+ expect($area.attr('up-href')).toBeUndefined()