Welcome to mirror list, hosted at ThFree Co, Russian Federation.

markdown_viewer_spec.js « viewers « content_viewer « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01ef52c6af9e999e947cc46a9dd3a7f6f3a23a77 (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
import { GlSkeletonLoader } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import MarkdownViewer from '~/vue_shared/components/content_viewer/viewers/markdown_viewer.vue';

describe('MarkdownViewer', () => {
  let wrapper;
  let mock;

  const createComponent = (props) => {
    wrapper = mount(MarkdownViewer, {
      propsData: {
        ...props,
        path: 'test.md',
        content: '*  Test',
        projectPath: 'testproject',
        type: 'markdown',
      },
    });
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);

    jest.spyOn(axios, 'post');
    jest.spyOn($.fn, 'renderGFM');
  });

  afterEach(() => {
    mock.restore();
  });

  describe('success', () => {
    beforeEach(() => {
      mock.onPost(`${gon.relative_url_root}/testproject/preview_markdown`).replyOnce(200, {
        body: '<b>testing</b> {{gl_md_img_1}}',
      });
    });

    it('renders a skeleton loader while the markdown is loading', () => {
      createComponent();

      expect(wrapper.findComponent(GlSkeletonLoader).exists()).toBe(true);
    });

    it('renders markdown preview preview renders and loads rendered markdown from server', () => {
      createComponent();

      return waitForPromises().then(() => {
        expect(wrapper.find('.md-previewer').text()).toContain('testing');
      });
    });

    it('receives the filePath and commitSha as a parameters and passes them on to the server', () => {
      createComponent({ filePath: 'foo/test.md', commitSha: 'abcdef' });

      expect(axios.post).toHaveBeenCalledWith(
        `${gon.relative_url_root}/testproject/preview_markdown`,
        { path: 'foo/test.md', text: '*  Test', ref: 'abcdef' },
        expect.any(Object),
      );
    });

    it.each`
      imgSrc                               | imgAlt
      ${'data:image/jpeg;base64,AAAAAA+/'} | ${'my image title'}
      ${'data:image/jpeg;base64,AAAAAA+/'} | ${'"somebody\'s image" &'}
      ${'hack" onclick=alert(0)'}          | ${'hack" onclick=alert(0)'}
      ${'hack\\" onclick=alert(0)'}        | ${'hack\\" onclick=alert(0)'}
      ${"hack' onclick=alert(0)"}          | ${"hack' onclick=alert(0)"}
      ${"hack'><script>alert(0)</script>"} | ${"hack'><script>alert(0)</script>"}
    `(
      'transforms template tags with base64 encoded images available locally',
      ({ imgSrc, imgAlt }) => {
        createComponent({
          images: {
            '{{gl_md_img_1}}': {
              src: imgSrc,
              alt: imgAlt,
              title: imgAlt,
            },
          },
        });

        return waitForPromises().then(() => {
          const img = wrapper.find('.md-previewer img').element;

          // if the values are the same as the input, it means
          // they were escaped correctly
          expect(img).toHaveAttr('src', imgSrc);
          expect(img).toHaveAttr('alt', imgAlt);
          expect(img).toHaveAttr('title', imgAlt);
        });
      },
    );
  });

  describe('error', () => {
    beforeEach(() => {
      mock.onPost(`${gon.relative_url_root}/testproject/preview_markdown`).replyOnce(500, {
        body: 'Internal Server Error',
      });
    });
    it('renders an error message if loading the markdown preview fails', () => {
      createComponent();

      return waitForPromises().then(() => {
        expect(wrapper.find('.md-previewer').text()).toContain('error');
      });
    });
  });
});