summaryrefslogtreecommitdiff
path: root/spec/frontend/pages/shared/wikis/components/wiki_content_spec.js
blob: c8e9a31b526e9c50fd33b5516b2cdafaf89fdb00 (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
import { GlSkeletonLoader, GlAlert } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import WikiContent from '~/pages/shared/wikis/components/wiki_content.vue';
import { renderGFM } from '~/behaviors/markdown/render_gfm';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import waitForPromises from 'helpers/wait_for_promises';
import { handleLocationHash } from '~/lib/utils/common_utils';

jest.mock('~/behaviors/markdown/render_gfm');
jest.mock('~/lib/utils/common_utils');

describe('pages/shared/wikis/components/wiki_content', () => {
  const PATH = '/test';
  let wrapper;
  let mock;

  function buildWrapper(propsData = {}) {
    wrapper = shallowMount(WikiContent, {
      propsData: { getWikiContentUrl: PATH, ...propsData },
      stubs: {
        GlSkeletonLoader,
        GlAlert,
      },
    });
  }

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

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  const findGlAlert = () => wrapper.findComponent(GlAlert);
  const findGlSkeletonLoader = () => wrapper.findComponent(GlSkeletonLoader);
  const findContent = () => wrapper.find('[data-testid="wiki-page-content"]');

  describe('when loading content', () => {
    beforeEach(() => {
      buildWrapper();
    });

    it('renders skeleton loader', () => {
      expect(findGlSkeletonLoader().exists()).toBe(true);
    });

    it('does not render content container or error alert', () => {
      expect(findGlAlert().exists()).toBe(false);
      expect(findContent().exists()).toBe(false);
    });
  });

  describe('when content loads successfully', () => {
    const content = 'content';

    beforeEach(() => {
      mock.onGet(PATH, { params: { render_html: true } }).replyOnce(HTTP_STATUS_OK, { content });
      buildWrapper();
      return waitForPromises();
    });

    it('renders content container', () => {
      expect(findContent().text()).toBe(content);
    });

    it('does not render skeleton loader or error alert', () => {
      expect(findGlAlert().exists()).toBe(false);
      expect(findGlSkeletonLoader().exists()).toBe(false);
    });

    it('calls renderGFM after nextTick', async () => {
      await nextTick();

      expect(renderGFM).toHaveBeenCalledWith(wrapper.element);
    });

    it('handles hash after render', async () => {
      await nextTick();

      expect(handleLocationHash).toHaveBeenCalled();
    });
  });

  describe('when loading content fails', () => {
    beforeEach(() => {
      mock.onGet(PATH).replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR, '');
      buildWrapper();
      return waitForPromises();
    });

    it('renders error alert', () => {
      expect(findGlAlert().exists()).toBe(true);
    });

    it('does not render skeleton loader or content container', () => {
      expect(findContent().exists()).toBe(false);
      expect(findGlSkeletonLoader().exists()).toBe(false);
    });
  });
});