This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from SuperViz/chore/fix-hello-world-example
fix: should export all files by index and initialize by core
- Loading branch information
Showing
11 changed files
with
364 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,134 @@ | ||
import * as Core from '.'; | ||
import { Group, Participant } from '../common/types/participant.types'; | ||
import { SuperVizSdkOptions } from '../common/types/sdk-options.types'; | ||
import ApiService from '../services/api'; | ||
import RemoteConfigService from '../services/remote-config-service'; | ||
|
||
describe('Core', () => { | ||
test('should be export Laucher', () => { | ||
expect(Core.Laucher).toBeDefined(); | ||
import sdk from '.'; | ||
|
||
const REMOTE_CONFIG_MOCK = { | ||
apiUrl: 'https://dev.nodeapi.superviz.com', | ||
conferenceLayerUrl: 'https://video-conference-layer.superviz.com/14.0.1-rc.2/index.html', | ||
}; | ||
|
||
const COMMUNICATOR_INSTANCE_MOCK = { | ||
setSyncProperty: jest.fn(), | ||
subscribe: jest.fn(), | ||
unsubscribe: jest.fn(), | ||
destroy: jest.fn(), | ||
follow: jest.fn(), | ||
fetchSyncProperty: jest.fn(), | ||
gather: jest.fn(), | ||
goTo: jest.fn(), | ||
toggleMeetingSetup: jest.fn(), | ||
toggleMicrophone: jest.fn(), | ||
toggleCam: jest.fn(), | ||
toggleScreenShare: jest.fn(), | ||
hangUp: jest.fn(), | ||
toggleChat: jest.fn(), | ||
startTranscription: jest.fn(), | ||
stopTranscription: jest.fn(), | ||
loadPlugin: jest.fn(), | ||
unloadPlugin: jest.fn(), | ||
}; | ||
|
||
const UNIT_TEST_API_KEY = 'unit-test-api-key'; | ||
|
||
const SIMPLE_INITIALIZATION_MOCK: SuperVizSdkOptions = { | ||
roomId: 'unit-test-room-id', | ||
participant: { | ||
id: 'unit-test-participant-id', | ||
name: 'unit-test-participant-name', | ||
}, | ||
group: { | ||
name: 'unit-test-group-test-name', | ||
id: 'unit-test-group-test-id', | ||
}, | ||
}; | ||
|
||
jest.mock('../services/api'); | ||
jest.mock('../services/auth-service', () => ({ | ||
__esModule: true, | ||
default: jest.fn().mockImplementation((_, apiKey: string) => { | ||
if (apiKey === UNIT_TEST_API_KEY) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}), | ||
})); | ||
jest.mock('../services/remote-config-service'); | ||
jest.mock('../services/communicator', () => ({ | ||
__esModule: true, | ||
default: jest.fn().mockImplementation(() => { | ||
return COMMUNICATOR_INSTANCE_MOCK; | ||
}), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('root export', () => { | ||
test('should export init function', () => { | ||
expect(sdk).toEqual(expect.any(Function)); | ||
}); | ||
}); | ||
|
||
describe('initialization errors', () => { | ||
test('should throw an error if no API key is provided', async () => { | ||
await expect(sdk('', SIMPLE_INITIALIZATION_MOCK)).rejects.toThrow('API key is required'); | ||
}); | ||
|
||
test('should throw an error if API key is invalid', async () => { | ||
RemoteConfigService.getRemoteConfig = jest.fn().mockResolvedValue(REMOTE_CONFIG_MOCK); | ||
|
||
await expect(sdk('invalid-api-key', SIMPLE_INITIALIZATION_MOCK)).rejects.toThrow( | ||
'Failed to validate API key', | ||
); | ||
}); | ||
|
||
test('should throw an error if no options are provided', async () => { | ||
await expect( | ||
sdk(UNIT_TEST_API_KEY, undefined as unknown as SuperVizSdkOptions), | ||
).rejects.toThrow('Options is required'); | ||
}); | ||
|
||
test('should throw an error if no room id is provided', async () => { | ||
await expect( | ||
sdk(UNIT_TEST_API_KEY, { ...SIMPLE_INITIALIZATION_MOCK, roomId: '' }), | ||
).rejects.toThrow('Room id is required'); | ||
}); | ||
|
||
test('should throw an error if no participant id is provided', async () => { | ||
await expect( | ||
sdk(UNIT_TEST_API_KEY, { | ||
...SIMPLE_INITIALIZATION_MOCK, | ||
participant: { name: 'unit-test-participant-name' } as Participant, | ||
}), | ||
).rejects.toThrow('Participants fields is required'); | ||
|
||
await expect( | ||
sdk(UNIT_TEST_API_KEY, { | ||
...SIMPLE_INITIALIZATION_MOCK, | ||
participant: undefined as unknown as Participant, | ||
}), | ||
).rejects.toThrow('Participants fields is required'); | ||
}); | ||
|
||
test('should throw an error if no group name is provided', async () => { | ||
await expect( | ||
sdk(UNIT_TEST_API_KEY, { | ||
...SIMPLE_INITIALIZATION_MOCK, | ||
group: { id: 'unit-test-group-test-id' } as Group, | ||
}), | ||
).rejects.toThrow('Group fields is required'); | ||
}); | ||
|
||
test('should throw an error if envoriment is invalid', async () => { | ||
ApiService.fetchConfig = jest.fn().mockResolvedValue(undefined); | ||
|
||
expect(sdk(UNIT_TEST_API_KEY, SIMPLE_INITIALIZATION_MOCK)).rejects.toThrow( | ||
'Failed to load configuration from server', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,88 @@ | ||
export { default as Laucher } from './laucher'; | ||
import { debug } from 'debug'; | ||
|
||
import { SuperVizSdkOptions } from '../common/types/sdk-options.types'; | ||
import ApiService from '../services/api'; | ||
import AuthService from '../services/auth-service'; | ||
import { BrowserService } from '../services/browser'; | ||
import config from '../services/config'; | ||
import RemoteConfigService from '../services/remote-config-service'; | ||
import { SuperVizSdk } from '../types'; | ||
|
||
import LauncherFacade from './launcher'; | ||
|
||
/** | ||
* @function validateOptions | ||
* @description Validate the options passed to the SDK | ||
* @param {SuperVizSdkOptions} param | ||
* @returns {void} | ||
*/ | ||
const validateOptions = ({ group, participant, roomId }: SuperVizSdkOptions): void => { | ||
if (!group || !group.name || !group.id) { | ||
throw new Error('Group fields is required'); | ||
} | ||
|
||
if (!participant || !participant.id) { | ||
throw new Error('Participants fields is required'); | ||
} | ||
|
||
if (!roomId) { | ||
throw new Error('Room id is required'); | ||
} | ||
}; | ||
|
||
/** | ||
* @function init | ||
* @description Initialize the SDK | ||
* @param apiKey - API key | ||
* @param options - SDK options | ||
* @returns {SuperVizSdk} | ||
*/ | ||
const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<SuperVizSdk> => { | ||
const validApiKey = apiKey && apiKey.trim(); | ||
|
||
if (!validApiKey) throw new Error('API key is required'); | ||
|
||
if (!options) throw new Error('Options is required'); | ||
|
||
validateOptions(options); | ||
|
||
if (options.debug) { | ||
debug.enable('*'); | ||
} else { | ||
debug.disable(); | ||
} | ||
|
||
const { apiUrl, conferenceLayerUrl } = await RemoteConfigService.getRemoteConfig( | ||
options.environment, | ||
); | ||
|
||
const isValid = await AuthService(apiUrl, apiKey); | ||
|
||
if (!isValid) { | ||
throw new Error('Failed to validate API key'); | ||
} | ||
|
||
const environment = await ApiService.fetchConfig(apiUrl, apiKey); | ||
|
||
if (!environment || !environment.ablyKey) { | ||
throw new Error('Failed to load configuration from server'); | ||
} | ||
|
||
const { ablyKey } = environment; | ||
|
||
config.setConfig({ | ||
apiUrl, | ||
ablyKey, | ||
apiKey, | ||
conferenceLayerUrl, | ||
environment, | ||
roomId: options.roomId, | ||
debug: options.debug, | ||
}); | ||
|
||
return LauncherFacade( | ||
Object.assign({}, options, { apiKey, ablyKey, conferenceLayerUrl, apiUrl }), | ||
); | ||
}; | ||
|
||
export default init; |
Oops, something went wrong.