diff --git a/src/mocks/mock_dialog.json b/src/mocks/mock_dialog.json new file mode 100644 index 0000000000000000000000000000000000000000..da3492dddfba4d9f6336ac42b9ed24ad2850572f --- /dev/null +++ b/src/mocks/mock_dialog.json @@ -0,0 +1,4 @@ +{ + "type" :"Network Error", + "message" : "The experiment is loading" +} \ No newline at end of file diff --git a/src/services/__tests__/dialog-service.test.js b/src/services/__tests__/dialog-service.test.js new file mode 100644 index 0000000000000000000000000000000000000000..dce12befa2b01295109d20924aba3cb0fad7d527 --- /dev/null +++ b/src/services/__tests__/dialog-service.test.js @@ -0,0 +1,126 @@ +/** + * @jest-environment jsdom +*/ +import '@testing-library/jest-dom'; +import 'jest-fetch-mock'; + +import DialogService from '../dialog-service'; + +import MockDialog from '../../mocks/mock_dialog.json'; + +test('makes sure that invoking the constructor fails with the right message', () => { + expect(() => { + new DialogService(); + }).toThrow(Error); + expect(() => { + new DialogService(); + }).toThrowError(Error('Use DialogService.instance')); +}); + +test('the experiments service instance always refers to the same object', () => { + const instance1 = DialogService.instance; + const instance2 = DialogService.instance; + expect(instance1).toBe(instance2); +}); + +test('should emit an event on network error', () => { + jest.spyOn(DialogService.instance, 'networkError').mockImplementation(() => { + return Promise.resolve(); + }); + let NetworkError = MockDialog + + let confirmNetworkError = (startingNetwork) => { + expect(startingNetwork).toEqual(NetworkError); + }; + DialogService.instance.addListener( + DialogService.EVENTS.Error, + confirmNetworkError + ); + DialogService.instance.networkError(NetworkError); + DialogService.instance.removeListener( + DialogService.EVENTS.Error, + confirmNetworkError + ); +}); + +test('should emit an event on data error', () => { + jest.spyOn(DialogService.instance, 'dataError').mockImplementation(() => { + return Promise.resolve(); + }); + let DataError = MockDialog + + let confirmDataError = (startingData) => { + expect(startingData).toEqual(DataError); + }; + DialogService.instance.addListener( + DialogService.EVENTS.ERROR, + confirmDataError + ); + DialogService.instance.dataError(DataError); + DialogService.instance.removeListener( + DialogService.EVENTS.ERROR, + confirmDataError + ); +}); + +test('should emit an event on simulation error', () => { + jest.spyOn(DialogService.instance, 'simulationError').mockImplementation(() => { + return Promise.resolve(); + }); + let SimulationError = MockDialog + + let confirmSimulationError = (startingSimulation) => { + expect(startingSimulation).toEqual(SimulationError); + }; + DialogService.instance.addListener( + DialogService.EVENTS.ERROR, + confirmSimulationError + ); + DialogService.instance.dataError(SimulationError); + DialogService.instance.removeListener( + DialogService.EVENTS.ERROR, + confirmSimulationError + ); +}); + +test('should emit an event on progress notification', () => { + jest.spyOn(DialogService.instance, 'progressNotification').mockImplementation(() => { + return Promise.resolve(); + }); + let ProgressNotification = MockDialog + + let confirmProgressNotification = (startingProgress) => { + expect(startingProgress).toEqual(ProgressNotification); + }; + DialogService.instance.addListener( + DialogService.EVENTS.ERROR, + confirmProgressNotification + ); + DialogService.instance.dataError(ProgressNotification); + DialogService.instance.removeListener( + DialogService.EVENTS.ERROR, + confirmProgressNotification + ); +}); + +test('should emit an event on warning notification', () => { + jest.spyOn(DialogService.instance, 'warningNotification').mockImplementation(() => { + return Promise.resolve(); + }); + + let WarningNotification = MockDialog + + let confirmWarningNotification = (startingWarning) => { + expect(startingWarning).toEqual(WarningNotification); + }; + + DialogService.instance.addListener( + DialogService.EVENTS.ERROR, + confirmWarningNotification + ); + DialogService.instance.dataError(WarningNotification); + DialogService.instance.removeListener( + DialogService.EVENTS.ERROR, + confirmWarningNotification + ); +}); diff --git a/src/services/__tests__/import-experiment-service.test.js b/src/services/__tests__/import-experiment-service.test.js deleted file mode 100644 index 4adb8571286d8b585bfcd1e918a6e0b53879dffd..0000000000000000000000000000000000000000 --- a/src/services/__tests__/import-experiment-service.test.js +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @jest-environment jsdom -*/ -import '@testing-library/jest-dom'; -import 'jest-fetch-mock'; - -import ErrorHandlerService from '../error-handler-service'; - -test('makes sure that invoking the constructor fails with the right message', () => { - expect(() => { - new ErrorHandlerService(); - }).toThrow(Error); - expect(() => { - new ErrorHandlerService(); - }).toThrowError(Error('Use ErrorHandlerService.instance')); -}); - -test('the experiments service instance always refers to the same object', () => { - const instance1 = ErrorHandlerService.instance; - const instance2 = ErrorHandlerService.instance; - expect(instance1).toBe(instance2); -}); - -test('should emit an event on network error', () => { - jest.spyOn(ErrorHandlerService.instance, 'networkError').mockImplementation(() => { - return Promise.resolve(); - }); - let NetworkError = MockNetworkError; - ErrorHandlerService.instance.addListener( - ErrorHandlerService.EVENTS.Error, - confirmStartingExperiment - ); - await ErrorHandlerService.instance.networkError(NetworkError); - ErrorHandlerService.instance.removeListener( - ErrorHandlerService.EVENTS.Error, - confirmStartingExperiment - ); -}); - -test('should emit an event on data error', () => { - jest.spyOn(ErrorHandlerService.instance, 'dataError').mockImplementation(() => { - return Promise.resolve(); - }); - let DataError = DataNetworkError; - ErrorHandlerService.instance.addListener( - ErrorHandlerService.EVENTS.ERROR, - confirmStartingExperiment - ); - await ErrorHandlerService.instance.dataError(NetworkError); - ErrorHandlerService.instance.removeListener( - ErrorHandlerService.EVENTS.ERROR, - confirmStartingExperiment - ); -}); diff --git a/src/services/experiments/files/__tests__/import-experiment-service.test.js b/src/services/experiments/files/__tests__/import-experiment-service.test.js new file mode 100644 index 0000000000000000000000000000000000000000..b6163b4d7e67a98829db4b38b5dd41cedd332e0f --- /dev/null +++ b/src/services/experiments/files/__tests__/import-experiment-service.test.js @@ -0,0 +1,20 @@ +/** + * @jest-environment jsdom +*/ +import '@testing-library/jest-dom'; +import 'jest-fetch-mock'; + +import ImportExperimentService from '../import-experiment-service'; +import MockEvent from '../../../../mocks/mock-event' +import MockResponse from '../../../../mocks/mock-response' +import MockResponses from '../../../../mocks/mock-responses' + +test('makes sure that invoking the constructor fails with the right message', () => { + expect(() => { + new ImportExperimentService(); + }).toThrow(Error); + expect(() => { + new ImportExperimentService(); + }).toThrowError(Error('Use ImportExperimentService.instance')); +}); + diff --git a/src/services/models/models-storage-service.js b/src/services/models/models-storage-service.js index f9ae2bff1f24161f1d9e6912308873d4045b95ba..1f0d961ac5267ab21c49653c0428ad8dfe530415 100644 --- a/src/services/models/models-storage-service.js +++ b/src/services/models/models-storage-service.js @@ -2,7 +2,7 @@ import { HttpService } from '../http-service.js'; import endpoints from '../proxy/data/endpoints.json'; import config from '../../config.json'; -import ErrorHandlerService from '../error-handler-service'; +import DialogService from '../dialog-service'; const storageModelsURL = `${config.api.proxy.url}${endpoints.proxy.models.url}`; const allCustomModelsURL = `${config.api.proxy.url}${endpoints.proxy.storage.allCustomModels.url}`; @@ -48,7 +48,7 @@ class ModelsStorageService extends HttpService { this.verifyModelType(modelType); } catch (error) { - ErrorHandlerService.instance.dataError(error); + DialogService.instance.dataError(error); } try { @@ -58,7 +58,7 @@ class ModelsStorageService extends HttpService { this.models = await (await this.httpRequestGET(modelsWithTypeURL)).json(); } catch (error) { - ErrorHandlerService.instance.networkError(error); + DialogService.instance.networkError(error); } } @@ -80,7 +80,7 @@ class ModelsStorageService extends HttpService { return (await this.httpRequestGET(customModelsURL)).json(); } catch (error) { - ErrorHandlerService.instance.networkError(error); + DialogService.instance.networkError(error); } } @@ -115,7 +115,7 @@ class ModelsStorageService extends HttpService { return (await this.httpRequestDELETE(deleteCustomModelURL)).json(); } catch (error) { - ErrorHandlerService.instance.dataError(error); + DialogService.instance.dataError(error); } } @@ -136,7 +136,7 @@ class ModelsStorageService extends HttpService { return (await this.httpRequestPOST(setCustomModelURL, fileContent)).json(); } catch (error) { - ErrorHandlerService.instance.networkError(error); + DialogService.instance.networkError(error); } } }