Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: split the SDKConnect class into smaller chunks and add unit tests #8028

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2ec32d1
chore: add unit tests
omridan159 Dec 5, 2023
4924a28
Merge remote-tracking branch 'origin/main' into chore_add-unit-tests-…
omridan159 Dec 5, 2023
bc005a9
chore: add unit tests
omridan159 Dec 5, 2023
72a4578
Merge remote-tracking branch 'origin/main' into chore_add-unit-tests-…
omridan159 Dec 6, 2023
9aa7115
chore: create new files for all the SDKConnect methods
omridan159 Dec 6, 2023
af86097
chore: split the SDKConnect class into smaller chunks
omridan159 Dec 7, 2023
fec2d97
chore: split the init method
omridan159 Dec 7, 2023
495e185
chore: remove duplicated code
omridan159 Dec 7, 2023
e28f66a
Merge branch 'chore_add-unit-tests-to-SDKConnect-handlers' into chore…
omridan159 Dec 7, 2023
da366e2
chore: add unit tests
omridan159 Dec 7, 2023
459180f
chore: rename file
omridan159 Dec 7, 2023
cab12dd
Merge branch 'chore_add-unit-tests-to-SDKConnect-handlers' into chore…
omridan159 Dec 7, 2023
bbd446a
chore: add unit tests
omridan159 Dec 8, 2023
3fe8965
fix: the Cannot assign to read only property 'performance' of object …
omridan159 Dec 8, 2023
635daa3
Merge remote-tracking branch 'origin/main' into chore_add-unit-tests-…
omridan159 Dec 11, 2023
fc6e809
Merge branch 'chore_add-unit-tests-to-SDKConnect-handlers' into chore…
omridan159 Dec 11, 2023
123e709
chore: downgrade jest back to the og version
omridan159 Dec 11, 2023
ec6711d
chore: add unit tests
omridan159 Dec 11, 2023
ef032ae
chore: fix 2 bugs from sonarcloud
omridan159 Dec 11, 2023
98b912a
Merge remote-tracking branch 'origin/main' into chore_split-the-SDKCo…
omridan159 Dec 12, 2023
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
65 changes: 65 additions & 0 deletions app/core/SDKConnect/AndroidSDK/addAndroidConnection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import DefaultPreference from 'react-native-default-preference';
import AppConstants from '../../../core/AppConstants';
import { ConnectionProps } from '../Connection';
import SDKConnect from '../SDKConnect';
import addAndroidConnection from './addAndroidConnection';

jest.mock('../Connection');
jest.mock('../SDKConnect');
jest.mock('../utils/DevLogger');
jest.mock('react-native-default-preference', () => ({
set: jest.fn().mockResolvedValue(''),
}));
jest.mock('../../../core/AppConstants');

describe('addAndroidConnection', () => {
let mockInstance = {} as unknown as SDKConnect;
const mockEmit = jest.fn();

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

mockInstance = {
state: {
connections: {},
},
emit: mockEmit,
} as unknown as SDKConnect;
});

it('should add the connection to the instance state', async () => {
const mockConnection = {
id: 'test-id',
} as unknown as ConnectionProps;

await addAndroidConnection(mockConnection, mockInstance);

expect(mockInstance.state.connections[mockConnection.id]).toBe(
mockConnection,
);
});

it('should save the updated connections to DefaultPreference', async () => {
const mockConnection = {
id: 'test-id',
} as unknown as ConnectionProps;

await addAndroidConnection(mockConnection, mockInstance);

expect(DefaultPreference.set).toHaveBeenCalledWith(
AppConstants.MM_SDK.ANDROID_CONNECTIONS,
JSON.stringify(mockInstance.state.connections),
);
});

it('should emit a refresh event', async () => {
const mockConnection = {
id: 'test-id',
} as unknown as ConnectionProps;

await addAndroidConnection(mockConnection, mockInstance);

expect(mockEmit).toHaveBeenCalledTimes(1);
expect(mockEmit).toHaveBeenCalledWith('refresh');
});
});
25 changes: 25 additions & 0 deletions app/core/SDKConnect/AndroidSDK/addAndroidConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ConnectionProps } from '../Connection';
import SDKConnect from '../SDKConnect';
import DevLogger from '../utils/DevLogger';
import DefaultPreference from 'react-native-default-preference';
import AppConstants from '../../../core/AppConstants';

async function addAndroidConnection(
connection: ConnectionProps,
instance: SDKConnect,
) {
instance.state.connections[connection.id] = connection;

DevLogger.log(`SDKConnect::addAndroidConnection`, connection);

await DefaultPreference.set(
AppConstants.MM_SDK.ANDROID_CONNECTIONS,
JSON.stringify(instance.state.connections),
).catch((err) => {
throw err;
});

instance.emit('refresh');
}

export default addAndroidConnection;
44 changes: 44 additions & 0 deletions app/core/SDKConnect/AndroidSDK/bindAndroidSDK.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { NativeModules, Platform } from 'react-native';
import SDKConnect from '../SDKConnect';
import bindAndroidSDK from './bindAndroidSDK';

jest.mock('../SDKConnect');
jest.mock('../../../util/Logger');

describe('bindAndroidSDK', () => {
let mockInstance = {} as unknown as SDKConnect;

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

NativeModules.CommunicationClient = {
bindService: jest.fn(),
};

mockInstance = {
state: {
androidSDKBound: false,
},
} as unknown as SDKConnect;
});

it('should return early if the platform is not android', async () => {
(Platform as any).OS = 'ios';

await bindAndroidSDK(mockInstance);

expect(
NativeModules.CommunicationClient.bindService,
).not.toHaveBeenCalled();
});

it('should return early if the Android SDK is already bound', async () => {
mockInstance.state.androidSDKBound = true;

await bindAndroidSDK(mockInstance);

expect(
NativeModules.CommunicationClient.bindService,
).not.toHaveBeenCalled();
});
});
21 changes: 21 additions & 0 deletions app/core/SDKConnect/AndroidSDK/bindAndroidSDK.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Logger from '../../../util/Logger';
import { NativeModules, Platform } from 'react-native';
import SDKConnect from '../SDKConnect';

async function bindAndroidSDK(instance: SDKConnect) {
if (Platform.OS !== 'android') {
return;
}

if (instance.state.androidSDKBound) return;

try {
// Always bind native module to client as early as possible otherwise connection may have an invalid status
await NativeModules.CommunicationClient.bindService();
instance.state.androidSDKBound = true;
} catch (err) {
Logger.log(err, `SDKConnect::bindAndroiSDK failed`);
}
}

export default bindAndroidSDK;
46 changes: 46 additions & 0 deletions app/core/SDKConnect/AndroidSDK/loadAndroidConnections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import DefaultPreference from 'react-native-default-preference';
import AppConstants from '../../../core/AppConstants';
import loadAndroidConnections from './loadAndroidConnections';

jest.mock('../../../core/AppConstants');
jest.mock('react-native-default-preference', () => ({
get: jest.fn().mockResolvedValue(''),
set: jest.fn().mockResolvedValue(''),
}));
jest.mock('../utils/DevLogger');

describe('loadAndroidConnections', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should retrieve Android connections from DefaultPreference', async () => {
await loadAndroidConnections();

expect(DefaultPreference.get).toHaveBeenCalledWith(
AppConstants.MM_SDK.ANDROID_CONNECTIONS,
);
});

it('should return an empty object if no connections are found', async () => {
const result = await loadAndroidConnections();

expect(result).toStrictEqual({});
});

it('should parse the retrieved connections', async () => {
const mockConnections = {
'test-id': {
id: 'test-id',
},
};

(DefaultPreference.get as jest.Mock).mockResolvedValueOnce(
JSON.stringify(mockConnections),
);

const result = await loadAndroidConnections();

expect(result).toStrictEqual(mockConnections);
});
});
22 changes: 22 additions & 0 deletions app/core/SDKConnect/AndroidSDK/loadAndroidConnections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import AppConstants from '../../../core/AppConstants';
import { ConnectionProps } from '../Connection';
import DefaultPreference from 'react-native-default-preference';
import DevLogger from '../utils/DevLogger';

async function loadAndroidConnections(): Promise<{
[id: string]: ConnectionProps;
}> {
const rawConnections = await DefaultPreference.get(
AppConstants.MM_SDK.ANDROID_CONNECTIONS,
);

if (!rawConnections) return {};

const parsed = JSON.parse(rawConnections);
DevLogger.log(
`SDKConnect::loadAndroidConnections found ${Object.keys(parsed).length}`,
);
return parsed;
}

export default loadAndroidConnections;
69 changes: 69 additions & 0 deletions app/core/SDKConnect/AndroidSDK/removeAndroidConnection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import DefaultPreference from 'react-native-default-preference';
import AppConstants from '../../../core/AppConstants';
import { ConnectionProps } from '../Connection';
import SDKConnect from '../SDKConnect';
import removeAndroidConnection from './removeAndroidConnection';

jest.mock('../../../core/AppConstants');
jest.mock('../SDKConnect');
jest.mock('react-native-default-preference', () => ({
set: jest.fn().mockResolvedValue(''),
}));

describe('removeAndroidConnection', () => {
let mockInstance = {} as unknown as SDKConnect;
const mockEmit = jest.fn();

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

mockInstance = {
state: {
connections: {},
},
emit: mockEmit,
} as unknown as SDKConnect;
});

it('should remove the specified connection from the instance state', () => {
const mockConnection = {
id: 'test-id',
};

mockInstance.state.connections[mockConnection.id] =
mockConnection as unknown as ConnectionProps;

removeAndroidConnection(mockConnection.id, mockInstance);

expect(mockInstance.state.connections[mockConnection.id]).toBeUndefined();
});

it('should update the connections in DefaultPreference', () => {
const mockConnection = {
id: 'test-id',
};

mockInstance.state.connections[mockConnection.id] =
mockConnection as unknown as ConnectionProps;

removeAndroidConnection(mockConnection.id, mockInstance);

expect(DefaultPreference.set).toHaveBeenCalledWith(
AppConstants.MM_SDK.ANDROID_CONNECTIONS,
JSON.stringify(mockInstance.state.connections),
);
});

it('should emit a refresh event', () => {
const mockConnection = {
id: 'test-id',
};

mockInstance.state.connections[mockConnection.id] =
mockConnection as unknown as ConnectionProps;

removeAndroidConnection(mockConnection.id, mockInstance);

expect(mockEmit).toHaveBeenCalledWith('refresh');
});
});
16 changes: 16 additions & 0 deletions app/core/SDKConnect/AndroidSDK/removeAndroidConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import AppConstants from '../../../core/AppConstants';
import SDKConnect from '../SDKConnect';
import DefaultPreference from 'react-native-default-preference';

function removeAndroidConnection(id: string, instance: SDKConnect) {
delete instance.state.connections[id];
DefaultPreference.set(
AppConstants.MM_SDK.ANDROID_CONNECTIONS,
JSON.stringify(instance.state.connections),
).catch((err) => {
throw err;
});
instance.emit('refresh');
}

export default removeAndroidConnection;
Loading
Loading