summaryrefslogtreecommitdiff
path: root/spec/frontend/snippets/components/snippet_blob_edit_spec.js
blob: 009074b45581c3491ddc258693b7be361cf881e7 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import SnippetBlobEdit from '~/snippets/components/snippet_blob_edit.vue';
import BlobHeaderEdit from '~/blob/components/blob_edit_header.vue';
import BlobContentEdit from '~/blob/components/blob_edit_content.vue';
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import AxiosMockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { joinPaths } from '~/lib/utils/url_utility';
import waitForPromises from 'helpers/wait_for_promises';

jest.mock('~/blob/utils', () => jest.fn());

jest.mock('~/lib/utils/url_utility', () => ({
  getBaseURL: jest.fn().mockReturnValue('foo/'),
  joinPaths: jest
    .fn()
    .mockName('joinPaths')
    .mockReturnValue('contentApiURL'),
}));

jest.mock('~/flash');

let flashSpy;

describe('Snippet Blob Edit component', () => {
  let wrapper;
  let axiosMock;
  const contentMock = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  const pathMock = 'lorem.txt';
  const rawPathMock = 'foo/bar';
  const blob = {
    path: pathMock,
    content: contentMock,
    rawPath: rawPathMock,
  };
  const findComponent = component => wrapper.find(component);

  function createComponent(props = {}, data = { isContentLoading: false }) {
    wrapper = shallowMount(SnippetBlobEdit, {
      propsData: {
        ...props,
      },
      data() {
        return {
          ...data,
        };
      },
    });
    flashSpy = jest.spyOn(wrapper.vm, 'flashAPIFailure');
  }

  beforeEach(() => {
    axiosMock = new AxiosMockAdapter(axios);
    createComponent();
  });

  afterEach(() => {
    axiosMock.restore();
    wrapper.destroy();
  });

  describe('rendering', () => {
    it('matches the snapshot', () => {
      createComponent({ blob });
      expect(wrapper.element).toMatchSnapshot();
    });

    it('renders required components', () => {
      expect(findComponent(BlobHeaderEdit).exists()).toBe(true);
      expect(findComponent(BlobContentEdit).exists()).toBe(true);
    });

    it('renders loader if existing blob is supplied but no content is fetched yet', () => {
      createComponent({ blob }, { isContentLoading: true });
      expect(wrapper.contains(GlLoadingIcon)).toBe(true);
      expect(findComponent(BlobContentEdit).exists()).toBe(false);
    });

    it('does not render loader if when blob is not supplied', () => {
      createComponent();
      expect(wrapper.contains(GlLoadingIcon)).toBe(false);
      expect(findComponent(BlobContentEdit).exists()).toBe(true);
    });
  });

  describe('functionality', () => {
    it('does not fail without blob', () => {
      const spy = jest.spyOn(global.console, 'error');
      createComponent({ blob: undefined });

      expect(spy).not.toHaveBeenCalled();
      expect(findComponent(BlobContentEdit).exists()).toBe(true);
    });

    it.each`
      emitter            | prop
      ${BlobHeaderEdit}  | ${'filePath'}
      ${BlobContentEdit} | ${'content'}
    `('emits "blob-updated" event when the $prop gets changed', ({ emitter, prop }) => {
      expect(wrapper.emitted('blob-updated')).toBeUndefined();
      const newValue = 'foo.bar';
      findComponent(emitter).vm.$emit('input', newValue);

      return nextTick().then(() => {
        expect(wrapper.emitted('blob-updated')[0]).toEqual([
          expect.objectContaining({
            [prop]: newValue,
          }),
        ]);
      });
    });

    describe('fetching blob content', () => {
      const bootstrapForExistingSnippet = resp => {
        createComponent({
          blob: {
            ...blob,
            content: '',
          },
        });

        if (resp === 500) {
          axiosMock.onGet('contentApiURL').reply(500);
        } else {
          axiosMock.onGet('contentApiURL').reply(200, contentMock);
        }
      };

      const bootstrapForNewSnippet = () => {
        createComponent();
      };

      it('fetches blob content with the additional query', () => {
        bootstrapForExistingSnippet();

        return waitForPromises().then(() => {
          expect(joinPaths).toHaveBeenCalledWith('foo/', rawPathMock);
          expect(findComponent(BlobHeaderEdit).props('value')).toBe(pathMock);
          expect(findComponent(BlobContentEdit).props('value')).toBe(contentMock);
        });
      });

      it('flashes the error message if fetching content fails', () => {
        bootstrapForExistingSnippet(500);

        return waitForPromises().then(() => {
          expect(flashSpy).toHaveBeenCalled();
          expect(findComponent(BlobContentEdit).props('value')).toBe('');
        });
      });

      it('does not fetch content for new snippet', () => {
        bootstrapForNewSnippet();

        return waitForPromises().then(() => {
          // we keep using waitForPromises to make sure we do not run failed test
          expect(findComponent(BlobHeaderEdit).props('value')).toBe('');
          expect(findComponent(BlobContentEdit).props('value')).toBe('');
          expect(joinPaths).not.toHaveBeenCalled();
        });
      });
    });
  });
});