X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Futils%2FErrorUtils.test.ts;h=59c455f94df9f7ae4aa9a70efda1acb1ab19b45e;hb=2d4928a7237a906158f2e9652e08f0392eeabdcd;hp=486a031635af4a0e00ae15957b9135a00265deb4;hpb=b49550e256d11c7a30fb19cd672e5e3611d01553;p=e-mobility-charging-stations-simulator.git diff --git a/tests/utils/ErrorUtils.test.ts b/tests/utils/ErrorUtils.test.ts index 486a0316..59c455f9 100644 --- a/tests/utils/ErrorUtils.test.ts +++ b/tests/utils/ErrorUtils.test.ts @@ -2,21 +2,80 @@ import { describe, it } from 'node:test' import { expect } from 'expect' -import { FileType } from '../../src/types/index.js' -import { handleFileException, setDefaultErrorParams } from '../../src/utils/ErrorUtils.js' +import type { ChargingStation } from '../../src/charging-station/index.js' +import { + FileType, + GenericStatus, + IncomingRequestCommand, + MessageType, + RequestCommand +} from '../../src/types/index.js' +import { + handleFileException, + handleIncomingRequestError, + handleSendMessageError, + setDefaultErrorParams +} from '../../src/utils/ErrorUtils.js' await describe('ErrorUtils test suite', async () => { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + const chargingStation = { + logPrefix: () => 'CS-TEST |' + } as ChargingStation + await it('Verify handleFileException()', () => { const error = new Error() error.code = 'ENOENT' expect(() => { - handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix', {}) + handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {}) + }).toThrow(error) + expect(() => { + handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { + throwError: false + }) + }).not.toThrow() + }) + + await it('Verify handleSendMessageError()', () => { + const error = new Error() + expect(() => { + handleSendMessageError( + chargingStation, + RequestCommand.BOOT_NOTIFICATION, + MessageType.CALL_MESSAGE, + error + ) + }).not.toThrow() + expect(() => { + handleSendMessageError( + chargingStation, + RequestCommand.BOOT_NOTIFICATION, + MessageType.CALL_MESSAGE, + error, + { throwError: true } + ) + }).toThrow(error) + }) + + await it('Verify handleIncomingRequestError()', () => { + const error = new Error() + expect(() => { + handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error) }).toThrow(error) expect(() => { - handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix', { + handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { throwError: false }) }).not.toThrow() + const errorResponse = { + status: GenericStatus.Rejected + } + expect( + handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { + throwError: false, + errorResponse + }) + ).toStrictEqual(errorResponse) }) await it('Verify setDefaultErrorParams()', () => {