Commit 551f146e authored by Filipa Lacerda's avatar Filipa Lacerda

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
parents 62b05636 203185fe
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);
}
};
}
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();
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment