debian-mirror-gitlab/spec/frontend/blob/components/table_contents_spec.js

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

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-04-23 21:23:45 +05:30
import { GlDisclosureDropdown } from '@gitlab/ui';
2021-09-04 01:27:46 +05:30
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-09-04 01:27:46 +05:30
import TableContents from '~/blob/components/table_contents.vue';
let wrapper;
function createComponent() {
wrapper = shallowMount(TableContents);
}
2023-04-23 21:23:45 +05:30
const findDropdown = () => wrapper.findComponent(GlDisclosureDropdown);
2021-09-04 01:27:46 +05:30
async function setLoaded(loaded) {
2022-07-23 23:45:48 +05:30
document.querySelector('.blob-viewer').dataset.loaded = loaded;
2021-09-04 01:27:46 +05:30
await nextTick();
}
describe('Markdown table of contents component', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2021-09-04 01:27:46 +05:30
<div class="blob-viewer" data-type="rich" data-loaded="false">
2023-04-23 21:23:45 +05:30
<h1><a id="hello">$</a> Hello</h1>
<h2><a id="world">$</a> World</h2>
<h3><a id="hakuna">$</a> Hakuna</h3>
<h2><a id="matata">$</a> Matata</h2>
2021-09-04 01:27:46 +05:30
</div>
`);
});
afterEach(() => {
wrapper.destroy();
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2021-09-04 01:27:46 +05:30
});
describe('not loaded', () => {
it('does not populate dropdown', () => {
createComponent();
2023-04-23 21:23:45 +05:30
expect(findDropdown().exists()).toBe(false);
2021-12-11 22:18:48 +05:30
});
it('does not show dropdown when loading blob content', async () => {
createComponent();
await setLoaded(false);
2023-04-23 21:23:45 +05:30
expect(findDropdown().exists()).toBe(false);
2021-12-11 22:18:48 +05:30
});
it('does not show dropdown when viewing non-rich content', async () => {
createComponent();
2022-07-23 23:45:48 +05:30
document.querySelector('.blob-viewer').dataset.type = 'simple';
2021-12-11 22:18:48 +05:30
await setLoaded(true);
2023-04-23 21:23:45 +05:30
expect(findDropdown().exists()).toBe(false);
2021-09-04 01:27:46 +05:30
});
});
describe('loaded', () => {
it('populates dropdown', async () => {
createComponent();
await setLoaded(true);
2023-04-23 21:23:45 +05:30
const dropdown = findDropdown();
2021-09-04 01:27:46 +05:30
2023-04-23 21:23:45 +05:30
expect(dropdown.exists()).toBe(true);
expect(dropdown.props('items').length).toBe(4);
2022-11-25 23:54:43 +05:30
// make sure that this only happens once
await setLoaded(true);
2023-04-23 21:23:45 +05:30
expect(dropdown.props('items').length).toBe(4);
});
it('generates proper anchor links', async () => {
createComponent();
await setLoaded(true);
const dropdown = findDropdown();
const items = dropdown.props('items');
const hrefs = items.map((item) => item.href);
expect(hrefs).toEqual(['#hello', '#world', '#hakuna', '#matata']);
2021-09-04 01:27:46 +05:30
});
it('sets padding for dropdown items', async () => {
createComponent();
await setLoaded(true);
2023-04-23 21:23:45 +05:30
const items = findDropdown().props('items');
2021-09-04 01:27:46 +05:30
2023-04-23 21:23:45 +05:30
expect(items[0].extraAttrs.style.paddingLeft).toBe('16px');
expect(items[1].extraAttrs.style.paddingLeft).toBe('24px');
expect(items[2].extraAttrs.style.paddingLeft).toBe('32px');
expect(items[3].extraAttrs.style.paddingLeft).toBe('24px');
2021-09-04 01:27:46 +05:30
});
});
});