debian-mirror-gitlab/spec/frontend/code_navigation/store/actions_spec.js

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

270 lines
7.2 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2020-03-13 15:44:24 +05:30
import testAction from 'helpers/vuex_action_helper';
import actions from '~/code_navigation/store/actions';
import { setCurrentHoverElement, addInteractionClass } from '~/code_navigation/utils';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2020-03-13 15:44:24 +05:30
jest.mock('~/code_navigation/utils');
describe('Code navigation actions', () => {
2022-06-21 17:19:12 +05:30
const wrapTextNodes = true;
2020-03-13 15:44:24 +05:30
describe('setInitialData', () => {
2022-06-21 17:19:12 +05:30
it('commits SET_INITIAL_DATA', () => {
return testAction(
2020-03-13 15:44:24 +05:30
actions.setInitialData,
2022-06-21 17:19:12 +05:30
{ projectPath: 'test', wrapTextNodes },
2020-03-13 15:44:24 +05:30
{},
2022-06-21 17:19:12 +05:30
[{ type: 'SET_INITIAL_DATA', payload: { projectPath: 'test', wrapTextNodes } }],
2020-03-13 15:44:24 +05:30
[],
);
});
});
describe('requestDataError', () => {
it('commits REQUEST_DATA_ERROR', () =>
testAction(actions.requestDataError, null, {}, [{ type: 'REQUEST_DATA_ERROR' }], []));
});
describe('fetchData', () => {
let mock;
2020-04-22 19:07:51 +05:30
const codeNavigationPath =
'gitlab-org/gitlab-shell/-/jobs/1114/artifacts/raw/lsif/cmd/check/main.go.json';
2022-06-21 17:19:12 +05:30
const state = { blobs: [{ path: 'index.js', codeNavigationPath }], wrapTextNodes };
2020-03-13 15:44:24 +05:30
beforeEach(() => {
window.gon = { api_version: '1' };
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('success', () => {
beforeEach(() => {
2020-04-22 19:07:51 +05:30
mock.onGet(codeNavigationPath).replyOnce(200, [
{
start_line: 0,
start_char: 0,
hover: { value: '123' },
},
{
start_line: 1,
start_char: 0,
hover: null,
},
]);
2020-03-13 15:44:24 +05:30
});
2022-06-21 17:19:12 +05:30
it('commits REQUEST_DATA_SUCCESS with normalized data', () => {
return testAction(
2020-03-13 15:44:24 +05:30
actions.fetchData,
null,
state,
[
{ type: 'REQUEST_DATA' },
{
type: 'REQUEST_DATA_SUCCESS',
2020-04-22 19:07:51 +05:30
payload: {
path: 'index.js',
normalizedData: {
2020-07-28 23:09:34 +05:30
'0:0': {
definitionLineNumber: 0,
start_line: 0,
start_char: 0,
hover: { value: '123' },
},
2020-04-22 19:07:51 +05:30
},
},
2020-03-13 15:44:24 +05:30
},
],
[],
);
});
2022-06-21 17:19:12 +05:30
it('calls addInteractionClass with data', () => {
return testAction(
2020-03-13 15:44:24 +05:30
actions.fetchData,
null,
state,
[
{ type: 'REQUEST_DATA' },
{
type: 'REQUEST_DATA_SUCCESS',
2020-04-22 19:07:51 +05:30
payload: {
path: 'index.js',
normalizedData: {
2020-07-28 23:09:34 +05:30
'0:0': {
definitionLineNumber: 0,
start_line: 0,
start_char: 0,
hover: { value: '123' },
},
2020-04-22 19:07:51 +05:30
},
},
2020-03-13 15:44:24 +05:30
},
],
[],
2022-06-21 17:19:12 +05:30
).then(() => {
expect(addInteractionClass).toHaveBeenCalledWith({
path: 'index.js',
d: {
2020-03-13 15:44:24 +05:30
start_line: 0,
start_char: 0,
hover: { value: '123' },
2022-06-21 17:19:12 +05:30
},
wrapTextNodes,
});
});
2020-03-13 15:44:24 +05:30
});
});
describe('error', () => {
beforeEach(() => {
2020-04-22 19:07:51 +05:30
mock.onGet(codeNavigationPath).replyOnce(500);
2020-03-13 15:44:24 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestDataError', () => {
return testAction(
2020-03-13 15:44:24 +05:30
actions.fetchData,
null,
state,
[{ type: 'REQUEST_DATA' }],
[{ type: 'requestDataError' }],
);
});
});
});
2020-04-22 19:07:51 +05:30
describe('showBlobInteractionZones', () => {
it('calls addInteractionClass with data for a path', () => {
const state = {
data: {
'index.js': { '0:0': 'test', '1:1': 'console.log' },
},
2022-06-21 17:19:12 +05:30
wrapTextNodes,
2020-04-22 19:07:51 +05:30
};
actions.showBlobInteractionZones({ state }, 'index.js');
expect(addInteractionClass).toHaveBeenCalled();
expect(addInteractionClass.mock.calls.length).toBe(2);
2022-06-21 17:19:12 +05:30
expect(addInteractionClass.mock.calls[0]).toEqual([
{ path: 'index.js', d: 'test', wrapTextNodes },
]);
expect(addInteractionClass.mock.calls[1]).toEqual([
{ path: 'index.js', d: 'console.log', wrapTextNodes },
]);
2020-04-22 19:07:51 +05:30
});
2020-05-24 23:13:21 +05:30
it('does not call addInteractionClass when no data exists', () => {
const state = {
data: null,
};
actions.showBlobInteractionZones({ state }, 'index.js');
expect(addInteractionClass).not.toHaveBeenCalled();
});
2020-04-22 19:07:51 +05:30
});
2020-03-13 15:44:24 +05:30
describe('showDefinition', () => {
let target;
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="line"><div class="js-test"></div></div></div>',
);
2020-04-22 19:07:51 +05:30
target = document.querySelector('.js-test');
2020-03-13 15:44:24 +05:30
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2022-06-21 17:19:12 +05:30
it('returns early when no data exists', () => {
return testAction(actions.showDefinition, { target }, {}, [], []);
2020-03-13 15:44:24 +05:30
});
2022-06-21 17:19:12 +05:30
it('commits SET_CURRENT_DEFINITION when target is not code navitation element', () => {
return testAction(actions.showDefinition, { target }, { data: {} }, [], []);
2020-03-13 15:44:24 +05:30
});
2022-06-21 17:19:12 +05:30
it('commits SET_CURRENT_DEFINITION with LSIF data', () => {
2020-03-13 15:44:24 +05:30
target.classList.add('js-code-navigation');
2022-07-23 23:45:48 +05:30
target.dataset.lineIndex = '0';
target.dataset.charIndex = '0';
2020-03-13 15:44:24 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2020-03-13 15:44:24 +05:30
actions.showDefinition,
{ target },
2020-04-22 19:07:51 +05:30
{ data: { 'index.js': { '0:0': { hover: 'test' } } } },
2020-03-13 15:44:24 +05:30
[
{
type: 'SET_CURRENT_DEFINITION',
2020-05-24 23:13:21 +05:30
payload: {
blobPath: 'index.js',
definition: { hover: 'test' },
2020-07-28 23:09:34 +05:30
position: { height: 0, x: 0, y: 0, lineIndex: 0 },
2020-05-24 23:13:21 +05:30
},
2020-03-13 15:44:24 +05:30
},
],
[],
);
});
it('adds hll class to target element', () => {
target.classList.add('js-code-navigation');
2022-07-23 23:45:48 +05:30
target.dataset.lineIndex = '0';
target.dataset.charIndex = '0';
2020-03-13 15:44:24 +05:30
return testAction(
actions.showDefinition,
{ target },
2020-04-22 19:07:51 +05:30
{ data: { 'index.js': { '0:0': { hover: 'test' } } } },
2020-03-13 15:44:24 +05:30
[
{
type: 'SET_CURRENT_DEFINITION',
2020-05-24 23:13:21 +05:30
payload: {
blobPath: 'index.js',
definition: { hover: 'test' },
2020-07-28 23:09:34 +05:30
position: { height: 0, x: 0, y: 0, lineIndex: 0 },
2020-05-24 23:13:21 +05:30
},
2020-03-13 15:44:24 +05:30
},
],
[],
).then(() => {
expect(target.classList).toContain('hll');
});
});
it('caches current target element', () => {
target.classList.add('js-code-navigation');
2022-07-23 23:45:48 +05:30
target.dataset.lineIndex = '0';
target.dataset.charIndex = '0';
2020-03-13 15:44:24 +05:30
return testAction(
actions.showDefinition,
{ target },
2020-04-22 19:07:51 +05:30
{ data: { 'index.js': { '0:0': { hover: 'test' } } } },
2020-03-13 15:44:24 +05:30
[
{
type: 'SET_CURRENT_DEFINITION',
2020-05-24 23:13:21 +05:30
payload: {
blobPath: 'index.js',
definition: { hover: 'test' },
2020-07-28 23:09:34 +05:30
position: { height: 0, x: 0, y: 0, lineIndex: 0 },
2020-05-24 23:13:21 +05:30
},
2020-03-13 15:44:24 +05:30
},
],
[],
).then(() => {
expect(setCurrentHoverElement).toHaveBeenCalledWith(target);
});
});
});
});