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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
1.9 KiB
JavaScript
Raw Normal View History

import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import LinkedTabs from '~/lib/utils/bootstrap_linked_tabs';
2018-10-17 07:16:21 +00:00
describe('Linked Tabs', () => {
beforeEach(() => {
loadHTMLFixture('static/linked_tabs.html');
});
afterEach(() => {
resetHTMLFixture();
2018-10-17 07:16:21 +00:00
});
describe('when is initialized', () => {
beforeEach(() => {
jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});
});
2018-10-17 07:16:21 +00:00
it('should activate the tab correspondent to the given action', () => {
2018-10-17 07:21:28 +00:00
// eslint-disable-next-line no-new
new LinkedTabs({
2018-10-17 07:16:21 +00:00
action: 'tab1',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
2016-12-05 10:00:19 +00:00
});
2018-10-17 07:16:21 +00:00
expect(document.querySelector('#tab1').classList).toContain('active');
});
2018-10-17 07:16:21 +00:00
it('should active the default tab action when the action is show', () => {
2018-10-17 07:21:28 +00:00
// eslint-disable-next-line no-new
new LinkedTabs({
2018-10-17 07:16:21 +00:00
action: 'show',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
});
2018-10-17 07:16:21 +00:00
expect(document.querySelector('#tab1').classList).toContain('active');
});
2018-10-17 07:16:21 +00:00
});
2018-10-17 07:16:21 +00:00
describe('on click', () => {
it('should change the url according to the clicked tab', () => {
const historySpy = jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});
2018-10-17 07:16:21 +00:00
const linkedTabs = new LinkedTabs({
action: 'show',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
});
2018-10-17 07:16:21 +00: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 07:16:21 +00:00
secondTab.click();
2018-10-17 07:16:21 +00:00
if (historySpy) {
expect(historySpy).toHaveBeenCalledWith(
{
url: newState,
},
document.title,
newState,
);
}
});
});
2018-10-17 07:16:21 +00:00
});