debian-mirror-gitlab/spec/javascripts/lib/utils/ajax_cache_spec.js

185 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
2017-08-17 22:00:37 +05:30
import AjaxCache from '~/lib/utils/ajax_cache';
describe('AjaxCache', () => {
const dummyEndpoint = '/AjaxCache/dummyEndpoint';
const dummyResponse = {
important: 'dummy data',
};
beforeEach(() => {
AjaxCache.internalStorage = { };
2017-09-10 17:25:29 +05:30
AjaxCache.pendingRequests = { };
2017-08-17 22:00:37 +05:30
});
2017-09-10 17:25:29 +05:30
describe('get', () => {
2017-08-17 22:00:37 +05:30
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);
});
});
2017-09-10 17:25:29 +05:30
describe('hasData', () => {
2017-08-17 22:00:37 +05:30
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);
});
});
2017-09-10 17:25:29 +05:30
describe('remove', () => {
2017-08-17 22:00:37 +05:30
it('does nothing if cache is empty', () => {
2017-09-10 17:25:29 +05:30
AjaxCache.remove(dummyEndpoint);
2017-08-17 22:00:37 +05:30
expect(AjaxCache.internalStorage).toEqual({ });
});
it('does nothing if cache contains no matching data', () => {
AjaxCache.internalStorage['not matching'] = dummyResponse;
2017-09-10 17:25:29 +05:30
AjaxCache.remove(dummyEndpoint);
2017-08-17 22:00:37 +05:30
expect(AjaxCache.internalStorage['not matching']).toBe(dummyResponse);
});
it('removes matching data', () => {
AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
2017-09-10 17:25:29 +05:30
AjaxCache.remove(dummyEndpoint);
2017-08-17 22:00:37 +05:30
expect(AjaxCache.internalStorage).toEqual({ });
});
});
2017-09-10 17:25:29 +05:30
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', () => {
2018-03-17 18:26:18 +05:30
let mock;
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
spyOn(axios, 'get').and.callThrough();
});
afterEach(() => {
mock.restore();
2017-09-10 17:25:29 +05:30
});
2017-08-17 22:00:37 +05:30
it('stores and returns data from Ajax call if cache is empty', (done) => {
2018-03-17 18:26:18 +05:30
mock.onGet(dummyEndpoint).reply(200, dummyResponse);
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
AjaxCache.retrieve(dummyEndpoint)
.then((data) => {
2018-03-17 18:26:18 +05:30
expect(data).toEqual(dummyResponse);
expect(AjaxCache.internalStorage[dummyEndpoint]).toEqual(dummyResponse);
2017-08-17 22:00:37 +05:30
})
.then(done)
.catch(fail);
});
2018-03-17 18:26:18 +05:30
it('makes no Ajax call if request is pending', (done) => {
mock.onGet(dummyEndpoint).reply(200, dummyResponse);
2017-09-10 17:25:29 +05:30
AjaxCache.retrieve(dummyEndpoint)
2018-03-17 18:26:18 +05:30
.then(done)
2017-09-10 17:25:29 +05:30
.catch(fail);
AjaxCache.retrieve(dummyEndpoint)
2018-03-17 18:26:18 +05:30
.then(done)
2017-09-10 17:25:29 +05:30
.catch(fail);
2018-03-17 18:26:18 +05:30
expect(axios.get.calls.count()).toBe(1);
2017-09-10 17:25:29 +05:30
});
2017-08-17 22:00:37 +05:30
it('returns undefined if Ajax call fails and cache is empty', (done) => {
2018-03-17 18:26:18 +05:30
const errorMessage = 'Network Error';
mock.onGet(dummyEndpoint).networkError();
2017-08-17 22:00:37 +05:30
AjaxCache.retrieve(dummyEndpoint)
.then(data => fail(`Received unexpected data: ${JSON.stringify(data)}`))
.catch((error) => {
2018-03-17 18:26:18 +05:30
expect(error.message).toBe(`${dummyEndpoint}: ${errorMessage}`);
expect(error.textStatus).toBe(errorMessage);
2017-08-17 22:00:37 +05:30
done();
})
.catch(fail);
});
it('makes no Ajax call if matching data exists', (done) => {
AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
2018-03-17 18:26:18 +05:30
mock.onGet(dummyEndpoint).reply(() => {
fail(new Error('expected no Ajax call!'));
});
2017-08-17 22:00:37 +05:30
AjaxCache.retrieve(dummyEndpoint)
.then((data) => {
expect(data).toBe(dummyResponse);
})
.then(done)
.catch(fail);
});
2017-09-10 17:25:29 +05:30
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;
2018-03-17 18:26:18 +05:30
mock.onGet(dummyEndpoint).reply(200, dummyResponse);
2017-09-10 17:25:29 +05:30
// 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) => {
2018-03-17 18:26:18 +05:30
expect(data).toEqual(dummyResponse);
2017-09-10 17:25:29 +05:30
})
.then(done)
.catch(fail);
});
2017-08-17 22:00:37 +05:30
});
});