1
0
mirror of https://github.com/pavlobu/deskreen.git synced 2025-05-16 15:30:20 -07:00
deskreen/app/features/SharingSessionsService/SharingSessionService.spec.ts
Pavlo Buidenkov b925803d9f better client UI code
huge work done on sharing desktop session
2020-11-22 17:07:01 +02:00

111 lines
3.7 KiB
TypeScript

import SharingSessionStatusEnum from './SharingSessionStatusEnum';
import SharingSession from './SharingSession';
import SharingSessionService from '.';
import RoomIDService from '../../server/RoomIDService';
import ConnectedDevicesService from '../ConnectedDevicesService';
import PeerConnectionHelperRendererService from '../PeerConnectionHelperRendererService';
// this may look as an ugly mock, but hey, this works! and don't forget that it is a unit test
// why do we make it like that ? bacuse jest doesnt allow ex.
// duplicated __mock__/electron in different subfolders of the project, so.. better do mainual mock in a test file itself
// jest bug reference on duplicated mocks found: https://github.com/facebook/jest/issues/2070
// it is a bad design of jest itself by default, so this is the best workaround, simply by making manual mock in this way:
jest.mock('../PeerConnectionHelperRendererService', () => {
return jest.fn().mockImplementation(() => {
return {
createPeerConnectionHelper: () => {
return {
webContents: {
on: () => {},
toggleDevTools: () => {},
},
};
},
};
});
});
jest.useFakeTimers();
describe('SharingSessionService unit tests', () => {
let sharingSessionService: SharingSessionService;
beforeEach(() => {
sharingSessionService = new SharingSessionService(
new RoomIDService(),
new ConnectedDevicesService(),
new PeerConnectionHelperRendererService('')
);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('when new SharingSessionService() is created', () => {
it('should have empty sharingSessions Map', () => {
expect(sharingSessionService.sharingSessions.size).toBe(0);
});
it('should have waitingForConnectionSharingSession set to null', () => {
expect(sharingSessionService.waitingForConnectionSharingSession).toBe(
null
);
});
it('should have pollForInactiveSessions be called', () => {
const backup = SharingSessionService.prototype.pollForInactiveSessions;
const mockPollForInactiveSessions = jest.fn();
jest
.spyOn(SharingSessionService.prototype, 'pollForInactiveSessions')
.mockImplementation(mockPollForInactiveSessions);
// eslint-disable-next-line no-new
new SharingSessionService(
new RoomIDService(),
new ConnectedDevicesService(),
new PeerConnectionHelperRendererService('')
);
jest.advanceTimersByTime(1000 * 60 * 60 * 30); // thirty hours later
expect(mockPollForInactiveSessions).toBeCalled();
SharingSessionService.prototype.pollForInactiveSessions = backup;
});
});
describe('when createNewSharingSession is called', () => {
it('should have sharingSessions Map with size equal to 1', async () => {
await sharingSessionService.createNewSharingSession('');
expect(sharingSessionService.sharingSessions.size).toBe(1);
});
it('should have returned SharingSession object', async () => {
expect(sharingSessionService.createNewSharingSession('')).toBeInstanceOf(
SharingSession
);
const sharingSession = await sharingSessionService.createNewSharingSession(
''
);
expect(sharingSession).toBeInstanceOf(SharingSession);
});
});
describe('when pollForInactiveSessions is called', () => {
it('should have removed SharingSession with status ERROR from sharingSessions Map', async () => {
const testSharingSession = await sharingSessionService.createNewSharingSession(
''
);
testSharingSession.status = SharingSessionStatusEnum.ERROR;
sharingSessionService.pollForInactiveSessions();
expect(sharingSessionService.sharingSessions.size).toBe(0);
});
});
});