From 43568f6907eb1c1b88e057431d60518b74794713 Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Fri, 31 Mar 2023 15:48:05 +0200 Subject: [PATCH] test(errors): add tests for ErrorToI18nKeyMapper Signed-off-by: Erik Michelson --- .../common/error-to-i18n-key-mapper.spec.ts | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 frontend/src/api/common/error-to-i18n-key-mapper.spec.ts diff --git a/frontend/src/api/common/error-to-i18n-key-mapper.spec.ts b/frontend/src/api/common/error-to-i18n-key-mapper.spec.ts new file mode 100644 index 000000000..02ae3ea4f --- /dev/null +++ b/frontend/src/api/common/error-to-i18n-key-mapper.spec.ts @@ -0,0 +1,57 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { ApiError } from './api-error' +import { ErrorToI18nKeyMapper } from './error-to-i18n-key-mapper' + +describe('ErrorToI18nKeyMapper', () => { + it('returns fallback with namespace when no mapper is defined', () => { + const mapper = new ErrorToI18nKeyMapper(new Error('test'), 'namespace') + const result = mapper.orFallbackI18nKey('fallback') + expect(result).toEqual('namespace.fallback') + }) + + it('returns fallback without namespace when no mapper is defined and absolute key should be used', () => { + const mapper = new ErrorToI18nKeyMapper(new Error('test'), 'namespace') + const result = mapper.orFallbackI18nKey('fallback', true) + expect(result).toEqual('fallback') + }) + + it('returns undefined when no mapper is defined and no fallback defined', () => { + const mapper = new ErrorToI18nKeyMapper(new Error('test'), 'namespace') + const result = mapper.orFallbackI18nKey(undefined) + expect(result).toEqual(undefined) + }) + + it('maps an error by its error message to an i18n key', () => { + const mapper = new ErrorToI18nKeyMapper(new Error('test'), 'namespace') + const result = mapper + .withBackendErrorName('BackendError', 'backendError') + .withErrorMessage('test', 'testError') + .withHttpCode(418, 'teapot') + .orFallbackI18nKey('fallback') + expect(result).toEqual('namespace.testError') + }) + + it('maps an API error by its error name to an i18n key', () => { + const mapper = new ErrorToI18nKeyMapper(new ApiError(500, 'BackendError'), 'namespace') + const result = mapper + .withBackendErrorName('BackendError', 'backendError') + .withErrorMessage('test', 'testError') + .withHttpCode(418, 'teapot') + .orFallbackI18nKey('fallback') + expect(result).toEqual('namespace.backendError') + }) + + it('maps an API error by its status code to an i18n key', () => { + const mapper = new ErrorToI18nKeyMapper(new ApiError(418, 'Coffee'), 'namespace') + const result = mapper + .withBackendErrorName('BackendError', 'backendError') + .withErrorMessage('test', 'testError') + .withHttpCode(418, 'teapot') + .orFallbackI18nKey('fallback') + expect(result).toEqual('namespace.teapot') + }) +})