Add some tests for favicon manager and refactor it
This commit is contained in:
parent
6bb53716a9
commit
c20f5bb61a
|
@ -9,30 +9,26 @@ manager.prototype.getHeadTag = ->
|
||||||
@headTag || document.getElementsByTagName('head')[0]
|
@headTag || document.getElementsByTagName('head')[0]
|
||||||
|
|
||||||
manager.prototype.setFavicon = (href) ->
|
manager.prototype.setFavicon = (href) ->
|
||||||
link = @getLinkTag()
|
head = @getHeadTag()
|
||||||
|
|
||||||
if !link
|
if oldLink = @getLinkTag()
|
||||||
oldLink = link
|
|
||||||
link = @createLinkTag()
|
|
||||||
head = @getHeadTag()
|
|
||||||
head.appendChild(link)
|
|
||||||
|
|
||||||
link.setAttribute('href', href)
|
|
||||||
|
|
||||||
if oldLink
|
|
||||||
head.removeChild(oldLink)
|
head.removeChild(oldLink)
|
||||||
|
|
||||||
|
link = @createLinkTag()
|
||||||
|
head.appendChild(link)
|
||||||
|
link.setAttribute('href', href)
|
||||||
|
|
||||||
manager.prototype.getLinkTag = ->
|
manager.prototype.getLinkTag = ->
|
||||||
links = document.getElementsByTagName('head')[0].getElementsByTagName('link')
|
links = @getHeadTag().getElementsByTagName('link')
|
||||||
if links.length
|
if links.length
|
||||||
for link in links
|
for link in links
|
||||||
if link.getAttribute('rel').trim() == 'icon'
|
if (link.getAttribute('rel') || '').trim() == 'icon'
|
||||||
return link
|
return link
|
||||||
|
|
||||||
manager.prototype.createLinkTag = ->
|
manager.prototype.createLinkTag = ->
|
||||||
link = document.createElement('link')
|
link = document.createElement('link')
|
||||||
link.setAttribute('rel', 'icon')
|
link.setAttribute('rel', 'icon')
|
||||||
link.setAttribute('type', 'image/png')
|
link.setAttribute('type', 'image/png')
|
||||||
document.getElementsByTagName('head')[0].appendChild(link)
|
@getHeadTag().appendChild(link)
|
||||||
|
|
||||||
`export default manager`
|
`export default manager`
|
||||||
|
|
51
tests/unit/utils/favicon-manager-test.coffee
Normal file
51
tests/unit/utils/favicon-manager-test.coffee
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
`import Ember from 'ember'`
|
||||||
|
`import FaviconManager from 'travis/utils/favicon-manager'`
|
||||||
|
|
||||||
|
manager = null
|
||||||
|
fakeHead = null
|
||||||
|
|
||||||
|
module("Favicon manager",
|
||||||
|
beforeEach: ->
|
||||||
|
fakeHead = $('<div id="fake-head"></div>').appendTo($('#qunit-fixture'))
|
||||||
|
manager = new FaviconManager(fakeHead[0])
|
||||||
|
afterEach: ->
|
||||||
|
fakeHead.remove()
|
||||||
|
manager = null
|
||||||
|
)
|
||||||
|
|
||||||
|
test 'use <head> tag by default', ->
|
||||||
|
manager = new FaviconManager()
|
||||||
|
equal manager.getHeadTag(), $('head')[0]
|
||||||
|
|
||||||
|
test 'set favicon if there is no link tag in head', ->
|
||||||
|
equal fakeHead.find('link').length, 0, 'there should be no link tags initially'
|
||||||
|
|
||||||
|
manager.setFavicon('foobar')
|
||||||
|
|
||||||
|
link = fakeHead.find('link')[0]
|
||||||
|
|
||||||
|
ok link, 'link tag should be added by favicon manager'
|
||||||
|
equal link.getAttribute('href'), 'foobar', 'href attribute for the link should be properly set'
|
||||||
|
equal link.getAttribute('rel'), 'icon', 'rel attribute for the link should be properly set'
|
||||||
|
equal link.getAttribute('type'), 'image/png', 'type attribute for the link should be properly set'
|
||||||
|
|
||||||
|
test 'replace exisiting link tag', ->
|
||||||
|
fakeHead.append($('<link id="foo" rel="icon"></link>'))
|
||||||
|
|
||||||
|
ok 'foo', fakeHead.find('link').attr('id'), 'initially link should exist'
|
||||||
|
|
||||||
|
manager.setFavicon('foobar')
|
||||||
|
|
||||||
|
links = fakeHead.find('link')
|
||||||
|
equal links.length, 1, 'there should be only one link in head'
|
||||||
|
|
||||||
|
link = links[0]
|
||||||
|
|
||||||
|
ok !link.getAttribute('id'), 'existing link should be replaced with a new one'
|
||||||
|
equal link.getAttribute('href'), 'foobar', 'href attribute for the link should be properly set'
|
||||||
|
equal link.getAttribute('rel'), 'icon', 'rel attribute for the link should be properly set'
|
||||||
|
equal link.getAttribute('type'), 'image/png', 'type attribute for the link should be properly set'
|
||||||
|
|
||||||
|
test 'find link with rel=icon only', ->
|
||||||
|
fakeHead.append($('<link id="foo" rel="foo"></link>'))
|
||||||
|
ok !manager.getLinkTag()
|
Loading…
Reference in New Issue
Block a user