gitlab-org--gitlab-foss/spec/javascripts/bootstrap_linked_tabs_spec.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

import LinkedTabs from '~/lib/utils/bootstrap_linked_tabs';
2018-10-17 03:16:21 -04:00
describe('Linked Tabs', () => {
preloadFixtures('static/linked_tabs.html');
2018-10-17 03:16:21 -04:00
beforeEach(() => {
loadFixtures('static/linked_tabs.html');
2018-10-17 03:16:21 -04:00
});
describe('when is initialized', () => {
beforeEach(() => {
2018-10-17 03:16:21 -04:00
spyOn(window.history, 'replaceState').and.callFake(function() {});
});
2018-10-17 03:16:21 -04:00
it('should activate the tab correspondent to the given action', () => {
2018-10-17 03:21:28 -04:00
// eslint-disable-next-line no-new
new LinkedTabs({
2018-10-17 03:16:21 -04:00
action: 'tab1',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
2016-12-05 05:00:19 -05:00
});
2018-10-17 03:16:21 -04:00
expect(document.querySelector('#tab1').classList).toContain('active');
});
2018-10-17 03:16:21 -04:00
it('should active the default tab action when the action is show', () => {
2018-10-17 03:21:28 -04:00
// eslint-disable-next-line no-new
new LinkedTabs({
2018-10-17 03:16:21 -04:00
action: 'show',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
});
2018-10-17 03:16:21 -04:00
expect(document.querySelector('#tab1').classList).toContain('active');
});
2018-10-17 03:16:21 -04:00
});
2018-10-17 03:16:21 -04:00
describe('on click', () => {
it('should change the url according to the clicked tab', () => {
const historySpy = spyOn(window.history, 'replaceState').and.callFake(() => {});
2018-10-17 03:16:21 -04:00
const linkedTabs = new LinkedTabs({
action: 'show',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
});
2018-10-17 03:16:21 -04:00
const secondTab = document.querySelector('.linked-tabs li:nth-child(2) a');
const newState =
secondTab.getAttribute('href') +
linkedTabs.currentLocation.search +
linkedTabs.currentLocation.hash;
2018-10-17 03:16:21 -04:00
secondTab.click();
2018-10-17 03:16:21 -04:00
if (historySpy) {
expect(historySpy).toHaveBeenCalledWith(
{
url: newState,
},
document.title,
newState,
);
}
});
});
2018-10-17 03:16:21 -04:00
});