Skip to content
Snippets Groups Projects
Commit a7430f95 authored by Detailleur's avatar Detailleur
Browse files

[NRRPLT-8288] dialog service tests

parent a44da008
No related branches found
No related tags found
No related merge requests found
{
"type" :"Network Error",
"message" : "The experiment is loading"
}
\ No newline at end of file
/**
* @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
);
});
/**
* @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
);
});
/**
* @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'));
});
......@@ -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);
}
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment