|
| 1 | +/** |
| 2 | + * © 2014 Liferay, Inc. <https://liferay.com> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +import Logger from '../logger'; |
| 8 | + |
| 9 | +/* eslint-disable no-console */ |
| 10 | + |
| 11 | +const savedError = console.error; |
| 12 | +const savedWarn = console.warn; |
| 13 | +const savedInfo = console.info; |
| 14 | +const savedDebug = console.debug; |
| 15 | +const savedLog = console.log; |
| 16 | + |
| 17 | +describe('Logger', function() { |
| 18 | + beforeEach(() => { |
| 19 | + console.error = jest.fn(); |
| 20 | + console.warn = jest.fn(); |
| 21 | + console.info = jest.fn(); |
| 22 | + console.debug = jest.fn(); |
| 23 | + console.log = jest.fn(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + console.error = savedError; |
| 28 | + console.warn = savedWarn; |
| 29 | + console.info = savedInfo; |
| 30 | + console.debug = savedDebug; |
| 31 | + console.log = savedLog; |
| 32 | + }); |
| 33 | + |
| 34 | + it('off does not write any message', () => { |
| 35 | + const log = new Logger({logLevel: 'off'}); |
| 36 | + |
| 37 | + log.error('an error'); |
| 38 | + log.warn('a warn'); |
| 39 | + log.info('an info'); |
| 40 | + log.debug('a debug'); |
| 41 | + |
| 42 | + expect(console.error.mock.calls).toHaveLength(0); |
| 43 | + expect(console.warn.mock.calls).toHaveLength(0); |
| 44 | + expect(console.info.mock.calls).toHaveLength(0); |
| 45 | + expect(console.debug.mock.calls).toHaveLength(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it('error writes error messages only', () => { |
| 49 | + const log = new Logger({logLevel: 'error'}); |
| 50 | + |
| 51 | + log.error('an error'); |
| 52 | + log.warn('a warn'); |
| 53 | + log.info('an info'); |
| 54 | + log.debug('a debug'); |
| 55 | + |
| 56 | + expect(console.error.mock.calls).toHaveLength(1); |
| 57 | + expect(console.error.mock.calls[0]).toEqual([ |
| 58 | + 'liferay-amd-loader |', |
| 59 | + 'an error', |
| 60 | + ]); |
| 61 | + expect(console.warn.mock.calls).toHaveLength(0); |
| 62 | + expect(console.info.mock.calls).toHaveLength(0); |
| 63 | + expect(console.debug.mock.calls).toHaveLength(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it('warn writes error and warn messages only', () => { |
| 67 | + const log = new Logger({logLevel: 'warn'}); |
| 68 | + |
| 69 | + log.error('an error'); |
| 70 | + log.warn('a warn'); |
| 71 | + log.info('an info'); |
| 72 | + log.debug('a debug'); |
| 73 | + |
| 74 | + expect(console.error.mock.calls).toHaveLength(1); |
| 75 | + expect(console.error.mock.calls[0]).toEqual([ |
| 76 | + 'liferay-amd-loader |', |
| 77 | + 'an error', |
| 78 | + ]); |
| 79 | + expect(console.warn.mock.calls).toHaveLength(1); |
| 80 | + expect(console.warn.mock.calls[0]).toEqual([ |
| 81 | + 'liferay-amd-loader |', |
| 82 | + 'a warn', |
| 83 | + ]); |
| 84 | + expect(console.info.mock.calls).toHaveLength(0); |
| 85 | + expect(console.debug.mock.calls).toHaveLength(0); |
| 86 | + }); |
| 87 | + |
| 88 | + it('info writes error, warn and info messages only', () => { |
| 89 | + const log = new Logger({logLevel: 'info'}); |
| 90 | + |
| 91 | + log.error('an error'); |
| 92 | + log.warn('a warn'); |
| 93 | + log.info('an info'); |
| 94 | + log.debug('a debug'); |
| 95 | + |
| 96 | + expect(console.error.mock.calls).toHaveLength(1); |
| 97 | + expect(console.error.mock.calls[0]).toEqual([ |
| 98 | + 'liferay-amd-loader |', |
| 99 | + 'an error', |
| 100 | + ]); |
| 101 | + expect(console.warn.mock.calls).toHaveLength(1); |
| 102 | + expect(console.warn.mock.calls[0]).toEqual([ |
| 103 | + 'liferay-amd-loader |', |
| 104 | + 'a warn', |
| 105 | + ]); |
| 106 | + expect(console.info.mock.calls).toHaveLength(1); |
| 107 | + expect(console.info.mock.calls[0]).toEqual([ |
| 108 | + 'liferay-amd-loader |', |
| 109 | + 'an info', |
| 110 | + ]); |
| 111 | + expect(console.debug.mock.calls).toHaveLength(0); |
| 112 | + }); |
| 113 | + |
| 114 | + it('debug writes all messages', () => { |
| 115 | + const log = new Logger({logLevel: 'debug'}); |
| 116 | + |
| 117 | + log.error('an error'); |
| 118 | + log.warn('a warn'); |
| 119 | + log.info('an info'); |
| 120 | + log.debug('a debug'); |
| 121 | + |
| 122 | + expect(console.error.mock.calls).toHaveLength(1); |
| 123 | + expect(console.error.mock.calls[0]).toEqual([ |
| 124 | + 'liferay-amd-loader |', |
| 125 | + 'an error', |
| 126 | + ]); |
| 127 | + expect(console.warn.mock.calls).toHaveLength(1); |
| 128 | + expect(console.warn.mock.calls[0]).toEqual([ |
| 129 | + 'liferay-amd-loader |', |
| 130 | + 'a warn', |
| 131 | + ]); |
| 132 | + expect(console.info.mock.calls).toHaveLength(1); |
| 133 | + expect(console.info.mock.calls[0]).toEqual([ |
| 134 | + 'liferay-amd-loader |', |
| 135 | + 'an info', |
| 136 | + ]); |
| 137 | + expect(console.debug.mock.calls).toHaveLength(1); |
| 138 | + expect(console.debug.mock.calls[0]).toEqual([ |
| 139 | + 'liferay-amd-loader |', |
| 140 | + 'a debug', |
| 141 | + ]); |
| 142 | + }); |
| 143 | +}); |
0 commit comments