debian-mirror-gitlab/spec/frontend/lib/utils/users_cache_spec.js

266 lines
6.9 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Api from '~/api';
import UsersCache from '~/lib/utils/users_cache';
describe('UsersCache', () => {
const dummyUsername = 'win';
2019-02-15 15:39:39 +05:30
const dummyUserId = 123;
2020-01-01 13:55:28 +05:30
const dummyUser = {
name: 'has a farm',
username: 'farmer',
};
2019-02-15 15:39:39 +05:30
const dummyUserStatus = 'my status';
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2018-12-13 13:39:08 +05:30
UsersCache.internalStorage = {};
2017-09-10 17:25:29 +05:30
});
describe('get', () => {
it('returns undefined for empty cache', () => {
2018-12-13 13:39:08 +05:30
expect(UsersCache.internalStorage).toEqual({});
2017-09-10 17:25:29 +05:30
const user = UsersCache.get(dummyUsername);
expect(user).toBe(undefined);
});
it('returns undefined for missing user', () => {
UsersCache.internalStorage['no body'] = 'no data';
const user = UsersCache.get(dummyUsername);
expect(user).toBe(undefined);
});
it('returns matching user', () => {
UsersCache.internalStorage[dummyUsername] = dummyUser;
const user = UsersCache.get(dummyUsername);
expect(user).toBe(dummyUser);
});
});
describe('hasData', () => {
it('returns false for empty cache', () => {
2018-12-13 13:39:08 +05:30
expect(UsersCache.internalStorage).toEqual({});
2017-09-10 17:25:29 +05:30
expect(UsersCache.hasData(dummyUsername)).toBe(false);
});
it('returns false for missing user', () => {
UsersCache.internalStorage['no body'] = 'no data';
expect(UsersCache.hasData(dummyUsername)).toBe(false);
});
it('returns true for matching user', () => {
UsersCache.internalStorage[dummyUsername] = dummyUser;
expect(UsersCache.hasData(dummyUsername)).toBe(true);
});
});
describe('remove', () => {
it('does nothing if cache is empty', () => {
2018-12-13 13:39:08 +05:30
expect(UsersCache.internalStorage).toEqual({});
2017-09-10 17:25:29 +05:30
UsersCache.remove(dummyUsername);
2018-12-13 13:39:08 +05:30
expect(UsersCache.internalStorage).toEqual({});
2017-09-10 17:25:29 +05:30
});
it('does nothing if cache contains no matching data', () => {
UsersCache.internalStorage['no body'] = 'no data';
UsersCache.remove(dummyUsername);
expect(UsersCache.internalStorage['no body']).toBe('no data');
});
it('removes matching data', () => {
UsersCache.internalStorage[dummyUsername] = dummyUser;
UsersCache.remove(dummyUsername);
2018-12-13 13:39:08 +05:30
expect(UsersCache.internalStorage).toEqual({});
2017-09-10 17:25:29 +05:30
});
});
describe('retrieve', () => {
let apiSpy;
beforeEach(() => {
2020-01-01 13:55:28 +05:30
jest.spyOn(Api, 'users').mockImplementation((query, options) => apiSpy(query, options));
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
it('stores and returns data from API call if cache is empty', done => {
2017-09-10 17:25:29 +05:30
apiSpy = (query, options) => {
expect(query).toBe('');
2020-01-01 13:55:28 +05:30
expect(options).toEqual({
username: dummyUsername,
});
2018-03-17 18:26:18 +05:30
return Promise.resolve({
data: [dummyUser],
});
2017-09-10 17:25:29 +05:30
};
UsersCache.retrieve(dummyUsername)
2018-12-13 13:39:08 +05:30
.then(user => {
expect(user).toBe(dummyUser);
expect(UsersCache.internalStorage[dummyUsername]).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
it('returns undefined if Ajax call fails and cache is empty', done => {
2017-09-10 17:25:29 +05:30
const dummyError = new Error('server exploded');
2020-01-01 13:55:28 +05:30
2017-09-10 17:25:29 +05:30
apiSpy = (query, options) => {
expect(query).toBe('');
2020-01-01 13:55:28 +05:30
expect(options).toEqual({
username: dummyUsername,
});
2017-09-10 17:25:29 +05:30
return Promise.reject(dummyError);
};
UsersCache.retrieve(dummyUsername)
2020-01-01 13:55:28 +05:30
.then(user => done.fail(`Received unexpected user: ${JSON.stringify(user)}`))
2018-12-13 13:39:08 +05:30
.catch(error => {
expect(error).toBe(dummyError);
})
.then(done)
.catch(done.fail);
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
it('makes no Ajax call if matching data exists', done => {
2017-09-10 17:25:29 +05:30
UsersCache.internalStorage[dummyUsername] = dummyUser;
2020-01-01 13:55:28 +05:30
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
2017-09-10 17:25:29 +05:30
UsersCache.retrieve(dummyUsername)
2018-12-13 13:39:08 +05:30
.then(user => {
expect(user).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
2017-09-10 17:25:29 +05:30
});
});
2019-02-15 15:39:39 +05:30
describe('retrieveById', () => {
let apiSpy;
beforeEach(() => {
2020-01-01 13:55:28 +05:30
jest.spyOn(Api, 'user').mockImplementation(id => apiSpy(id));
2019-02-15 15:39:39 +05:30
});
it('stores and returns data from API call if cache is empty', done => {
apiSpy = id => {
expect(id).toBe(dummyUserId);
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
return Promise.resolve({
data: dummyUser,
});
};
UsersCache.retrieveById(dummyUserId)
.then(user => {
expect(user).toBe(dummyUser);
expect(UsersCache.internalStorage[dummyUserId]).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
});
it('returns undefined if Ajax call fails and cache is empty', done => {
const dummyError = new Error('server exploded');
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
apiSpy = id => {
expect(id).toBe(dummyUserId);
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
return Promise.reject(dummyError);
};
UsersCache.retrieveById(dummyUserId)
2020-01-01 13:55:28 +05:30
.then(user => done.fail(`Received unexpected user: ${JSON.stringify(user)}`))
2019-02-15 15:39:39 +05:30
.catch(error => {
expect(error).toBe(dummyError);
})
.then(done)
.catch(done.fail);
});
it('makes no Ajax call if matching data exists', done => {
UsersCache.internalStorage[dummyUserId] = dummyUser;
2020-01-01 13:55:28 +05:30
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
2019-02-15 15:39:39 +05:30
UsersCache.retrieveById(dummyUserId)
.then(user => {
expect(user).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
});
});
describe('retrieveStatusById', () => {
let apiSpy;
beforeEach(() => {
2020-01-01 13:55:28 +05:30
jest.spyOn(Api, 'userStatus').mockImplementation(id => apiSpy(id));
2019-02-15 15:39:39 +05:30
});
it('stores and returns data from API call if cache is empty', done => {
apiSpy = id => {
expect(id).toBe(dummyUserId);
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
return Promise.resolve({
data: dummyUserStatus,
});
};
UsersCache.retrieveStatusById(dummyUserId)
.then(userStatus => {
expect(userStatus).toBe(dummyUserStatus);
expect(UsersCache.internalStorage[dummyUserId].status).toBe(dummyUserStatus);
})
.then(done)
.catch(done.fail);
});
it('returns undefined if Ajax call fails and cache is empty', done => {
const dummyError = new Error('server exploded');
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
apiSpy = id => {
expect(id).toBe(dummyUserId);
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
return Promise.reject(dummyError);
};
UsersCache.retrieveStatusById(dummyUserId)
2020-01-01 13:55:28 +05:30
.then(userStatus => done.fail(`Received unexpected user: ${JSON.stringify(userStatus)}`))
2019-02-15 15:39:39 +05:30
.catch(error => {
expect(error).toBe(dummyError);
})
.then(done)
.catch(done.fail);
});
it('makes no Ajax call if matching data exists', done => {
2020-01-01 13:55:28 +05:30
UsersCache.internalStorage[dummyUserId] = {
status: dummyUserStatus,
};
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
2019-02-15 15:39:39 +05:30
UsersCache.retrieveStatusById(dummyUserId)
.then(userStatus => {
expect(userStatus).toBe(dummyUserStatus);
})
.then(done)
.catch(done.fail);
});
});
2017-09-10 17:25:29 +05:30
});