|
| 1 | +import { InstabugRNConfig } from '../../src/utils/config'; |
| 2 | +import { LogLevel } from '../../src'; |
| 3 | +import { Logger } from '../../src/utils/logger'; |
| 4 | + |
| 5 | +describe('Logger', () => { |
| 6 | + const consoleMethods = { |
| 7 | + error: jest.spyOn(console, 'error').mockImplementation(() => {}), |
| 8 | + info: jest.spyOn(console, 'info').mockImplementation(() => {}), |
| 9 | + log: jest.spyOn(console, 'log').mockImplementation(() => {}), |
| 10 | + warn: jest.spyOn(console, 'warn').mockImplementation(() => {}), |
| 11 | + trace: jest.spyOn(console, 'trace').mockImplementation(() => {}), |
| 12 | + debug: jest.spyOn(console, 'debug').mockImplementation(() => {}), |
| 13 | + }; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + const allLevels: LogLevel[] = [LogLevel.none, LogLevel.error, LogLevel.debug, LogLevel.verbose]; |
| 20 | + |
| 21 | + it.each(allLevels)('should respect logging level: %s', (level) => { |
| 22 | + InstabugRNConfig.debugLogsLevel = level; |
| 23 | + |
| 24 | + Logger.error('error'); |
| 25 | + Logger.info('info'); |
| 26 | + Logger.log('log'); |
| 27 | + Logger.warn('warn'); |
| 28 | + Logger.trace('trace'); |
| 29 | + Logger.debug('debug'); |
| 30 | + |
| 31 | + const logLevelHierarchy: Record<LogLevel, number> = { |
| 32 | + [LogLevel.verbose]: 3, |
| 33 | + [LogLevel.debug]: 2, |
| 34 | + [LogLevel.error]: 1, |
| 35 | + [LogLevel.none]: 0, |
| 36 | + }; |
| 37 | + |
| 38 | + const current = logLevelHierarchy[level]; |
| 39 | + |
| 40 | + expect(consoleMethods.error).toHaveBeenCalledTimes(current >= 1 ? 1 : 0); |
| 41 | + expect(consoleMethods.info).toHaveBeenCalledTimes(current >= 3 ? 1 : 0); |
| 42 | + expect(consoleMethods.log).toHaveBeenCalledTimes(current >= 3 ? 1 : 0); |
| 43 | + expect(consoleMethods.warn).toHaveBeenCalledTimes(current >= 2 ? 1 : 0); |
| 44 | + expect(consoleMethods.trace).toHaveBeenCalledTimes(current >= 2 ? 1 : 0); |
| 45 | + expect(consoleMethods.debug).toHaveBeenCalledTimes(current >= 2 ? 1 : 0); |
| 46 | + }); |
| 47 | + |
| 48 | + it('passes message and params correctly', () => { |
| 49 | + InstabugRNConfig.debugLogsLevel = LogLevel.verbose; |
| 50 | + |
| 51 | + Logger.log('test message', 'extra', 123); |
| 52 | + |
| 53 | + expect(consoleMethods.log).toHaveBeenCalledWith('test message', 'extra', 123); |
| 54 | + }); |
| 55 | +}); |
0 commit comments