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

261 lines
7 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import actions from '~/code_navigation/store/actions';
import axios from '~/lib/utils/axios_utils';
import { setCurrentHoverElement, addInteractionClass } from '~/code_navigation/utils';
jest.mock('~/code_navigation/utils');
describe('Code navigation actions', () => {
describe('setInitialData', () => {
it('commits SET_INITIAL_DATA', done => {
testAction(
actions.setInitialData,
{ projectPath: 'test' },
{},
[{ type: 'SET_INITIAL_DATA', payload: { projectPath: 'test' } }],
[],
done,
);
});
});
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';
const state = { blobs: [{ path: 'index.js', codeNavigationPath }] };
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
});
it('commits REQUEST_DATA_SUCCESS with normalized data', done => {
testAction(
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
},
],
[],
done,
);
});
it('calls addInteractionClass with data', done => {
testAction(
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
},
],
[],
)
.then(() => {
2020-04-22 19:07:51 +05:30
expect(addInteractionClass).toHaveBeenCalledWith('index.js', {
2020-03-13 15:44:24 +05:30
start_line: 0,
start_char: 0,
hover: { value: '123' },
});
})
.then(done)
.catch(done.fail);
});
});
describe('error', () => {
beforeEach(() => {
2020-04-22 19:07:51 +05:30
mock.onGet(codeNavigationPath).replyOnce(500);
2020-03-13 15:44:24 +05:30
});
it('dispatches requestDataError', done => {
testAction(
actions.fetchData,
null,
state,
[{ type: 'REQUEST_DATA' }],
[{ type: 'requestDataError' }],
done,
);
});
});
});
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' },
},
};
actions.showBlobInteractionZones({ state }, 'index.js');
expect(addInteractionClass).toHaveBeenCalled();
expect(addInteractionClass.mock.calls.length).toBe(2);
expect(addInteractionClass.mock.calls[0]).toEqual(['index.js', 'test']);
expect(addInteractionClass.mock.calls[1]).toEqual(['index.js', 'console.log']);
});
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(() => {
2020-07-28 23:09:34 +05:30
setFixtures(
'<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
});
it('returns early when no data exists', done => {
testAction(actions.showDefinition, { target }, {}, [], [], done);
});
it('commits SET_CURRENT_DEFINITION when target is not code navitation element', done => {
2020-04-22 19:07:51 +05:30
testAction(actions.showDefinition, { target }, { data: {} }, [], [], done);
2020-03-13 15:44:24 +05:30
});
it('commits SET_CURRENT_DEFINITION with LSIF data', done => {
target.classList.add('js-code-navigation');
target.setAttribute('data-line-index', '0');
target.setAttribute('data-char-index', '0');
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
},
],
[],
done,
);
});
it('adds hll class to target element', () => {
target.classList.add('js-code-navigation');
target.setAttribute('data-line-index', '0');
target.setAttribute('data-char-index', '0');
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');
target.setAttribute('data-line-index', '0');
target.setAttribute('data-char-index', '0');
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);
});
});
});
});