|
| 1 | +/** |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { describe, it, expect, vi } from 'vitest'; |
| 17 | + |
| 18 | +import { ErrorReporter } from './error_reporter'; |
| 19 | + |
| 20 | +import { OptimizelyError } from './optimizly_error'; |
| 21 | + |
| 22 | +const mockMessageResolver = (prefix = '') => { |
| 23 | + return { |
| 24 | + resolve: vi.fn().mockImplementation((message) => `${prefix} ${message}`), |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +describe('ErrorReporter', () => { |
| 29 | + it('should call the logger and errorNotifier with the first argument if it is an Error object', () => { |
| 30 | + const logger = { error: vi.fn() }; |
| 31 | + const errorNotifier = { notify: vi.fn() }; |
| 32 | + const errorReporter = new ErrorReporter(logger as any, errorNotifier as any); |
| 33 | + |
| 34 | + const error = new Error('error'); |
| 35 | + errorReporter.report(error); |
| 36 | + |
| 37 | + expect(logger.error).toHaveBeenCalledWith(error); |
| 38 | + expect(errorNotifier.notify).toHaveBeenCalledWith(error); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should create an OptimizelyError and call the logger and errorNotifier with it if the first argument is a string', () => { |
| 42 | + const logger = { error: vi.fn() }; |
| 43 | + const errorNotifier = { notify: vi.fn() }; |
| 44 | + const errorReporter = new ErrorReporter(logger as any, errorNotifier as any); |
| 45 | + |
| 46 | + errorReporter.report('message', 1, 2); |
| 47 | + |
| 48 | + expect(logger.error).toHaveBeenCalled(); |
| 49 | + const loggedError = logger.error.mock.calls[0][0]; |
| 50 | + expect(loggedError).toBeInstanceOf(OptimizelyError); |
| 51 | + expect(loggedError.baseMessage).toBe('message'); |
| 52 | + expect(loggedError.params).toEqual([1, 2]); |
| 53 | + |
| 54 | + expect(errorNotifier.notify).toHaveBeenCalled(); |
| 55 | + const notifiedError = errorNotifier.notify.mock.calls[0][0]; |
| 56 | + expect(notifiedError).toBeInstanceOf(OptimizelyError); |
| 57 | + expect(notifiedError.baseMessage).toBe('message'); |
| 58 | + expect(notifiedError.params).toEqual([1, 2]); |
| 59 | + }); |
| 60 | +}); |
0 commit comments