| 
 | 1 | +import { config } from '@global/config';  | 
 | 2 | + | 
 | 3 | +import { printIonError, printIonWarning } from '../index';  | 
 | 4 | + | 
 | 5 | +describe('Logging', () => {  | 
 | 6 | +  describe('#printIonWarning', () => {  | 
 | 7 | +    let consoleWarnSpy: jest.SpyInstance;  | 
 | 8 | + | 
 | 9 | +    beforeEach(() => {  | 
 | 10 | +      consoleWarnSpy = jest.spyOn(console, 'warn');  | 
 | 11 | +      // Suppress console.warn output from polluting the test output  | 
 | 12 | +      consoleWarnSpy.mockImplementation(() => {});  | 
 | 13 | +    });  | 
 | 14 | + | 
 | 15 | +    afterEach(() => {  | 
 | 16 | +      consoleWarnSpy.mockRestore();  | 
 | 17 | +    });  | 
 | 18 | + | 
 | 19 | +    describe('when the logLevel configuration is not set', () => {  | 
 | 20 | +      it('logs a warning to the console', () => {  | 
 | 21 | +        config.set('logLevel', undefined);  | 
 | 22 | + | 
 | 23 | +        printIonWarning('This is a warning message');  | 
 | 24 | + | 
 | 25 | +        expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message');  | 
 | 26 | +      });  | 
 | 27 | +    });  | 
 | 28 | + | 
 | 29 | +    describe("when the logLevel configuration is set to 'ERROR'", () => {  | 
 | 30 | +      it('logs a warning to the console', () => {  | 
 | 31 | +        config.set('logLevel', 'ERROR');  | 
 | 32 | + | 
 | 33 | +        printIonWarning('This is a warning message');  | 
 | 34 | + | 
 | 35 | +        expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message');  | 
 | 36 | +      });  | 
 | 37 | +    });  | 
 | 38 | + | 
 | 39 | +    describe("when the logLevel configuration is set to 'WARN'", () => {  | 
 | 40 | +      it('logs a warning to the console', () => {  | 
 | 41 | +        config.set('logLevel', 'WARN');  | 
 | 42 | + | 
 | 43 | +        printIonWarning('This is a warning message');  | 
 | 44 | + | 
 | 45 | +        expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message');  | 
 | 46 | +      });  | 
 | 47 | +    });  | 
 | 48 | + | 
 | 49 | +    describe("when the logLevel configuration is set to 'OFF'", () => {  | 
 | 50 | +      it('does not log a warning to the console', () => {  | 
 | 51 | +        config.set('logLevel', 'OFF');  | 
 | 52 | + | 
 | 53 | +        printIonWarning('This is a warning message');  | 
 | 54 | + | 
 | 55 | +        expect(consoleWarnSpy).not.toHaveBeenCalled();  | 
 | 56 | +      });  | 
 | 57 | +    });  | 
 | 58 | +  });  | 
 | 59 | + | 
 | 60 | +  describe('#printIonError', () => {  | 
 | 61 | +    let consoleErrorSpy: jest.SpyInstance;  | 
 | 62 | + | 
 | 63 | +    beforeEach(() => {  | 
 | 64 | +      consoleErrorSpy = jest.spyOn(console, 'error');  | 
 | 65 | +      // Suppress console.error output from polluting the test output  | 
 | 66 | +      consoleErrorSpy.mockImplementation(() => {});  | 
 | 67 | +    });  | 
 | 68 | + | 
 | 69 | +    afterEach(() => {  | 
 | 70 | +      consoleErrorSpy.mockRestore();  | 
 | 71 | +    });  | 
 | 72 | + | 
 | 73 | +    describe('when the logLevel configuration is not set', () => {  | 
 | 74 | +      it('logs an error to the console', () => {  | 
 | 75 | +        config.set('logLevel', undefined);  | 
 | 76 | + | 
 | 77 | +        printIonError('This is an error message');  | 
 | 78 | + | 
 | 79 | +        expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message');  | 
 | 80 | +      });  | 
 | 81 | +    });  | 
 | 82 | + | 
 | 83 | +    describe("when the logLevel configuration is set to 'ERROR'", () => {  | 
 | 84 | +      it('logs an error to the console', () => {  | 
 | 85 | +        config.set('logLevel', 'ERROR');  | 
 | 86 | + | 
 | 87 | +        printIonError('This is an error message');  | 
 | 88 | + | 
 | 89 | +        expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message');  | 
 | 90 | +      });  | 
 | 91 | +    });  | 
 | 92 | + | 
 | 93 | +    describe("when the logLevel configuration is set to 'WARN'", () => {  | 
 | 94 | +      it('does not log an error to the console', () => {  | 
 | 95 | +        config.set('logLevel', 'WARN');  | 
 | 96 | + | 
 | 97 | +        printIonError('This is an error message');  | 
 | 98 | + | 
 | 99 | +        expect(consoleErrorSpy).not.toHaveBeenCalled();  | 
 | 100 | +      });  | 
 | 101 | +    });  | 
 | 102 | + | 
 | 103 | +    describe("when the logLevel configuration is set to 'OFF'", () => {  | 
 | 104 | +      it('does not log an error to the console', () => {  | 
 | 105 | +        config.set('logLevel', 'OFF');  | 
 | 106 | + | 
 | 107 | +        printIonError('This is an error message');  | 
 | 108 | + | 
 | 109 | +        expect(consoleErrorSpy).not.toHaveBeenCalled();  | 
 | 110 | +      });  | 
 | 111 | +    });  | 
 | 112 | +  });  | 
 | 113 | +});  | 
0 commit comments