diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2018-04-16 12:05:30 +0300 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2018-04-16 12:05:30 +0300 |
commit | 551f146e8f8def4cc83edd6721e96b26d4c15ef8 (patch) | |
tree | c8f20b6757efc7e279c03d91bf18795c5100b913 | |
parent | 62b0563615d22b3d68a0119da209284ebfe51b6b (diff) | |
parent | 203185feca9d1ba8063d817d18b535c673794ef2 (diff) |
Merge branch 'fix-shortcuts-dashboard-navigation-error' into 'master'
Fix "Uncaught TypeError: Cannot read property 'getAttribute' of null"
See merge request gitlab-org/gitlab-ce!18358
-rw-r--r-- | app/assets/javascripts/shortcuts_dashboard_navigation.js | 11 | ||||
-rw-r--r-- | spec/javascripts/shortcuts_dashboard_navigation_spec.js | 24 |
2 files changed, 31 insertions, 4 deletions
diff --git a/app/assets/javascripts/shortcuts_dashboard_navigation.js b/app/assets/javascripts/shortcuts_dashboard_navigation.js index 25f39e4fdb6..9f69f110d06 100644 --- a/app/assets/javascripts/shortcuts_dashboard_navigation.js +++ b/app/assets/javascripts/shortcuts_dashboard_navigation.js @@ -1,12 +1,15 @@ +import { visitUrl } from './lib/utils/url_utility'; + /** * Helper function that finds the href of the fiven selector and updates the location. * * @param {String} selector */ -export default (selector) => { - const link = document.querySelector(selector).getAttribute('href'); +export default function findAndFollowLink(selector) { + const element = document.querySelector(selector); + const link = element && element.getAttribute('href'); if (link) { - window.location = link; + visitUrl(link); } -}; +} diff --git a/spec/javascripts/shortcuts_dashboard_navigation_spec.js b/spec/javascripts/shortcuts_dashboard_navigation_spec.js new file mode 100644 index 00000000000..888b49004bf --- /dev/null +++ b/spec/javascripts/shortcuts_dashboard_navigation_spec.js @@ -0,0 +1,24 @@ +import findAndFollowLink from '~/shortcuts_dashboard_navigation'; +import * as urlUtility from '~/lib/utils/url_utility'; + +describe('findAndFollowLink', () => { + it('visits a link when the selector exists', () => { + const href = '/some/path'; + const locationSpy = spyOn(urlUtility, 'visitUrl'); + + setFixtures(`<a class="my-shortcut" href="${href}">link</a>`); + + findAndFollowLink('.my-shortcut'); + + expect(locationSpy).toHaveBeenCalledWith(href); + }); + + it('does not throw an exception when the selector does not exist', () => { + const locationSpy = spyOn(urlUtility, 'visitUrl'); + + // this should not throw an exception + findAndFollowLink('.this-selector-does-not-exist'); + + expect(locationSpy).not.toHaveBeenCalled(); + }); +}); |