debian-mirror-gitlab/spec/frontend/code_navigation/utils/index_spec.js

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

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2020-03-13 15:44:24 +05:30
import {
cachedData,
getCurrentHoverElement,
setCurrentHoverElement,
addInteractionClass,
} from '~/code_navigation/utils';
afterEach(() => {
if (cachedData.has('current')) {
cachedData.delete('current');
}
});
describe('getCurrentHoverElement', () => {
it.each`
value
${'test'}
${undefined}
2022-10-11 01:57:18 +05:30
`('returns cached current key', ({ value }) => {
2020-03-13 15:44:24 +05:30
if (value) {
cachedData.set('current', value);
}
expect(getCurrentHoverElement()).toEqual(value);
});
});
describe('setCurrentHoverElement', () => {
it('sets cached current key', () => {
setCurrentHoverElement('test');
expect(getCurrentHoverElement()).toEqual('test');
});
});
describe('addInteractionClass', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(
2020-07-28 23:09:34 +05:30
'<div data-path="index.js"><div class="blob-content"><div id="LC1" class="line"><span>console</span><span>.</span><span>log</span></div><div id="LC2" class="line"><span>function</span></div></div></div>',
2020-03-13 15:44:24 +05:30
);
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2020-03-13 15:44:24 +05:30
it.each`
line | char | index
${0} | ${0} | ${0}
${0} | ${8} | ${2}
${1} | ${0} | ${0}
2022-06-21 17:19:12 +05:30
${1} | ${0} | ${0}
2020-03-13 15:44:24 +05:30
`(
2022-10-11 01:57:18 +05:30
'sets code navigation attributes for line $line and character $char',
2020-03-13 15:44:24 +05:30
({ line, char, index }) => {
2022-06-21 17:19:12 +05:30
addInteractionClass({ path: 'index.js', d: { start_line: line, start_char: char } });
2020-03-13 15:44:24 +05:30
expect(document.querySelectorAll(`#LC${line + 1} span`)[index].classList).toContain(
'js-code-navigation',
);
},
);
2022-06-21 17:19:12 +05:30
describe('wrapTextNodes', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(
2022-06-21 17:19:12 +05:30
'<div data-path="index.js"><div class="blob-content"><div id="LC1" class="line"> Text </div></div></div>',
);
});
const params = { path: 'index.js', d: { start_line: 0, start_char: 0 } };
const findAllSpans = () => document.querySelectorAll('#LC1 span');
it('does not wrap text nodes by default', () => {
addInteractionClass(params);
const spans = findAllSpans();
expect(spans.length).toBe(0);
});
it('wraps text nodes if wrapTextNodes is true', () => {
addInteractionClass({ ...params, wrapTextNodes: true });
const spans = findAllSpans();
expect(spans.length).toBe(3);
expect(spans[0].textContent).toBe(' ');
expect(spans[1].textContent).toBe('Text');
expect(spans[2].textContent).toBe(' ');
});
2022-11-25 23:54:43 +05:30
it('adds the correct class names to wrapped nodes', () => {
setHTMLFixture(
'<div data-path="index.js"><div class="blob-content"><div id="LC1" class="line"><span class="test"> Text </span></div></div></div>',
);
addInteractionClass({ ...params, wrapTextNodes: true });
expect(findAllSpans()[1].classList.contains('test')).toBe(true);
});
2022-06-21 17:19:12 +05:30
});
2020-03-13 15:44:24 +05:30
});