summaryrefslogtreecommitdiff
path: root/spec/frontend/drawio/markdown_field_editor_facade_spec.js
blob: e3eafc63839f2489f1668cddc0c88fbce970b99e (plain)
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
import AxiosMockAdapter from 'axios-mock-adapter';
import { create } from '~/drawio/markdown_field_editor_facade';
import * as textMarkdown from '~/lib/utils/text_markdown';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import axios from '~/lib/utils/axios_utils';

jest.mock('~/lib/utils/text_markdown');

describe('drawio/textareaMarkdownEditor', () => {
  let textArea;
  let textareaMarkdownEditor;
  let axiosMock;

  const markdownPreviewPath = '/markdown/preview';
  const imageURL = '/assets/image.png';
  const diagramMarkdown = '![](image.png)';
  const diagramSvg = '<svg></svg>';
  const contentType = 'image/svg+xml';
  const filename = 'image.png';
  const newDiagramMarkdown = '![](newdiagram.svg)';
  const uploadsPath = '/uploads';

  beforeEach(() => {
    textArea = document.createElement('textarea');
    textareaMarkdownEditor = create({ textArea, markdownPreviewPath, uploadsPath });

    document.body.appendChild(textArea);
  });
  beforeEach(() => {
    axiosMock = new AxiosMockAdapter(axios);
  });

  afterEach(() => {
    axiosMock.restore();
    textArea.remove();
  });

  describe('getDiagram', () => {
    describe('when there is a selected diagram', () => {
      beforeEach(() => {
        textMarkdown.resolveSelectedImage.mockReturnValueOnce({
          imageURL,
          imageMarkdown: diagramMarkdown,
          filename,
        });
        axiosMock
          .onGet(imageURL)
          .reply(HTTP_STATUS_OK, diagramSvg, { 'content-type': contentType });
      });

      it('returns diagram information', async () => {
        const diagram = await textareaMarkdownEditor.getDiagram();

        expect(textMarkdown.resolveSelectedImage).toHaveBeenCalledWith(
          textArea,
          markdownPreviewPath,
        );

        expect(diagram).toEqual({
          diagramURL: imageURL,
          diagramMarkdown,
          filename,
          diagramSvg,
          contentType,
        });
      });
    });

    describe('when there is not a selected diagram', () => {
      beforeEach(() => {
        textMarkdown.resolveSelectedImage.mockReturnValueOnce(null);
      });

      it('returns null', async () => {
        const diagram = await textareaMarkdownEditor.getDiagram();

        expect(textMarkdown.resolveSelectedImage).toHaveBeenCalledWith(
          textArea,
          markdownPreviewPath,
        );

        expect(diagram).toBe(null);
      });
    });
  });

  describe('updateDiagram', () => {
    beforeEach(() => {
      jest.spyOn(textArea, 'focus');
      jest.spyOn(textArea, 'dispatchEvent');

      textArea.value = `diagram ${diagramMarkdown}`;

      textareaMarkdownEditor.updateDiagram({
        diagramMarkdown,
        uploadResults: { link: { markdown: newDiagramMarkdown } },
      });
    });

    it('focuses the textarea', () => {
      expect(textArea.focus).toHaveBeenCalled();
    });

    it('replaces previous diagram markdown with new diagram markdown', () => {
      expect(textArea.value).toBe(`diagram ${newDiagramMarkdown}`);
    });

    it('dispatches input event in the textarea', () => {
      expect(textArea.dispatchEvent).toHaveBeenCalledWith(new Event('input'));
    });
  });

  describe('insertDiagram', () => {
    it('inserts markdown text and replaces any selected markdown in the textarea', () => {
      textArea.value = `diagram ${diagramMarkdown}`;
      textArea.setSelectionRange(0, 8);

      textareaMarkdownEditor.insertDiagram({
        uploadResults: { link: { markdown: newDiagramMarkdown } },
      });

      expect(textMarkdown.insertMarkdownText).toHaveBeenCalledWith({
        textArea,
        text: textArea.value,
        tag: newDiagramMarkdown,
        selected: textArea.value.substring(0, 8),
      });
    });
  });

  describe('uploadDiagram', () => {
    it('sends a post request to the uploadsPath containing the diagram svg', async () => {
      const link = { markdown: '![](diagram.drawio.svg)' };
      const blob = new Blob([diagramSvg], { type: 'image/svg+xml' });
      const formData = new FormData();

      formData.append('file', blob, filename);

      axiosMock.onPost(uploadsPath, formData).reply(HTTP_STATUS_OK, {
        link,
      });

      const response = await textareaMarkdownEditor.uploadDiagram({ diagramSvg, filename });

      expect(response).toEqual({ link });
    });
  });
});