|
| 1 | +/** |
| 2 | + * Copyright 2024 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 | + * http://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 * as optimizely from '@optimizely/optimizely-sdk'; |
| 18 | +import { logger } from './logger'; |
| 19 | +import { sprintf } from './utils'; |
| 20 | + |
| 21 | +jest.mock('@optimizely/optimizely-sdk', () => ({ |
| 22 | + getLogger: jest.fn().mockReturnValue({ |
| 23 | + log: jest.fn(), |
| 24 | + }), |
| 25 | + enums: { |
| 26 | + LOG_LEVEL: { |
| 27 | + WARNING: 'WARNING', |
| 28 | + INFO: 'INFO', |
| 29 | + DEBUG: 'DEBUG', |
| 30 | + ERROR: 'ERROR', |
| 31 | + }, |
| 32 | + }, |
| 33 | +})); |
| 34 | + |
| 35 | +describe('logger module', () => { |
| 36 | + const mockLogHandler = optimizely.getLogger('ReactSDK'); |
| 37 | + const logSpy = mockLogHandler.log as jest.Mock; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should log a warning message', () => { |
| 44 | + const message = 'This is a warning: %s'; |
| 45 | + const arg = 'something went wrong'; |
| 46 | + |
| 47 | + logger.warn(message, arg); |
| 48 | + |
| 49 | + expect(logSpy).toHaveBeenCalledWith(optimizely.enums.LOG_LEVEL.WARNING, sprintf(message, arg)); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should log an info message', () => { |
| 53 | + const message = 'This is an info: %s'; |
| 54 | + const arg = 'all good'; |
| 55 | + |
| 56 | + logger.info(message, arg); |
| 57 | + |
| 58 | + expect(logSpy).toHaveBeenCalledWith(optimizely.enums.LOG_LEVEL.INFO, sprintf(message, arg)); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should log a debug message', () => { |
| 62 | + const message = 'Debugging: %s'; |
| 63 | + const arg = 'checking details'; |
| 64 | + |
| 65 | + logger.debug(message, arg); |
| 66 | + |
| 67 | + expect(logSpy).toHaveBeenCalledWith(optimizely.enums.LOG_LEVEL.DEBUG, sprintf(message, arg)); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should log an error message', () => { |
| 71 | + const message = 'Error occurred: %s'; |
| 72 | + const arg = 'critical failure'; |
| 73 | + |
| 74 | + logger.error(message, arg); |
| 75 | + |
| 76 | + expect(logSpy).toHaveBeenCalledWith(optimizely.enums.LOG_LEVEL.ERROR, sprintf(message, arg)); |
| 77 | + }); |
| 78 | +}); |
0 commit comments