Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/services/sockets/socket.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ describe('RealtimeService', () => {

test.each([
{ isProduction: true, reconnection: true, withCredentials: true, logs: false },
{ isProduction: false, reconnection: true, withCredentials: false, logs: true },
{ isProduction: false, reconnection: false, withCredentials: false, logs: true },
])(
'When running in isProduction=$isProduction environment, then it adjusts reconnection=$reconnection, withCredentials=$withCredentials and logging=$logs',
({ isProduction, reconnection, withCredentials, logs }) => {
vi.spyOn(envService, 'isProduction').mockReturnValue(isProduction);
(RealtimeService as unknown as { instance: RealtimeService | undefined }).instance = undefined;
service = RealtimeService.getInstance();

service.init();

Expand All @@ -118,6 +120,64 @@ describe('RealtimeService', () => {
);
});

describe('Logging behavior', () => {
const resetServiceWithProduction = (isProduction: boolean) => {
vi.spyOn(envService, 'isProduction').mockReturnValue(isProduction);
(RealtimeService as unknown as { instance: RealtimeService | undefined }).instance = undefined;
return RealtimeService.getInstance();
};

test('When not in production, then it logs on connect event', () => {
service = resetServiceWithProduction(false);
service.init();
const connectHandler = mockSocket.on.mock.calls.find((call) => call[0] === 'connect')?.[1];
connectHandler?.();
expect(consoleLogSpy).toHaveBeenCalledWith('[REALTIME]: CONNECTED WITH ID', mockSocket.id);
});

test('When in production, then it does not log on connect event', () => {
service = resetServiceWithProduction(true);
service.init();
const connectHandler = mockSocket.on.mock.calls.find((call) => call[0] === 'connect')?.[1];
connectHandler?.();
expect(consoleLogSpy).not.toHaveBeenCalledWith('[REALTIME]: CONNECTED WITH ID', mockSocket.id);
});

test('When not in production, then it logs on disconnect event', () => {
service = resetServiceWithProduction(false);
service.init();
const disconnectHandler = mockSocket.on.mock.calls.find((call) => call[0] === 'disconnect')?.[1];
disconnectHandler?.('transport close');
expect(consoleLogSpy).toHaveBeenCalledWith('[REALTIME] DISCONNECTED:', 'transport close');
});

test('When in production, then it does not log on disconnect event', () => {
service = resetServiceWithProduction(true);
service.init();
const disconnectHandler = mockSocket.on.mock.calls.find((call) => call[0] === 'disconnect')?.[1];
disconnectHandler?.('transport close');
expect(consoleLogSpy).not.toHaveBeenCalledWith('[REALTIME] DISCONNECTED:', 'transport close');
});

test('When not in production, then it logs errors on connect_error event', () => {
service = resetServiceWithProduction(false);
service.init();
const connectErrorHandler = mockSocket.on.mock.calls.find((call) => call[0] === 'connect_error')?.[1];
const error = new Error('connection refused');
connectErrorHandler?.(error);
expect(consoleErrorSpy).toHaveBeenCalledWith('[REALTIME] CONNECTION ERROR:', error);
});

test('When in production, then it does not log errors on connect_error event', () => {
service = resetServiceWithProduction(true);
service.init();
const connectErrorHandler = mockSocket.on.mock.calls.find((call) => call[0] === 'connect_error')?.[1];
const error = new Error('connection refused');
connectErrorHandler?.(error);
expect(consoleErrorSpy).not.toHaveBeenCalledWith('[REALTIME] CONNECTION ERROR:', error);
});
});

describe('Retrieving connection identifier', () => {
test('When getting the client Id after initialization, then it provides a unique identifier', () => {
service.init();
Expand Down
19 changes: 10 additions & 9 deletions src/services/sockets/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class RealtimeService {
private socket?: Socket;
private static instance: RealtimeService;
private readonly eventHandlers: Set<(data: EventData) => void> = new Set();
private readonly isProduction = envService.isProduction();

static getInstance(): RealtimeService {
if (!this.instance) {
Expand All @@ -18,20 +19,20 @@ export default class RealtimeService {
}

init(onConnected?: () => void): void {
if (!envService.isProduction()) {
if (!this.isProduction) {
console.log('[REALTIME]: CONNECTING...');
}

this.socket = io(envService.getVariable('notifications'), {
auth: {
token: getToken(),
},
reconnection: true,
withCredentials: envService.isProduction(),
reconnection: this.isProduction,
withCredentials: this.isProduction,
});

this.socket.on('connect', () => {
if (!envService.isProduction()) {
if (!this.isProduction) {
console.log('[REALTIME]: CONNECTED WITH ID', this.socket?.id);
}
onConnected?.();
Expand All @@ -50,13 +51,13 @@ export default class RealtimeService {
});

this.socket.on('disconnect', (reason) => {
if (!envService.isProduction()) {
if (!this.isProduction) {
console.log('[REALTIME] DISCONNECTED:', reason);
}
});

this.socket.on('connect_error', (error) => {
if (!envService.isProduction()) console.error('[REALTIME] CONNECTION ERROR:', error);
if (!this.isProduction) console.error('[REALTIME] CONNECTION ERROR:', error);
});
}

Expand All @@ -68,22 +69,22 @@ export default class RealtimeService {
}

onEvent(cb: (data: any) => void): () => void {
if (!envService.isProduction()) {
if (!this.isProduction) {
console.log('[REALTIME] Registering event handler. Total handlers:', this.eventHandlers.size + 1);
}

this.eventHandlers.add(cb);

return () => {
if (!envService.isProduction()) {
if (!this.isProduction) {
console.log('[REALTIME] Removing event handler. Remaining handlers:', this.eventHandlers.size - 1);
}
this.eventHandlers.delete(cb);
};
}

removeAllListeners() {
if (!envService.isProduction()) {
if (!this.isProduction) {
console.log('[REALTIME] Clearing all event handlers');
}
this.eventHandlers.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ const DriveExplorer = (props: DriveExplorerProps): JSX.Element => {
}
};

const onTrashCleared = useCallback(() => {
dispatch(storageActions.resetTrash());
paginationState.setHasMoreItems(false);
setHasMoreTrashFolders(false);
onItemsDeleted?.();
}, [onItemsDeleted]);

const onCloseEditItemDialog = (newItem) => {
if (newItem && editNameItem) {
if (isFileViewerOpen) {
Expand Down Expand Up @@ -530,7 +537,7 @@ const DriveExplorer = (props: DriveExplorerProps): JSX.Element => {
}}
isTrash={isTrash}
/>
<ClearTrashDialog onItemsDeleted={onItemsDeleted} />
<ClearTrashDialog onItemsDeleted={onTrashCleared} />
<UploadItemsFailsDialog />
<MenuItemToGetSize isTrash={isTrash} translate={translate} menuItemsRef={menuItemsRef} />
<ItemDetailsDialog onDetailsButtonClicked={onDetailsButtonClicked} />
Expand Down
2 changes: 1 addition & 1 deletion src/views/Drive/hooks/usePaginationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const usePaginationState = ({ isTrash, hasMoreFiles, hasMoreFolders }: Us
}, [hasMoreFiles, isTrash]);

useEffect(() => {
if (hasMoreFiles && hasMoreFolders) {
if (!isTrash && hasMoreFiles && hasMoreFolders) {
setHasMoreItems(true);
}
}, [hasMoreFiles, hasMoreFolders]);
Expand Down
Loading