Skip to content

Commit 2c26938

Browse files
[FSSDK-10616] logger improvement
1 parent c201a23 commit 2c26938

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/logger.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
});

src/logger.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Optimizely
2+
* Copyright 2022,2024 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)