2017-05-05 18:47:32 -04:00
|
|
|
import AjaxCache from '~/lib/utils/ajax_cache';
|
|
|
|
|
|
|
|
describe('AjaxCache', () => {
|
|
|
|
const dummyEndpoint = '/AjaxCache/dummyEndpoint';
|
|
|
|
const dummyResponse = {
|
|
|
|
important: 'dummy data',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
AjaxCache.internalStorage = { };
|
2017-05-10 18:01:00 -04:00
|
|
|
AjaxCache.pendingRequests = { };
|
2017-05-05 18:47:32 -04:00
|
|
|
});
|
|
|
|
|
2017-05-10 18:01:00 -04:00
|
|
|
describe('get', () => {
|
2017-05-05 18:47:32 -04: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 18:01:00 -04:00
|
|
|
describe('hasData', () => {
|
2017-05-05 18:47:32 -04: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 18:01:00 -04:00
|
|
|
describe('remove', () => {
|
2017-05-05 18:47:32 -04:00
|
|
|
it('does nothing if cache is empty', () => {
|
2017-05-10 18:01:00 -04:00
|
|
|
AjaxCache.remove(dummyEndpoint);
|
2017-05-05 18:47:32 -04:00
|
|
|
|
|
|
|
expect(AjaxCache.internalStorage).toEqual({ });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing if cache contains no matching data', () => {
|
|
|
|
AjaxCache.internalStorage['not matching'] = dummyResponse;
|
|
|
|
|
2017-05-10 18:01:00 -04:00
|
|
|
AjaxCache.remove(dummyEndpoint);
|
2017-05-05 18:47:32 -04:00
|
|
|
|
|
|
|
expect(AjaxCache.internalStorage['not matching']).toBe(dummyResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes matching data', () => {
|
|
|
|
AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
|
|
|
|
|
2017-05-10 18:01:00 -04:00
|
|
|
AjaxCache.remove(dummyEndpoint);
|
2017-05-05 18:47:32 -04:00
|
|
|
|
|
|
|
expect(AjaxCache.internalStorage).toEqual({ });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-02 05:27:24 -04:00
|
|
|
describe('override', () => {
|
|
|
|
it('overrides existing cache', () => {
|
|
|
|
AjaxCache.internalStorage.endpoint = 'existing-endpoint';
|
|
|
|
AjaxCache.override('endpoint', 'new-endpoint');
|
|
|
|
|
|
|
|
expect(AjaxCache.internalStorage.endpoint).toEqual('new-endpoint');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-10 18:01:00 -04:00
|
|
|
describe('retrieve', () => {
|
|
|
|
let ajaxSpy;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(jQuery, 'ajax').and.callFake(url => ajaxSpy(url));
|
|
|
|
});
|
|
|
|
|
2017-05-05 18:47:32 -04:00
|
|
|
it('stores and returns data from Ajax call if cache is empty', (done) => {
|
2017-05-10 18:01:00 -04:00
|
|
|
ajaxSpy = (url) => {
|
|
|
|
expect(url).toBe(dummyEndpoint);
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
deferred.resolve(dummyResponse);
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
2017-05-05 18:47:32 -04:00
|
|
|
AjaxCache.retrieve(dummyEndpoint)
|
|
|
|
.then((data) => {
|
|
|
|
expect(data).toBe(dummyResponse);
|
|
|
|
expect(AjaxCache.internalStorage[dummyEndpoint]).toBe(dummyResponse);
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(fail);
|
|
|
|
});
|
|
|
|
|
2017-05-10 18:01:00 -04:00
|
|
|
it('makes no Ajax call if request is pending', () => {
|
|
|
|
const responseDeferred = $.Deferred();
|
|
|
|
|
|
|
|
ajaxSpy = (url) => {
|
|
|
|
expect(url).toBe(dummyEndpoint);
|
|
|
|
// neither reject nor resolve to keep request pending
|
|
|
|
return responseDeferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
const unexpectedResponse = data => fail(`Did not expect response: ${data}`);
|
|
|
|
|
|
|
|
AjaxCache.retrieve(dummyEndpoint)
|
|
|
|
.then(unexpectedResponse)
|
|
|
|
.catch(fail);
|
|
|
|
|
|
|
|
AjaxCache.retrieve(dummyEndpoint)
|
|
|
|
.then(unexpectedResponse)
|
|
|
|
.catch(fail);
|
|
|
|
|
|
|
|
expect($.ajax.calls.count()).toBe(1);
|
|
|
|
});
|
|
|
|
|
2017-05-05 18:47:32 -04:00
|
|
|
it('returns undefined if Ajax call fails and cache is empty', (done) => {
|
|
|
|
const dummyStatusText = 'exploded';
|
|
|
|
const dummyErrorMessage = 'server exploded';
|
|
|
|
ajaxSpy = (url) => {
|
|
|
|
expect(url).toBe(dummyEndpoint);
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
deferred.reject(null, dummyStatusText, dummyErrorMessage);
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
AjaxCache.retrieve(dummyEndpoint)
|
|
|
|
.then(data => fail(`Received unexpected data: ${JSON.stringify(data)}`))
|
|
|
|
.catch((error) => {
|
|
|
|
expect(error.message).toBe(`${dummyEndpoint}: ${dummyErrorMessage}`);
|
|
|
|
expect(error.textStatus).toBe(dummyStatusText);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(fail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('makes no Ajax call if matching data exists', (done) => {
|
|
|
|
AjaxCache.internalStorage[dummyEndpoint] = dummyResponse;
|
|
|
|
ajaxSpy = () => fail(new Error('expected no Ajax call!'));
|
|
|
|
|
|
|
|
AjaxCache.retrieve(dummyEndpoint)
|
|
|
|
.then((data) => {
|
|
|
|
expect(data).toBe(dummyResponse);
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(fail);
|
|
|
|
});
|
2017-06-05 05:12:15 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ajaxSpy = (url) => {
|
|
|
|
expect(url).toBe(dummyEndpoint);
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
deferred.resolve(dummyResponse);
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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).toBe(dummyResponse);
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(fail);
|
|
|
|
});
|
2017-05-05 18:47:32 -04:00
|
|
|
});
|
|
|
|
});
|