debian-mirror-gitlab/spec/frontend/bootstrap_linked_tabs_spec.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-07-16 23:28:13 +05:30
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2017-08-17 22:00:37 +05:30
import LinkedTabs from '~/lib/utils/bootstrap_linked_tabs';
2018-12-13 13:39:08 +05:30
describe('Linked Tabs', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
loadHTMLFixture('static/linked_tabs.html');
});
afterEach(() => {
resetHTMLFixture();
2018-12-13 13:39:08 +05:30
});
describe('when is initialized', () => {
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2020-05-24 23:13:21 +05:30
jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('should activate the tab correspondent to the given action', () => {
// eslint-disable-next-line no-new
new LinkedTabs({
action: 'tab1',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#tab1').classList).toContain('active');
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should active the default tab action when the action is show', () => {
// eslint-disable-next-line no-new
new LinkedTabs({
action: 'show',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#tab1').classList).toContain('active');
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('on click', () => {
it('should change the url according to the clicked tab', () => {
2020-05-24 23:13:21 +05:30
const historySpy = jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
const linkedTabs = new LinkedTabs({
action: 'show',
defaultAction: 'tab1',
parentEl: '.linked-tabs',
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
const secondTab = document.querySelector('.linked-tabs li:nth-child(2) a');
const newState =
secondTab.getAttribute('href') +
linkedTabs.currentLocation.search +
linkedTabs.currentLocation.hash;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
secondTab.click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
if (historySpy) {
expect(historySpy).toHaveBeenCalledWith(
{
2017-08-17 22:00:37 +05:30
url: newState,
2018-12-13 13:39:08 +05:30
},
document.title,
newState,
);
}
2017-08-17 22:00:37 +05:30
});
});
2018-12-13 13:39:08 +05:30
});