2021-03-16 18:11:53 +00:00
|
|
|
import { Range } from 'monaco-editor';
|
|
|
|
import { useFakeRequestAnimationFrame } from 'helpers/fake_request_animation_frame';
|
2021-08-02 18:08:48 +00:00
|
|
|
import setWindowLocation from 'helpers/set_window_location_helper';
|
2021-03-16 18:11:53 +00:00
|
|
|
import {
|
|
|
|
ERROR_INSTANCE_REQUIRED_FOR_EXTENSION,
|
|
|
|
EDITOR_TYPE_CODE,
|
|
|
|
EDITOR_TYPE_DIFF,
|
|
|
|
} from '~/editor/constants';
|
2021-06-18 21:10:06 +00:00
|
|
|
import { SourceEditorExtension } from '~/editor/extensions/source_editor_extension_base';
|
2020-12-15 00:10:07 +00:00
|
|
|
|
2021-05-11 09:12:08 +00:00
|
|
|
jest.mock('~/helpers/startup_css_helper', () => {
|
|
|
|
return {
|
|
|
|
waitForCSSLoaded: jest.fn().mockImplementation((cb) => {
|
|
|
|
// We have to artificially put the callback's execution
|
|
|
|
// to the end of the current call stack to be able to
|
|
|
|
// test that the callback is called after waitForCSSLoaded.
|
|
|
|
// setTimeout with 0 delay does exactly that.
|
|
|
|
// Otherwise we might end up with false positive results
|
|
|
|
setTimeout(() => {
|
|
|
|
cb.apply();
|
|
|
|
}, 0);
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
describe('The basis for an Source Editor extension', () => {
|
2021-03-16 18:11:53 +00:00
|
|
|
const defaultLine = 3;
|
2020-12-15 00:10:07 +00:00
|
|
|
let ext;
|
2021-03-16 18:11:53 +00:00
|
|
|
let event;
|
|
|
|
|
2020-12-15 00:10:07 +00:00
|
|
|
const defaultOptions = { foo: 'bar' };
|
2021-03-16 18:11:53 +00:00
|
|
|
const findLine = (num) => {
|
|
|
|
return document.querySelector(`.line-numbers:nth-child(${num})`);
|
|
|
|
};
|
|
|
|
const generateLines = () => {
|
|
|
|
let res = '';
|
|
|
|
for (let line = 1, lines = 5; line <= lines; line += 1) {
|
|
|
|
res += `<div class="line-numbers">${line}</div>`;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
const generateEventMock = ({ line = defaultLine, el = null } = {}) => {
|
|
|
|
return {
|
|
|
|
target: {
|
|
|
|
element: el || findLine(line),
|
|
|
|
position: {
|
|
|
|
lineNumber: line,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures(generateLines());
|
|
|
|
event = generateEventMock();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('constructor', () => {
|
2021-05-11 09:12:08 +00:00
|
|
|
it('resets the layout in waitForCSSLoaded callback', async () => {
|
|
|
|
const instance = {
|
|
|
|
layout: jest.fn(),
|
|
|
|
};
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension({ instance });
|
2021-05-11 09:12:08 +00:00
|
|
|
expect(instance.layout).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
// We're waiting for the waitForCSSLoaded mock to kick in
|
|
|
|
await jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
expect(instance.layout).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2021-03-16 18:11:53 +00:00
|
|
|
it.each`
|
|
|
|
description | instance | options
|
|
|
|
${'accepts configuration options and instance'} | ${{}} | ${defaultOptions}
|
|
|
|
${'leaves instance intact if no options are passed'} | ${{}} | ${undefined}
|
|
|
|
${'does not fail if both instance and the options are omitted'} | ${undefined} | ${undefined}
|
|
|
|
${'throws if only options are passed'} | ${undefined} | ${defaultOptions}
|
|
|
|
`('$description', ({ instance, options } = {}) => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.deferRerender = jest.fn();
|
2021-03-16 18:11:53 +00:00
|
|
|
const originalInstance = { ...instance };
|
2020-12-15 00:10:07 +00:00
|
|
|
|
2021-03-16 18:11:53 +00:00
|
|
|
if (instance) {
|
|
|
|
if (options) {
|
|
|
|
Object.entries(options).forEach((prop) => {
|
|
|
|
expect(instance[prop]).toBeUndefined();
|
|
|
|
});
|
|
|
|
// Both instance and options are passed
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension({ instance, ...options });
|
2021-03-16 18:11:53 +00:00
|
|
|
Object.entries(options).forEach(([prop, value]) => {
|
|
|
|
expect(ext[prop]).toBeUndefined();
|
|
|
|
expect(instance[prop]).toBe(value);
|
|
|
|
});
|
|
|
|
} else {
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension({ instance });
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(instance).toEqual(originalInstance);
|
|
|
|
}
|
|
|
|
} else if (options) {
|
|
|
|
// Options are passed without instance
|
|
|
|
expect(() => {
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension({ ...options });
|
2021-03-16 18:11:53 +00:00
|
|
|
}).toThrow(ERROR_INSTANCE_REQUIRED_FOR_EXTENSION);
|
2020-12-15 00:10:07 +00:00
|
|
|
} else {
|
2021-03-16 18:11:53 +00:00
|
|
|
// Neither options nor instance are passed
|
|
|
|
expect(() => {
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension();
|
2021-03-16 18:11:53 +00:00
|
|
|
}).not.toThrow();
|
2020-12-15 00:10:07 +00:00
|
|
|
}
|
2021-03-16 18:11:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('initializes the line highlighting', () => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.deferRerender = jest.fn();
|
|
|
|
const spy = jest.spyOn(SourceEditorExtension, 'highlightLines');
|
|
|
|
ext = new SourceEditorExtension({ instance: {} });
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets up the line linking for code instance', () => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.deferRerender = jest.fn();
|
|
|
|
const spy = jest.spyOn(SourceEditorExtension, 'setupLineLinking');
|
2021-03-16 18:11:53 +00:00
|
|
|
const instance = {
|
|
|
|
getEditorType: jest.fn().mockReturnValue(EDITOR_TYPE_CODE),
|
|
|
|
onMouseMove: jest.fn(),
|
|
|
|
onMouseDown: jest.fn(),
|
|
|
|
};
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension({ instance });
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(spy).toHaveBeenCalledWith(instance);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not set up the line linking for diff instance', () => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.deferRerender = jest.fn();
|
|
|
|
const spy = jest.spyOn(SourceEditorExtension, 'setupLineLinking');
|
2021-03-16 18:11:53 +00:00
|
|
|
const instance = {
|
|
|
|
getEditorType: jest.fn().mockReturnValue(EDITOR_TYPE_DIFF),
|
|
|
|
};
|
2021-06-18 21:10:06 +00:00
|
|
|
ext = new SourceEditorExtension({ instance });
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('highlightLines', () => {
|
|
|
|
const revealSpy = jest.fn();
|
|
|
|
const decorationsSpy = jest.fn();
|
|
|
|
const instance = {
|
|
|
|
revealLineInCenter: revealSpy,
|
|
|
|
deltaDecorations: decorationsSpy,
|
|
|
|
};
|
|
|
|
const defaultDecorationOptions = { isWholeLine: true, className: 'active-line-text' };
|
|
|
|
|
|
|
|
useFakeRequestAnimationFrame();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-08-02 18:08:48 +00:00
|
|
|
setWindowLocation('https://localhost');
|
2021-03-16 18:11:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
desc | hash | shouldReveal | expectedRange
|
|
|
|
${'properly decorates a single line'} | ${'#L10'} | ${true} | ${[10, 1, 10, 1]}
|
|
|
|
${'properly decorates multiple lines'} | ${'#L7-42'} | ${true} | ${[7, 1, 42, 1]}
|
|
|
|
${'correctly highlights if lines are reversed'} | ${'#L42-7'} | ${true} | ${[7, 1, 42, 1]}
|
|
|
|
${'highlights one line if start/end are the same'} | ${'#L7-7'} | ${true} | ${[7, 1, 7, 1]}
|
|
|
|
${'does not highlight if there is no hash'} | ${''} | ${false} | ${null}
|
|
|
|
${'does not highlight if the hash is undefined'} | ${undefined} | ${false} | ${null}
|
|
|
|
${'does not highlight if hash is incomplete 1'} | ${'#L'} | ${false} | ${null}
|
|
|
|
${'does not highlight if hash is incomplete 2'} | ${'#L-'} | ${false} | ${null}
|
|
|
|
`('$desc', ({ hash, shouldReveal, expectedRange } = {}) => {
|
|
|
|
window.location.hash = hash;
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.highlightLines(instance);
|
2021-03-16 18:11:53 +00:00
|
|
|
if (!shouldReveal) {
|
|
|
|
expect(revealSpy).not.toHaveBeenCalled();
|
|
|
|
expect(decorationsSpy).not.toHaveBeenCalled();
|
|
|
|
} else {
|
|
|
|
expect(revealSpy).toHaveBeenCalledWith(expectedRange[0]);
|
|
|
|
expect(decorationsSpy).toHaveBeenCalledWith(
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
range: new Range(...expectedRange),
|
|
|
|
options: defaultDecorationOptions,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stores the line decorations on the instance', () => {
|
|
|
|
decorationsSpy.mockReturnValue('foo');
|
|
|
|
window.location.hash = '#L10';
|
|
|
|
expect(instance.lineDecorations).toBeUndefined();
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.highlightLines(instance);
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(instance.lineDecorations).toBe('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setupLineLinking', () => {
|
|
|
|
const instance = {
|
|
|
|
onMouseMove: jest.fn(),
|
|
|
|
onMouseDown: jest.fn(),
|
|
|
|
deltaDecorations: jest.fn(),
|
|
|
|
lineDecorations: 'foo',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.onMouseMoveHandler(event); // generate the anchor
|
2021-03-16 18:11:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
desc | spy
|
|
|
|
${'onMouseMove'} | ${instance.onMouseMove}
|
|
|
|
${'onMouseDown'} | ${instance.onMouseDown}
|
|
|
|
`('sets up the $desc listener', ({ spy } = {}) => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.setupLineLinking(instance);
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
desc | eventTrigger | shouldRemove
|
|
|
|
${'does not remove the line decorations if the event is triggered on a wrong node'} | ${null} | ${false}
|
|
|
|
${'removes existing line decorations when clicking a line number'} | ${'.link-anchor'} | ${true}
|
|
|
|
`('$desc', ({ eventTrigger, shouldRemove } = {}) => {
|
|
|
|
event = generateEventMock({ el: eventTrigger ? document.querySelector(eventTrigger) : null });
|
|
|
|
instance.onMouseDown.mockImplementation((fn) => {
|
|
|
|
fn(event);
|
|
|
|
});
|
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.setupLineLinking(instance);
|
2021-03-16 18:11:53 +00:00
|
|
|
if (shouldRemove) {
|
|
|
|
expect(instance.deltaDecorations).toHaveBeenCalledWith(instance.lineDecorations, []);
|
|
|
|
} else {
|
|
|
|
expect(instance.deltaDecorations).not.toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onMouseMoveHandler', () => {
|
|
|
|
it('stops propagation for contextmenu event on the generated anchor', () => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.onMouseMoveHandler(event);
|
2021-03-16 18:11:53 +00:00
|
|
|
const anchor = findLine(defaultLine).querySelector('a');
|
|
|
|
const contextMenuEvent = new Event('contextmenu');
|
|
|
|
|
|
|
|
jest.spyOn(contextMenuEvent, 'stopPropagation');
|
|
|
|
anchor.dispatchEvent(contextMenuEvent);
|
|
|
|
|
|
|
|
expect(contextMenuEvent.stopPropagation).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates an anchor if it does not exist yet', () => {
|
|
|
|
expect(findLine(defaultLine).querySelector('a')).toBe(null);
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.onMouseMoveHandler(event);
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(findLine(defaultLine).querySelector('a')).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create a new anchor if it exists', () => {
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.onMouseMoveHandler(event);
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(findLine(defaultLine).querySelector('a')).not.toBe(null);
|
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.createAnchor = jest.fn();
|
|
|
|
SourceEditorExtension.onMouseMoveHandler(event);
|
|
|
|
expect(SourceEditorExtension.createAnchor).not.toHaveBeenCalled();
|
2021-03-16 18:11:53 +00:00
|
|
|
expect(findLine(defaultLine).querySelectorAll('a')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create a link if the event is triggered on a wrong node', () => {
|
|
|
|
setFixtures('<div class="wrong-class">3</div>');
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.createAnchor = jest.fn();
|
2021-03-16 18:11:53 +00:00
|
|
|
const wrongEvent = generateEventMock({ el: document.querySelector('.wrong-class') });
|
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
SourceEditorExtension.onMouseMoveHandler(wrongEvent);
|
|
|
|
expect(SourceEditorExtension.createAnchor).not.toHaveBeenCalled();
|
2021-03-16 18:11:53 +00:00
|
|
|
});
|
2020-12-15 00:10:07 +00:00
|
|
|
});
|
|
|
|
});
|