1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
import MockAdapter from 'axios-mock-adapter';
import { Emitter } from 'monaco-editor';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import waitForPromises from 'helpers/wait_for_promises';
import {
EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS,
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID,
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH,
EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS,
EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY,
} from '~/editor/constants';
import { EditorMarkdownPreviewExtension } from '~/editor/extensions/source_editor_markdown_livepreview_ext';
import SourceEditor from '~/editor/source_editor';
import { createAlert } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import syntaxHighlight from '~/syntax_highlight';
import { spyOnApi } from './helpers';
jest.mock('~/syntax_highlight');
jest.mock('~/alert');
describe('Markdown Live Preview Extension for Source Editor', () => {
let editor;
let instance;
let editorEl;
let panelSpy;
let mockAxios;
let extension;
let resizeCallback;
const previewMarkdownPath = '/gitlab/fooGroup/barProj/preview_markdown';
const firstLine = 'This is a';
const secondLine = 'multiline';
const thirdLine = 'string with some **markup**';
const text = `${firstLine}\n${secondLine}\n${thirdLine}`;
const markdownPath = 'foo.md';
const responseData = '<div>FooBar</div>';
const observeSpy = jest.fn();
const disconnectSpy = jest.fn();
const togglePreview = async () => {
instance.togglePreview();
await waitForPromises();
};
beforeEach(() => {
mockAxios = new MockAdapter(axios);
setHTMLFixture(
'<div style="width: 500px; height: 500px"><div id="editor" data-editor-loading></div></div>',
);
editorEl = document.getElementById('editor');
global.ResizeObserver = class {
constructor(callback) {
resizeCallback = callback;
this.observe = (node) => {
return observeSpy(node);
};
this.disconnect = () => {
return disconnectSpy();
};
}
};
editor = new SourceEditor();
instance = editor.createInstance({
el: editorEl,
blobPath: markdownPath,
blobContent: text,
});
instance.toolbar = {
addItems: jest.fn(),
updateItem: jest.fn(),
removeItems: jest.fn(),
};
extension = instance.use({
definition: EditorMarkdownPreviewExtension,
setupOptions: { previewMarkdownPath },
});
panelSpy = jest.spyOn(extension.obj.constructor.prototype, 'togglePreviewPanel');
});
afterEach(() => {
instance.dispose();
mockAxios.restore();
resetHTMLFixture();
});
it('sets up the preview on the instance', () => {
expect(instance.markdownPreview).toEqual({
el: undefined,
actions: expect.any(Object),
shown: false,
modelChangeListener: undefined,
path: previewMarkdownPath,
actionShowPreviewCondition: expect.any(Object),
eventEmitter: expect.any(Object),
});
});
it('support external preview trigger via emitter event', () => {
expect(panelSpy).not.toHaveBeenCalled();
instance.markdownPreview.eventEmitter.fire();
expect(panelSpy).toHaveBeenCalled();
});
describe('ResizeObserver handler', () => {
it('sets a ResizeObserver to observe the container DOM node', () => {
observeSpy.mockClear();
instance.togglePreview();
expect(observeSpy).toHaveBeenCalledWith(instance.getContainerDomNode());
});
describe('disconnects the ResizeObserver when…', () => {
beforeEach(() => {
instance.togglePreview();
instance.markdownPreview.modelChangeListener = {
dispose: jest.fn(),
};
});
it('the preview gets closed', () => {
expect(disconnectSpy).not.toHaveBeenCalled();
instance.togglePreview();
expect(disconnectSpy).toHaveBeenCalled();
});
it('the extension is unused', () => {
expect(disconnectSpy).not.toHaveBeenCalled();
instance.unuse(extension);
expect(disconnectSpy).toHaveBeenCalled();
});
});
describe('layout behavior', () => {
let layoutSpy;
let instanceDimensions;
let newInstanceWidth;
beforeEach(() => {
instanceDimensions = instance.getLayoutInfo();
});
it('does not trigger the layout if the preview panel is closed', () => {
layoutSpy = jest.spyOn(instance, 'layout');
newInstanceWidth = instanceDimensions.width + 100;
// Manually trigger the resize event
resizeCallback([{ contentRect: { width: newInstanceWidth } }]);
expect(layoutSpy).not.toHaveBeenCalled();
});
it('triggers the layout if the preview panel is opened, and width of the editor has changed', () => {
instance.togglePreview();
layoutSpy = jest.spyOn(instance, 'layout');
newInstanceWidth = instanceDimensions.width + 100;
// Manually trigger the resize event
resizeCallback([{ contentRect: { width: newInstanceWidth } }]);
expect(layoutSpy).toHaveBeenCalledWith({
width: newInstanceWidth * EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH,
height: instanceDimensions.height,
});
});
});
});
describe('model change listener', () => {
let cleanupSpy;
let actionSpy;
beforeEach(() => {
cleanupSpy = jest.fn();
actionSpy = jest.fn();
spyOnApi(extension, {
cleanup: cleanupSpy,
setupPreviewAction: actionSpy,
});
instance.togglePreview();
});
afterEach(() => {
jest.clearAllMocks();
});
it('does not do anything if there is no model', () => {
instance.setModel(null);
expect(cleanupSpy).not.toHaveBeenCalled();
expect(actionSpy).not.toHaveBeenCalled();
});
});
describe('onBeforeUnuse', () => {
beforeEach(async () => {
mockAxios.onPost().reply(HTTP_STATUS_OK, { body: responseData });
await togglePreview();
});
afterEach(() => {
jest.clearAllMocks();
});
it('removes the registered buttons from the toolbar', () => {
expect(instance.toolbar.removeItems).not.toHaveBeenCalled();
instance.unuse(extension);
expect(instance.toolbar.removeItems).toHaveBeenCalledWith([]);
});
it('disposes the modelChange listener and does not fetch preview on content changes', () => {
expect(instance.markdownPreview.modelChangeListener).toBeDefined();
const fetchPreviewSpy = jest.fn();
spyOnApi(extension, {
fetchPreview: fetchPreviewSpy,
});
instance.unuse(extension);
instance.setValue('Foo Bar');
jest.advanceTimersByTime(EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY);
expect(fetchPreviewSpy).not.toHaveBeenCalled();
});
it('removes the contextual menu action', () => {
expect(instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID)).toBeDefined();
instance.unuse(extension);
expect(instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID)).toBe(null);
});
it('toggles the panel only if the preview is visible', () => {
const { el: previewEl } = instance.markdownPreview;
const parentEl = previewEl.parentElement;
expect(previewEl).toBeVisible();
expect(parentEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(true);
instance.unuse(extension);
expect(previewEl).toBeHidden();
expect(parentEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
false,
);
instance.unuse(extension);
expect(previewEl).toBeHidden();
expect(parentEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
false,
);
});
it('toggles the layout only if the preview is visible', () => {
const { width } = instance.getLayoutInfo();
expect(instance.markdownPreview.shown).toBe(true);
instance.unuse(extension);
const { width: newWidth } = instance.getLayoutInfo();
expect(newWidth === width / EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH).toBe(true);
instance.unuse(extension);
expect(newWidth === width / EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH).toBe(true);
});
it('disconnects the ResizeObserver', () => {
instance.unuse(extension);
expect(disconnectSpy).toHaveBeenCalled();
});
it('does not trigger the re-layout after instance is unused', async () => {
const emitter = new Emitter();
instance.unuse(extension);
instance.onDidLayoutChange = emitter.event;
// we have to re-use the extension to pick up the emitter
extension = instance.use({
definition: EditorMarkdownPreviewExtension,
setupOptions: { previewMarkdownPath },
});
instance.unuse(extension);
const layoutSpy = jest.spyOn(instance, 'layout');
await emitter.fire();
expect(layoutSpy).not.toHaveBeenCalled();
});
});
describe('fetchPreview', () => {
const fetchPreview = async () => {
instance.fetchPreview();
await waitForPromises();
};
let previewMarkdownSpy;
beforeEach(() => {
previewMarkdownSpy = jest
.fn()
.mockImplementation(() => [HTTP_STATUS_OK, { body: responseData }]);
mockAxios.onPost(previewMarkdownPath).replyOnce((req) => previewMarkdownSpy(req));
});
it('correctly fetches preview based on previewMarkdownPath', async () => {
await fetchPreview();
expect(previewMarkdownSpy).toHaveBeenCalledWith(
expect.objectContaining({ data: JSON.stringify({ text }) }),
);
});
it('puts the fetched content into the preview DOM element', async () => {
instance.markdownPreview.el = editorEl.parentElement;
await fetchPreview();
expect(instance.markdownPreview.el.innerHTML).toEqual(responseData);
});
it('applies syntax highlighting to the preview content', async () => {
instance.markdownPreview.el = editorEl.parentElement;
await fetchPreview();
expect(syntaxHighlight).toHaveBeenCalled();
});
it('catches the errors when fetching the preview', async () => {
mockAxios.onPost().reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
await fetchPreview();
expect(createAlert).toHaveBeenCalled();
});
});
describe('setupPreviewAction', () => {
it('adds the contextual menu action', () => {
expect(instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID)).toBeDefined();
});
it('does not set up action if one already exists', () => {
jest.spyOn(instance, 'addAction').mockImplementation();
instance.setupPreviewAction();
expect(instance.addAction).not.toHaveBeenCalled();
});
it('toggles preview when the action is triggered', () => {
const togglePreviewSpy = jest.fn();
spyOnApi(extension, {
togglePreview: togglePreviewSpy,
});
expect(togglePreviewSpy).not.toHaveBeenCalled();
const action = instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID);
action.run();
expect(togglePreviewSpy).toHaveBeenCalled();
});
});
describe('togglePreview', () => {
beforeEach(() => {
mockAxios.onPost().reply(HTTP_STATUS_OK, { body: responseData });
});
it('toggles the condition to toggle preview/hide actions in the context menu', () => {
expect(instance.markdownPreview.actionShowPreviewCondition.get()).toBe(true);
instance.togglePreview();
expect(instance.markdownPreview.actionShowPreviewCondition.get()).toBe(false);
});
it('toggles preview flag on instance', () => {
expect(instance.markdownPreview.shown).toBe(false);
instance.togglePreview();
expect(instance.markdownPreview.shown).toBe(true);
instance.togglePreview();
expect(instance.markdownPreview.shown).toBe(false);
});
describe('panel DOM element set up', () => {
it('sets up an element to contain the preview and stores it on instance', () => {
expect(instance.markdownPreview.el).toBeUndefined();
instance.togglePreview();
expect(instance.markdownPreview.el).toBeDefined();
expect(
instance.markdownPreview.el.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS),
).toBe(true);
});
it('re-uses existing preview DOM element on repeated calls', () => {
instance.togglePreview();
const origPreviewEl = instance.markdownPreview.el;
instance.togglePreview();
expect(instance.markdownPreview.el).toBe(origPreviewEl);
});
it('hides the preview DOM element by default', () => {
panelSpy.mockImplementation();
instance.togglePreview();
expect(instance.markdownPreview.el.style.display).toBe('none');
});
});
describe('preview layout setup', () => {
it('sets correct preview layout', () => {
jest.spyOn(instance, 'layout');
const { width, height } = instance.getLayoutInfo();
instance.togglePreview();
expect(instance.layout).toHaveBeenCalledWith({
width: width * EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH,
height,
});
});
});
describe('preview panel', () => {
it('toggles preview CSS class on the editor', () => {
expect(editorEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
false,
);
instance.togglePreview();
expect(editorEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
true,
);
instance.togglePreview();
expect(editorEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
false,
);
});
it('toggles visibility of the preview DOM element', async () => {
await togglePreview();
expect(instance.markdownPreview.el.style.display).toBe('block');
await togglePreview();
expect(instance.markdownPreview.el.style.display).toBe('none');
});
describe('hidden preview DOM element', () => {
it('listens to model changes and re-fetches preview', async () => {
expect(mockAxios.history.post).toHaveLength(0);
await togglePreview();
expect(mockAxios.history.post).toHaveLength(1);
instance.setValue('New Value');
await waitForPromises();
expect(mockAxios.history.post).toHaveLength(2);
});
it('stores disposable listener for model changes', async () => {
expect(instance.markdownPreview.modelChangeListener).toBeUndefined();
await togglePreview();
expect(instance.markdownPreview.modelChangeListener).toBeDefined();
});
});
describe('already visible preview', () => {
beforeEach(async () => {
await togglePreview();
mockAxios.resetHistory();
});
it('does not re-fetch the preview', () => {
instance.togglePreview();
expect(mockAxios.history.post).toHaveLength(0);
});
it('disposes the model change event listener', () => {
const disposeSpy = jest.fn();
instance.markdownPreview.modelChangeListener = {
dispose: disposeSpy,
};
instance.togglePreview();
expect(disposeSpy).toHaveBeenCalled();
});
});
});
});
});
|