2018-01-31 09:58:14 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2017-05-05 22:47:32 +00:00
|
|
|
import AjaxCache from '~/lib/utils/ajax_cache';
|
2021-02-14 18:09:20 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-05-05 22:47:32 +00:00
|
|
|
|
|
|
|
describe('AjaxCache', () => {
|
|
|
|
const dummyEndpoint = '/AjaxCache/dummyEndpoint';
|
|
|
|
const dummyResponse = {
|
|
|
|
important: 'dummy data',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-10-17 07:13:26 +00:00
|
|
|
AjaxCache.internalStorage = {};
|
|
|
|
AjaxCache.pendingRequests = {};
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
|
|
|
|
2017-05-10 22:01:00 +00:00
|
|
|
describe('get', () => {
|
2017-05-05 22:47:32 +00:00
|
|
|
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-05-10 22:01:00 +00:00
|
|
|
describe('hasData', () => {
|
2017-05-05 22:47:32 +00:00
|
|
|
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-05-10 22:01:00 +00:00
|
|
|
describe('remove', () => {
|
2017-05-05 22:47:32 +00:00
|
|
|
it('does nothing if cache is empty', () => {
|
2017-05-10 22:01:00 +00:00
|
|
|
AjaxCache.remove(dummyEndpoint);
|
2017-05-05 22:47:32 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(AjaxCache.internalStorage).toEqual({});
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing if cache contains no matching data', () => {
|
|
|
|
AjaxCache.internalStorage['not matching'] = dummyResponse;
|
|
|
|
|
2017-05-10 22:01:00 +00:00
|
|
|
AjaxCache.remove(dummyEndpoint);
|
2017-05-05 22:47:32 +00:00
|
|
|
|
|
|
|
expect(AjaxCache.internalStorage['not matching']).toBe(dummyResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes matching data', () => {
|
|
|
|
AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
|
|
|
|
|
2017-05-10 22:01:00 +00:00
|
|
|
AjaxCache.remove(dummyEndpoint);
|
2017-05-05 22:47:32 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(AjaxCache.internalStorage).toEqual({});
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-02 09:27:24 +00:00
|
|
|
describe('override', () => {
|
|
|
|
it('overrides existing cache', () => {
|
|
|
|
AjaxCache.internalStorage.endpoint = 'existing-endpoint';
|
|
|
|
AjaxCache.override('endpoint', 'new-endpoint');
|
|
|
|
|
|
|
|
expect(AjaxCache.internalStorage.endpoint).toEqual('new-endpoint');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-31 15:12:10 +00:00
|
|
|
describe('retrieve', () => {
|
2018-01-31 09:58:14 +00:00
|
|
|
let mock;
|
2017-05-10 22:01:00 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-01-31 09:58:14 +00:00
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
jest.spyOn(axios, 'get');
|
2018-01-31 09:58:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2017-05-10 22:01:00 +00:00
|
|
|
});
|
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
it('stores and returns data from Ajax call if cache is empty', () => {
|
2018-01-31 09:58:14 +00:00
|
|
|
mock.onGet(dummyEndpoint).reply(200, dummyResponse);
|
2017-05-10 22:01:00 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
return AjaxCache.retrieve(dummyEndpoint).then((data) => {
|
2018-12-20 16:49:44 +00:00
|
|
|
expect(data).toEqual(dummyResponse);
|
|
|
|
expect(AjaxCache.internalStorage[dummyEndpoint]).toEqual(dummyResponse);
|
|
|
|
});
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
it('makes no Ajax call if request is pending', () => {
|
2018-01-31 09:58:14 +00:00
|
|
|
mock.onGet(dummyEndpoint).reply(200, dummyResponse);
|
2017-05-10 22:01:00 +00:00
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
return Promise.all([
|
|
|
|
AjaxCache.retrieve(dummyEndpoint),
|
|
|
|
AjaxCache.retrieve(dummyEndpoint),
|
|
|
|
]).then(() => {
|
|
|
|
expect(axios.get).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2017-05-10 22:01:00 +00:00
|
|
|
});
|
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
it('returns undefined if Ajax call fails and cache is empty', () => {
|
2018-01-31 09:58:14 +00:00
|
|
|
const errorMessage = 'Network Error';
|
|
|
|
mock.onGet(dummyEndpoint).networkError();
|
2017-05-05 22:47:32 +00:00
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
expect.assertions(2);
|
2020-12-23 21:10:24 +00:00
|
|
|
return AjaxCache.retrieve(dummyEndpoint).catch((error) => {
|
2018-12-20 16:49:44 +00:00
|
|
|
expect(error.message).toBe(`${dummyEndpoint}: ${errorMessage}`);
|
|
|
|
expect(error.textStatus).toBe(errorMessage);
|
|
|
|
});
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
it('makes no Ajax call if matching data exists', () => {
|
2017-05-05 22:47:32 +00:00
|
|
|
AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
return AjaxCache.retrieve(dummyEndpoint).then((data) => {
|
2018-12-20 16:49:44 +00:00
|
|
|
expect(data).toBe(dummyResponse);
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
});
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
2017-06-05 09:12:15 +00:00
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
it('makes Ajax call even if matching data exists when forceRequest parameter is provided', () => {
|
2017-06-05 09:12:15 +00:00
|
|
|
const oldDummyResponse = {
|
|
|
|
important: 'old dummy data',
|
|
|
|
};
|
|
|
|
|
|
|
|
AjaxCache.internalStorage[dummyEndpoint] = oldDummyResponse;
|
|
|
|
|
2018-01-31 09:58:14 +00:00
|
|
|
mock.onGet(dummyEndpoint).reply(200, dummyResponse);
|
2017-06-05 09:12:15 +00:00
|
|
|
|
2018-12-20 16:49:44 +00:00
|
|
|
return Promise.all([
|
|
|
|
AjaxCache.retrieve(dummyEndpoint),
|
|
|
|
AjaxCache.retrieve(dummyEndpoint, true),
|
2020-12-23 21:10:24 +00:00
|
|
|
]).then((data) => {
|
2018-12-20 16:49:44 +00:00
|
|
|
expect(data).toEqual([oldDummyResponse, dummyResponse]);
|
|
|
|
});
|
2017-06-05 09:12:15 +00:00
|
|
|
});
|
2017-05-05 22:47:32 +00:00
|
|
|
});
|
|
|
|
});
|