From c20f5bb61a7fde5968dcbabb8be916623aec033e Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 25 Feb 2015 13:48:41 +0100 Subject: [PATCH] Add some tests for favicon manager and refactor it --- app/utils/favicon-manager.coffee | 22 ++++----- tests/unit/utils/favicon-manager-test.coffee | 51 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 tests/unit/utils/favicon-manager-test.coffee diff --git a/app/utils/favicon-manager.coffee b/app/utils/favicon-manager.coffee index 47b90619..90eaa4ff 100644 --- a/app/utils/favicon-manager.coffee +++ b/app/utils/favicon-manager.coffee @@ -9,30 +9,26 @@ manager.prototype.getHeadTag = -> @headTag || document.getElementsByTagName('head')[0] manager.prototype.setFavicon = (href) -> - link = @getLinkTag() + head = @getHeadTag() - if !link - oldLink = link - link = @createLinkTag() - head = @getHeadTag() - head.appendChild(link) - - link.setAttribute('href', href) - - if oldLink + if oldLink = @getLinkTag() head.removeChild(oldLink) + link = @createLinkTag() + head.appendChild(link) + link.setAttribute('href', href) + manager.prototype.getLinkTag = -> - links = document.getElementsByTagName('head')[0].getElementsByTagName('link') + links = @getHeadTag().getElementsByTagName('link') if links.length for link in links - if link.getAttribute('rel').trim() == 'icon' + if (link.getAttribute('rel') || '').trim() == 'icon' return link manager.prototype.createLinkTag = -> link = document.createElement('link') link.setAttribute('rel', 'icon') link.setAttribute('type', 'image/png') - document.getElementsByTagName('head')[0].appendChild(link) + @getHeadTag().appendChild(link) `export default manager` diff --git a/tests/unit/utils/favicon-manager-test.coffee b/tests/unit/utils/favicon-manager-test.coffee new file mode 100644 index 00000000..d5013381 --- /dev/null +++ b/tests/unit/utils/favicon-manager-test.coffee @@ -0,0 +1,51 @@ +`import Ember from 'ember'` +`import FaviconManager from 'travis/utils/favicon-manager'` + +manager = null +fakeHead = null + +module("Favicon manager", + beforeEach: -> + fakeHead = $('
').appendTo($('#qunit-fixture')) + manager = new FaviconManager(fakeHead[0]) + afterEach: -> + fakeHead.remove() + manager = null +) + +test 'use 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($('')) + + 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($('')) + ok !manager.getLinkTag()