2018-12-18 21:59:50 +03:00
|
|
|
import ServersService from '../../../src/servers/services/ServersService';
|
2018-08-24 11:58:37 +03:00
|
|
|
|
|
|
|
describe('ServersService', () => {
|
|
|
|
const servers = {
|
|
|
|
abc123: { id: 'abc123' },
|
|
|
|
def456: { id: 'def456' },
|
|
|
|
};
|
2018-08-26 00:39:27 +03:00
|
|
|
const createStorageMock = (returnValue) => ({
|
2019-04-19 11:29:49 +03:00
|
|
|
set: jest.fn(),
|
|
|
|
get: jest.fn(() => returnValue),
|
2018-08-24 11:58:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('listServers', () => {
|
|
|
|
it('returns an empty object when servers are not found in storage', () => {
|
|
|
|
const storageMock = createStorageMock();
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
const result = service.listServers();
|
|
|
|
|
|
|
|
expect(result).toEqual({});
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).not.toHaveBeenCalled();
|
2018-08-24 11:58:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns value from storage when found', () => {
|
|
|
|
const storageMock = createStorageMock(servers);
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
const result = service.listServers();
|
|
|
|
|
|
|
|
expect(result).toEqual(servers);
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).not.toHaveBeenCalled();
|
2018-08-24 11:58:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findServerById', () => {
|
|
|
|
it('returns undefined when requested server is not found', () => {
|
|
|
|
const storageMock = createStorageMock(servers);
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
const result = service.findServerById('ghi789');
|
|
|
|
|
|
|
|
expect(result).toBeUndefined();
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).not.toHaveBeenCalled();
|
2018-08-24 11:58:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns server from list when found', () => {
|
|
|
|
const storageMock = createStorageMock(servers);
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
const result = service.findServerById('abc123');
|
|
|
|
|
|
|
|
expect(result).toEqual({ id: 'abc123' });
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).not.toHaveBeenCalled();
|
2018-08-24 11:58:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createServer', () => {
|
|
|
|
it('adds one server to the list', () => {
|
|
|
|
const storageMock = createStorageMock(servers);
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
service.createServer({ id: 'ghi789' });
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
|
2018-08-24 11:58:37 +03:00
|
|
|
abc123: { id: 'abc123' },
|
|
|
|
def456: { id: 'def456' },
|
|
|
|
ghi789: { id: 'ghi789' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createServers', () => {
|
|
|
|
it('adds multiple servers to the list', () => {
|
|
|
|
const storageMock = createStorageMock(servers);
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]);
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
|
2018-08-24 11:58:37 +03:00
|
|
|
abc123: { id: 'abc123' },
|
|
|
|
def456: { id: 'def456' },
|
|
|
|
ghi789: { id: 'ghi789' },
|
|
|
|
jkl123: { id: 'jkl123' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deleteServer', () => {
|
|
|
|
it('removes one server from the list', () => {
|
|
|
|
const storageMock = createStorageMock(servers);
|
|
|
|
const service = new ServersService(storageMock);
|
|
|
|
|
|
|
|
service.deleteServer({ id: 'abc123' });
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
|
|
|
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
|
2018-08-24 11:58:37 +03:00
|
|
|
def456: { id: 'def456' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|