|
| 1 | +/** |
| 2 | + * Copyright 2026, 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 | + |
| 17 | +import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 18 | +import { DEBUG, INFO, WARN, ERROR, LogLevel } from '@optimizely/optimizely-sdk'; |
| 19 | +import type { LogHandler } from '@optimizely/optimizely-sdk'; |
| 20 | +import { createReactLogger } from './ReactLogger'; |
| 21 | +import type { ReactLogger } from './ReactLogger'; |
| 22 | + |
| 23 | +const mockOpaqueLogger = vi.hoisted(() => ({ __opaque: true })); |
| 24 | + |
| 25 | +vi.mock('@optimizely/optimizely-sdk', async (importOriginal) => { |
| 26 | + const original = await importOriginal<typeof import('@optimizely/optimizely-sdk')>(); |
| 27 | + return { |
| 28 | + ...original, |
| 29 | + createLogger: vi.fn().mockReturnValue(mockOpaqueLogger), |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +import { createLogger, REACT_LOGGER } from './createLogger'; |
| 34 | + |
| 35 | +describe('createLogger', () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return the opaque logger from the JS SDK', () => { |
| 41 | + const result = createLogger({ level: INFO }); |
| 42 | + expect(result).toBe(mockOpaqueLogger); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should attach a ReactLogger via the REACT_LOGGER symbol', () => { |
| 46 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 47 | + const result = createLogger({ level: INFO, logHandler: mockHandler }); |
| 48 | + |
| 49 | + const reactLogger = (result as Record<symbol, unknown>)[REACT_LOGGER] as ReactLogger; |
| 50 | + expect(reactLogger).toBeDefined(); |
| 51 | + expect(reactLogger.debug).toBeTypeOf('function'); |
| 52 | + expect(reactLogger.info).toBeTypeOf('function'); |
| 53 | + expect(reactLogger.warn).toBeTypeOf('function'); |
| 54 | + expect(reactLogger.error).toBeTypeOf('function'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should create a ReactLogger that uses the provided logHandler', () => { |
| 58 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 59 | + const result = createLogger({ level: INFO, logHandler: mockHandler }); |
| 60 | + |
| 61 | + const reactLogger = (result as Record<symbol, unknown>)[REACT_LOGGER] as ReactLogger; |
| 62 | + reactLogger.info('hello'); |
| 63 | + |
| 64 | + expect(mockHandler.log).toHaveBeenCalledWith(LogLevel.Info, '[OPTIMIZELY - REACT] - INFO hello'); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('log level resolution', () => { |
| 68 | + it.each([ |
| 69 | + { preset: DEBUG, expectedCalls: 4, name: 'DEBUG' }, |
| 70 | + { preset: INFO, expectedCalls: 3, name: 'INFO' }, |
| 71 | + { preset: WARN, expectedCalls: 2, name: 'WARN' }, |
| 72 | + { preset: ERROR, expectedCalls: 1, name: 'ERROR' }, |
| 73 | + ])('should resolve $name preset correctly', ({ preset, expectedCalls }) => { |
| 74 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 75 | + const result = createLogger({ level: preset, logHandler: mockHandler }); |
| 76 | + |
| 77 | + const reactLogger = (result as Record<symbol, unknown>)[REACT_LOGGER] as ReactLogger; |
| 78 | + reactLogger.debug('d'); |
| 79 | + reactLogger.info('i'); |
| 80 | + reactLogger.warn('w'); |
| 81 | + reactLogger.error('e'); |
| 82 | + expect(mockHandler.log).toHaveBeenCalledTimes(expectedCalls); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('createReactLogger', () => { |
| 88 | + describe('log level filtering', () => { |
| 89 | + it('should filter messages below the configured level', () => { |
| 90 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 91 | + const logger = createReactLogger({ logLevel: LogLevel.Warn, logHandler: mockHandler }); |
| 92 | + |
| 93 | + logger.debug('should not appear'); |
| 94 | + logger.info('should not appear'); |
| 95 | + logger.warn('should appear'); |
| 96 | + logger.error('should appear'); |
| 97 | + |
| 98 | + expect(mockHandler.log).toHaveBeenCalledTimes(2); |
| 99 | + expect(mockHandler.log).toHaveBeenCalledWith(LogLevel.Warn, '[OPTIMIZELY - REACT] - WARN should appear'); |
| 100 | + expect(mockHandler.log).toHaveBeenCalledWith(LogLevel.Error, '[OPTIMIZELY - REACT] - ERROR should appear'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should allow all messages when level is Debug', () => { |
| 104 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 105 | + const logger = createReactLogger({ logLevel: LogLevel.Debug, logHandler: mockHandler }); |
| 106 | + |
| 107 | + logger.debug('d'); |
| 108 | + logger.info('i'); |
| 109 | + logger.warn('w'); |
| 110 | + logger.error('e'); |
| 111 | + |
| 112 | + expect(mockHandler.log).toHaveBeenCalledTimes(4); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should only allow error messages when level is Error', () => { |
| 116 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 117 | + const logger = createReactLogger({ logLevel: LogLevel.Error, logHandler: mockHandler }); |
| 118 | + |
| 119 | + logger.debug('d'); |
| 120 | + logger.info('i'); |
| 121 | + logger.warn('w'); |
| 122 | + logger.error('e'); |
| 123 | + |
| 124 | + expect(mockHandler.log).toHaveBeenCalledTimes(1); |
| 125 | + expect(mockHandler.log).toHaveBeenCalledWith(LogLevel.Error, '[OPTIMIZELY - REACT] - ERROR e'); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('log handler', () => { |
| 130 | + it('should use the provided logHandler', () => { |
| 131 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 132 | + const logger = createReactLogger({ logLevel: LogLevel.Info, logHandler: mockHandler }); |
| 133 | + |
| 134 | + logger.info('hello'); |
| 135 | + |
| 136 | + expect(mockHandler.log).toHaveBeenCalledWith(LogLevel.Info, '[OPTIMIZELY - REACT] - INFO hello'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should use default console handler when logHandler is not provided', () => { |
| 140 | + const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => {}); |
| 141 | + const logger = createReactLogger({ logLevel: LogLevel.Info }); |
| 142 | + |
| 143 | + logger.info('hello'); |
| 144 | + |
| 145 | + expect(consoleSpy).toHaveBeenCalledWith('[OPTIMIZELY - REACT] - INFO hello'); |
| 146 | + consoleSpy.mockRestore(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('message prefix', () => { |
| 151 | + it('should prepend [OPTIMIZELY - REACT] to all messages', () => { |
| 152 | + const mockHandler: LogHandler = { log: vi.fn() }; |
| 153 | + const logger = createReactLogger({ logLevel: LogLevel.Debug, logHandler: mockHandler }); |
| 154 | + |
| 155 | + logger.debug('test'); |
| 156 | + logger.info('test'); |
| 157 | + logger.warn('test'); |
| 158 | + logger.error('test'); |
| 159 | + |
| 160 | + for (const call of (mockHandler.log as ReturnType<typeof vi.fn>).mock.calls) { |
| 161 | + expect(call[1]).toMatch(/^\[OPTIMIZELY - REACT\] - (DEBUG|INFO|WARN|ERROR) /); |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments