|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
| 16 | + |
| 17 | +import { expect } from "chai"; |
| 18 | +import { spy as Spy } from "sinon"; |
| 19 | +import { Logger, LogLevel } from "../src/logger"; |
| 20 | +import { setLogLevel } from "../index"; |
| 21 | +import { debug } from "util"; |
| 22 | + |
| 23 | +describe('@firebase/logger', () => { |
| 24 | + const message = 'Hello there!'; |
| 25 | + let client: Logger; |
| 26 | + const spies = { |
| 27 | + debugSpy: null, |
| 28 | + logSpy: null, |
| 29 | + infoSpy: null, |
| 30 | + warnSpy: null, |
| 31 | + errorSpy: null |
| 32 | + } |
| 33 | + /** |
| 34 | + * Before each test, instantiate a new instance of Logger and set the log |
| 35 | + * level so that all |
| 36 | + * |
| 37 | + * Also spy on all of the console methods so we can assert against them as |
| 38 | + * needed |
| 39 | + */ |
| 40 | + beforeEach(() => { |
| 41 | + client = new Logger(); |
| 42 | + |
| 43 | + spies.debugSpy = Spy(console, 'debug'); |
| 44 | + spies.logSpy = Spy(console, 'log'); |
| 45 | + spies.infoSpy = Spy(console, 'info'); |
| 46 | + spies.warnSpy = Spy(console, 'warn'); |
| 47 | + spies.errorSpy = Spy(console, 'error'); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + spies.debugSpy.restore(); |
| 52 | + spies.logSpy.restore(); |
| 53 | + spies.infoSpy.restore(); |
| 54 | + spies.warnSpy.restore(); |
| 55 | + spies.errorSpy.restore(); |
| 56 | + }); |
| 57 | + |
| 58 | + function testLog(message, channel, shouldLog) { |
| 59 | + it(`Should ${shouldLog ? '' : 'not'} call \`console.${channel}\` if \`.${channel}\` is called`, () => { |
| 60 | + client[channel](message); |
| 61 | + expect( |
| 62 | + spies[`${channel}Spy`] && spies[`${channel}Spy`].called, |
| 63 | + `Expected ${channel} to ${shouldLog ? '' : 'not'} log` |
| 64 | + ).to.be[shouldLog ? 'true' : 'false']; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + describe('Class instance methods', () => { |
| 69 | + /** |
| 70 | + * Allow all logs to be exported for this block of tests |
| 71 | + */ |
| 72 | + before(() => { |
| 73 | + setLogLevel(LogLevel.DEBUG); |
| 74 | + }); |
| 75 | + testLog(message, 'debug', true); |
| 76 | + testLog(message, 'log', true); |
| 77 | + testLog(message, 'info', true); |
| 78 | + testLog(message, 'warn', true); |
| 79 | + testLog(message, 'error', true); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('Defaults', () => { |
| 83 | + before(() => { |
| 84 | + setLogLevel(LogLevel.WARN); |
| 85 | + }); |
| 86 | + testLog(message, 'debug', false); |
| 87 | + testLog(message, 'log', false); |
| 88 | + testLog(message, 'info', false); |
| 89 | + testLog(message, 'warn', true); |
| 90 | + testLog(message, 'error', true); |
| 91 | + }); |
| 92 | +}); |
0 commit comments