add test for shortcuts_dashboard_navigation.js
This commit is contained in:
parent
7ec607d441
commit
203185feca
|
@ -1,13 +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 => {
|
||||
export default function findAndFollowLink(selector) {
|
||||
const element = document.querySelector(selector);
|
||||
const link = element && element.getAttribute('href');
|
||||
|
||||
if (link) {
|
||||
window.location = link;
|
||||
visitUrl(link);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue