summaryrefslogtreecommitdiff
path: root/spec/javascripts/repo/services/repo_service_spec.js
blob: 6f530770525b4761d48909346a314e27c84e541d (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
166
167
168
169
170
171
import axios from 'axios';
import RepoService from '~/repo/services/repo_service';
import RepoStore from '~/repo/stores/repo_store';
import Api from '~/api';

describe('RepoService', () => {
  it('has default json format param', () => {
    expect(RepoService.options.params.format).toBe('json');
  });

  describe('buildParams', () => {
    let newParams;
    const url = 'url';

    beforeEach(() => {
      newParams = {};

      spyOn(Object, 'assign').and.returnValue(newParams);
    });

    it('clones params', () => {
      const params = RepoService.buildParams(url);

      expect(Object.assign).toHaveBeenCalledWith({}, RepoService.options.params);

      expect(params).toBe(newParams);
    });

    it('sets and returns viewer params to richif urlIsRichBlob is true', () => {
      spyOn(RepoService, 'urlIsRichBlob').and.returnValue(true);

      const params = RepoService.buildParams(url);

      expect(params.viewer).toEqual('rich');
    });

    it('returns params urlIsRichBlob is false', () => {
      spyOn(RepoService, 'urlIsRichBlob').and.returnValue(false);

      const params = RepoService.buildParams(url);

      expect(params.viewer).toBeUndefined();
    });

    it('calls urlIsRichBlob with the objects url prop if no url arg is provided', () => {
      spyOn(RepoService, 'urlIsRichBlob');
      RepoService.url = url;

      RepoService.buildParams();

      expect(RepoService.urlIsRichBlob).toHaveBeenCalledWith(url);
    });
  });

  describe('urlIsRichBlob', () => {
    it('returns true for md extension', () => {
      const isRichBlob = RepoService.urlIsRichBlob('url.md');

      expect(isRichBlob).toBeTruthy();
    });

    it('returns false for js extension', () => {
      const isRichBlob = RepoService.urlIsRichBlob('url.js');

      expect(isRichBlob).toBeFalsy();
    });
  });

  describe('getContent', () => {
    const params = {};
    const url = 'url';
    const requestPromise = Promise.resolve();

    beforeEach(() => {
      spyOn(RepoService, 'buildParams').and.returnValue(params);
      spyOn(axios, 'get').and.returnValue(requestPromise);
    });

    it('calls buildParams and axios.get', () => {
      const request = RepoService.getContent(url);

      expect(RepoService.buildParams).toHaveBeenCalledWith(url);
      expect(axios.get).toHaveBeenCalledWith(url, {
        params,
      });
      expect(request).toBe(requestPromise);
    });

    it('uses object url prop if no url arg is provided', () => {
      RepoService.url = url;

      RepoService.getContent();

      expect(axios.get).toHaveBeenCalledWith(url, {
        params,
      });
    });
  });

  describe('getBase64Content', () => {
    const url = 'url';
    const response = { data: 'data' };

    beforeEach(() => {
      spyOn(RepoService, 'bufferToBase64');
      spyOn(axios, 'get').and.returnValue(Promise.resolve(response));
    });

    it('calls axios.get and bufferToBase64 on completion', (done) => {
      const request = RepoService.getBase64Content(url);

      expect(axios.get).toHaveBeenCalledWith(url, {
        responseType: 'arraybuffer',
      });
      expect(request).toEqual(jasmine.any(Promise));

      request.then(() => {
        expect(RepoService.bufferToBase64).toHaveBeenCalledWith(response.data);
        done();
      }).catch(done.fail);
    });
  });

  describe('commitFiles', () => {
    it('calls commitMultiple and .then commitFlash', (done) => {
      const projectId = 'projectId';
      const payload = {};
      RepoStore.projectId = projectId;

      spyOn(Api, 'commitMultiple').and.returnValue(Promise.resolve());
      spyOn(RepoService, 'commitFlash');

      const apiPromise = RepoService.commitFiles(payload);

      expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, payload);

      apiPromise.then(() => {
        expect(RepoService.commitFlash).toHaveBeenCalled();
        done();
      }).catch(done.fail);
    });
  });

  describe('commitFlash', () => {
    it('calls Flash with data.message', () => {
      const data = {
        message: 'message',
      };
      spyOn(window, 'Flash');

      RepoService.commitFlash(data);

      expect(window.Flash).toHaveBeenCalledWith(data.message);
    });

    it('calls Flash with success string if short_id and stats', () => {
      const data = {
        short_id: 'short_id',
        stats: {
          additions: '4',
          deletions: '5',
        },
      };
      spyOn(window, 'Flash');

      RepoService.commitFlash(data);

      expect(window.Flash).toHaveBeenCalledWith(`Your changes have been committed. Commit ${data.short_id} with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`, 'notice');
    });
  });
});