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

ajax_cache_spec.js « utils « lib « javascripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7603400b55e9d5454a932d8d02b96cc85db3a28a (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
172
173
174
175
176
177
178
179
180
181
182
183
184
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import AjaxCache from '~/lib/utils/ajax_cache';

describe('AjaxCache', () => {
  const dummyEndpoint = '/AjaxCache/dummyEndpoint';
  const dummyResponse = {
    important: 'dummy data',
  };

  beforeEach(() => {
    AjaxCache.internalStorage = { };
    AjaxCache.pendingRequests = { };
  });

  describe('get', () => {
    it('returns undefined if cache is empty', () => {
      const data = AjaxCache.get(dummyEndpoint);

      expect(data).toBe(undefined);
    });

    it('returns undefined if cache contains no matching data', () => {
      AjaxCache.internalStorage['not matching'] = dummyResponse;

      const data = AjaxCache.get(dummyEndpoint);

      expect(data).toBe(undefined);
    });

    it('returns matching data', () => {
      AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;

      const data = AjaxCache.get(dummyEndpoint);

      expect(data).toBe(dummyResponse);
    });
  });

  describe('hasData', () => {
    it('returns false if cache is empty', () => {
      expect(AjaxCache.hasData(dummyEndpoint)).toBe(false);
    });

    it('returns false if cache contains no matching data', () => {
      AjaxCache.internalStorage['not matching'] = dummyResponse;

      expect(AjaxCache.hasData(dummyEndpoint)).toBe(false);
    });

    it('returns true if data is available', () => {
      AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;

      expect(AjaxCache.hasData(dummyEndpoint)).toBe(true);
    });
  });

  describe('remove', () => {
    it('does nothing if cache is empty', () => {
      AjaxCache.remove(dummyEndpoint);

      expect(AjaxCache.internalStorage).toEqual({ });
    });

    it('does nothing if cache contains no matching data', () => {
      AjaxCache.internalStorage['not matching'] = dummyResponse;

      AjaxCache.remove(dummyEndpoint);

      expect(AjaxCache.internalStorage['not matching']).toBe(dummyResponse);
    });

    it('removes matching data', () => {
      AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;

      AjaxCache.remove(dummyEndpoint);

      expect(AjaxCache.internalStorage).toEqual({ });
    });
  });

  describe('override', () => {
    it('overrides existing cache', () => {
      AjaxCache.internalStorage.endpoint = 'existing-endpoint';
      AjaxCache.override('endpoint', 'new-endpoint');

      expect(AjaxCache.internalStorage.endpoint).toEqual('new-endpoint');
    });
  });

  describe('retrieve', () => {
    let mock;

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

      spyOn(axios, 'get').and.callThrough();
    });

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

    it('stores and returns data from Ajax call if cache is empty', (done) => {
      mock.onGet(dummyEndpoint).reply(200, dummyResponse);

      AjaxCache.retrieve(dummyEndpoint)
      .then((data) => {
        expect(data).toEqual(dummyResponse);
        expect(AjaxCache.internalStorage[dummyEndpoint]).toEqual(dummyResponse);
      })
      .then(done)
      .catch(fail);
    });

    it('makes no Ajax call if request is pending', (done) => {
      mock.onGet(dummyEndpoint).reply(200, dummyResponse);

      AjaxCache.retrieve(dummyEndpoint)
      .then(done)
      .catch(fail);

      AjaxCache.retrieve(dummyEndpoint)
      .then(done)
      .catch(fail);

      expect(axios.get.calls.count()).toBe(1);
    });

    it('returns undefined if Ajax call fails and cache is empty', (done) => {
      const errorMessage = 'Network Error';
      mock.onGet(dummyEndpoint).networkError();

      AjaxCache.retrieve(dummyEndpoint)
      .then(data => fail(`Received unexpected data: ${JSON.stringify(data)}`))
      .catch((error) => {
        expect(error.message).toBe(`${dummyEndpoint}: ${errorMessage}`);
        expect(error.textStatus).toBe(errorMessage);
        done();
      })
      .catch(fail);
    });

    it('makes no Ajax call if matching data exists', (done) => {
      AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
      mock.onGet(dummyEndpoint).reply(() => {
        fail(new Error('expected no Ajax call!'));
      });

      AjaxCache.retrieve(dummyEndpoint)
      .then((data) => {
        expect(data).toBe(dummyResponse);
      })
      .then(done)
      .catch(fail);
    });

    it('makes Ajax call even if matching data exists when forceRequest parameter is provided', (done) => {
      const oldDummyResponse = {
        important: 'old dummy data',
      };

      AjaxCache.internalStorage[dummyEndpoint] = oldDummyResponse;

      mock.onGet(dummyEndpoint).reply(200, dummyResponse);

      // Call without forceRetrieve param
      AjaxCache.retrieve(dummyEndpoint)
        .then((data) => {
          expect(data).toBe(oldDummyResponse);
        })
        .then(done)
        .catch(fail);

      // Call with forceRetrieve param
      AjaxCache.retrieve(dummyEndpoint, true)
        .then((data) => {
          expect(data).toEqual(dummyResponse);
        })
        .then(done)
        .catch(fail);
    });
  });
});