Skip to content

Commit

Permalink
chore: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omridan159 committed Feb 29, 2024
1 parent ac14808 commit 84d6129
Show file tree
Hide file tree
Showing 89 changed files with 604 additions and 959 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { RemoteCommunicationState } from '../../../RemoteCommunication';
import * as loggerModule from '../../../utils/logger';
import { clean } from './clean';

describe('clean', () => {
const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');
let state: RemoteCommunicationState;

beforeEach(() => {
Expand All @@ -27,23 +29,11 @@ describe('clean', () => {
expect(state.originatorConnectStarted).toBe(false);
});

it('should log a debug message if debug is true', () => {
jest.spyOn(console, 'debug').mockImplementation();

state.debug = true;

it('should log a debug message', () => {
clean(state);

expect(console.debug).toHaveBeenCalledWith(
'RemoteCommunication::test::clean()',
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: clean()] context=test',
);
});

it('should not log a debug message if debug is false', () => {
jest.spyOn(console, 'debug').mockImplementation();

clean(state);

expect(console.debug).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { RemoteCommunicationState } from '../../../RemoteCommunication';
import { CommunicationLayer } from '../../../types/CommunicationLayer';
import { StorageManager } from '../../../types/StorageManager';
import * as loggerModule from '../../../utils/logger';
import { generateChannelIdConnect } from './generateChannelIdConnect';
import { clean } from './clean';

Expand All @@ -10,6 +11,8 @@ jest.mock('./clean');
describe('generateChannelIdConnect', () => {
let state: RemoteCommunicationState;

const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');

const mockClean = clean as jest.MockedFunction<typeof clean>;

beforeEach(() => {
Expand Down Expand Up @@ -117,20 +120,16 @@ describe('generateChannelIdConnect', () => {
);
});

it('should log debug messages if debug is enabled', () => {
jest.spyOn(console, 'debug').mockImplementation();

state.debug = true;

it('should log debug messages', () => {
generateChannelIdConnect(state);

expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::generateChannelId()`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: generateChannelId()]',
);

expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::generateChannelId() channel created`,
expect.anything(),
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: generateChannelId()] channel created',
{ channelId: 'mockChannelId', pubKey: 'mockPublicKey' },
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { v4 as uuid } from 'uuid';
import { RemoteCommunicationState } from '../../../RemoteCommunication';
import { CommunicationLayer } from '../../../types/CommunicationLayer';
import { StorageManager } from '../../../types/StorageManager';
import * as loggerModule from '../../../utils/logger';
import { connectToChannel } from './connectToChannel';

describe('connectToChannel', () => {
let state: RemoteCommunicationState;

const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');

beforeEach(() => {
jest.clearAllMocks();

Expand All @@ -32,19 +35,16 @@ describe('connectToChannel', () => {

it('should debug log if the channel is already connected', () => {
const channelId = uuid();

state.communicationLayer = {
isConnected: jest.fn(() => true),
} as unknown as CommunicationLayer;

const consoleDebugSpy = jest.spyOn(console, 'debug').mockImplementation();

connectToChannel({ channelId, state });

expect(consoleDebugSpy).toHaveBeenCalledWith(
`RemoteCommunication::TestContext::connectToChannel() already connected - interrup connection.`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: connectToChannel()] context=TestContext already connected - interrupt connection.',
);

consoleDebugSpy.mockRestore();
});

it('should connect to a valid channel', () => {
Expand Down Expand Up @@ -100,19 +100,14 @@ describe('connectToChannel', () => {
});
});

it('should debug log a valid channelId if debug is enabled', () => {
it('should debug log a valid channelId', () => {
const channelId = uuid();
state.debug = true;

const consoleDebugSpy = jest.spyOn(console, 'debug').mockImplementation();

connectToChannel({ channelId, state });

expect(consoleDebugSpy).toHaveBeenCalledWith(
`RemoteCommunication::${state.context}::connectToChannel() channelId=${channelId}`,
expect(spyLogger).toHaveBeenCalledWith(
`[RemoteCommunication: connectToChannel()] context=TestContext channelId=${channelId}`,
);

consoleDebugSpy.mockRestore();
});

it('should set the new channelId in the state', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function connectToChannel({
if (state.communicationLayer?.isConnected()) {
// Adding a check on previous connection to prevent reconnecting during dev when HMR is enabled
loggerRemoteLayer(
`[RemoteCommunication: connectToChannel()] context=${state.context} already connected - interrup connection.`,
`[RemoteCommunication: connectToChannel()] context=${state.context} already connected - interrupt connection.`,
);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { ConnectionStatus } from '../../../types/ConnectionStatus';
import { CommunicationLayer } from '../../../types/CommunicationLayer';
import { StorageManager } from '../../../types/StorageManager';
import { MessageType } from '../../../types/MessageType';
import * as loggerModule from '../../../utils/logger';
import { disconnect } from './disconnect';

describe('disconnect', () => {
let instance: RemoteCommunication;
const mockSetConnectionStatus = jest.fn();

const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');

beforeEach(() => {
jest.clearAllMocks();

Expand Down Expand Up @@ -134,17 +137,12 @@ describe('disconnect', () => {
});

it('should log debug information if debug is enabled', () => {
instance.state.debug = true;
const consoleDebugSpy = jest.spyOn(console, 'debug').mockImplementation();

disconnect({ instance });

expect(consoleDebugSpy).toHaveBeenCalledWith(
`RemoteCommunication::disconnect() channel=${instance.state.channelId}`,
expect(spyLogger).toHaveBeenCalledWith(
`[RemoteCommunication: disconnect()] channel=${instance.state.channelId}`,
undefined,
);

consoleDebugSpy.mockRestore();
});

it('should reset originatorConnectStarted on termination', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import { RemoteCommunication } from '../../../RemoteCommunication';
import { CommunicationLayerMessage } from '../../../types/CommunicationLayerMessage';
import { EventType } from '../../../types/EventType';
import * as loggerModule from '../../../utils/logger';
import { handleAuthorization } from './handleAuthorization';

describe('handleAuthorization', () => {
let instance: RemoteCommunication;
let message: CommunicationLayerMessage;

const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');

const mockOnce = jest.fn();
const mockSendMessage = jest.fn();

Expand Down Expand Up @@ -85,19 +88,16 @@ describe('handleAuthorization', () => {
);
});

it('should log debug information if debug is enabled', async () => {
instance.state.debug = true;
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
const consoleDebugSpy = jest.spyOn(console, 'debug').mockImplementation();

it('should log debug information', async () => {
await handleAuthorization(instance, message);

expect(consoleLogSpy).toHaveBeenCalledWith(
`RemoteCommunication::${instance.state.context}::sendMessage::handleAuthorization ready=${instance.state.ready} authorized=${instance.state.authorized} method=${message.method}`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: handleAuthorization()] context=testContext ready=true authorized=false method=sampleMethod',
);

consoleLogSpy.mockRestore();
consoleDebugSpy.mockRestore();
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: handleAuthorization()] context=testContext AFTER SKIP / AUTHORIZED -- sending pending message',
);
});

it('should correctly handle wallet versions like 7.10 vs 7.9', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { RemoteCommunication } from '../../../RemoteCommunication';
import * as loggerModule from '../../../utils/logger';
import { originatorSessionConnect } from './originatorSessionConnect';

describe('originatorSessionConnect', () => {
let instance: RemoteCommunication;

const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');
const mockIsConnected = jest.fn();
const mockGetPersistedChannelConfig = jest.fn();
const mockConnectToChannel = jest.fn();
Expand Down Expand Up @@ -31,17 +34,17 @@ describe('originatorSessionConnect', () => {
delete instance.state.storageManager;
const result = await originatorSessionConnect(instance);
expect(result).toBeUndefined();
expect(console.debug).toHaveBeenCalledWith(
'RemoteCommunication::connect() no storage manager defined - skip',
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: originatorSessionConnect()] no storage manager defined - skip',
);
});

it('should skip if the socket is already connected', async () => {
mockIsConnected.mockReturnValueOnce(true);
const result = await originatorSessionConnect(instance);
expect(result).toBeUndefined();
expect(console.debug).toHaveBeenCalledWith(
'RemoteCommunication::connect() socket already connected - skip',
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: originatorSessionConnect()] socket already connected - skip',
);
});

Expand Down Expand Up @@ -69,8 +72,8 @@ describe('originatorSessionConnect', () => {

const result = await originatorSessionConnect(instance);
expect(result).toBeUndefined();
expect(console.log).toHaveBeenCalledWith(
'RemoteCommunication::autoConnect Session has expired',
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: autoConnect()] Session has expired',
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { RemoteCommunication } from '../../../RemoteCommunication';
import { ConnectionStatus } from '../../../types/ConnectionStatus';
import * as loggerModule from '../../../utils/logger';
import { resume } from './resume';

describe('resume', () => {
let instance: RemoteCommunication;

const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');
const mockResume = jest.fn();
const mockSetConnectionStatus = jest.fn();

Expand All @@ -24,19 +27,13 @@ describe('resume', () => {
} as unknown as RemoteCommunication;
});

it('should log channel being resumed when debug is true', () => {
it('should log channel info', () => {
resume(instance);
expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::resume() channel=testChannel`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: resume()] channel=testChannel',
);
});

it('should not log when debug is false', () => {
instance.state.debug = false;
resume(instance);
expect(console.debug).not.toHaveBeenCalled();
});

it('should call resume on the communication layer', () => {
resume(instance);
expect(mockResume).toHaveBeenCalledTimes(1);
Expand All @@ -56,8 +53,8 @@ describe('resume', () => {
resume(instance);
}).not.toThrow();

expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::resume() channel=testChannel`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: resume()] channel=testChannel',
);
});

Expand All @@ -74,17 +71,17 @@ describe('resume', () => {
instance.state.channelId = undefined;

resume(instance);
expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::resume() channel=undefined`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: resume()] channel=undefined',
);
});

it('should handle when channelId is undefined', () => {
it('should log debug info when channelId is undefined', () => {
instance.state.channelId = undefined;

resume(instance);
expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::resume() channel=undefined`,
expect(spyLogger).toHaveBeenCalledWith(
'[RemoteCommunication: resume()] channel=undefined',
);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { RemoteCommunication } from '../../../RemoteCommunication';
import { EventType } from '../../../types/EventType';
import * as loggerModule from '../../../utils/logger';
import { handleChannelCreatedEvent } from './handleChannelCreatedEvent';

describe('handleChannelCreatedEvent', () => {
let instance: RemoteCommunication;
const mockEmit = jest.fn();

jest.spyOn(console, 'debug').mockImplementation();
const spyLogger = jest.spyOn(loggerModule, 'loggerRemoteLayer');
const mockEmit = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -28,17 +29,10 @@ describe('handleChannelCreatedEvent', () => {
const channelId = 'sampleChannelId';
const handler = handleChannelCreatedEvent(instance);
handler(channelId);
expect(console.debug).toHaveBeenCalledWith(
`RemoteCommunication::testContext::on 'channel_created' channelId=${channelId}`,
);
});

it('should not log the event details if debugging is disabled', () => {
instance.state.debug = false;
const channelId = 'sampleChannelId';
const handler = handleChannelCreatedEvent(instance);
handler(channelId);
expect(console.debug).not.toHaveBeenCalled();
expect(spyLogger).toHaveBeenCalledWith(
"[RemoteCommunication: handleChannelCreatedEvent()] context=testContext on 'channel_created' channelId=sampleChannelId",
);
});

it('should emit CHANNEL_CREATED event with the channel ID', () => {
Expand Down
Loading

0 comments on commit 84d6129

Please sign in to comment.