|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +const LOG_LEVELS = { |
| 13 | + trace: 0, |
| 14 | + info: 1, |
| 15 | + warn: 2, |
| 16 | + error: 3, |
| 17 | +}; |
| 18 | + |
| 19 | +describe('console', () => { |
| 20 | + describe('.table(data, rows)', () => { |
| 21 | + it('should print the passed array as a table', () => { |
| 22 | + const originalNativeLoggingHook = global.nativeLoggingHook; |
| 23 | + const logFn = (global.nativeLoggingHook = jest.fn()); |
| 24 | + |
| 25 | + // TODO: replace with `beforeEach` when supported. |
| 26 | + try { |
| 27 | + console.table([ |
| 28 | + {name: 'First', value: 500}, |
| 29 | + {name: 'Second', value: 600}, |
| 30 | + {name: 'Third', value: 700}, |
| 31 | + {name: 'Fourth', value: 800, extraValue: true}, |
| 32 | + ]); |
| 33 | + |
| 34 | + expect(logFn).toHaveBeenCalledTimes(1); |
| 35 | + expect(logFn.mock.lastCall).toEqual([ |
| 36 | + ` |
| 37 | +name | value |
| 38 | +-------|------ |
| 39 | +First | 500 \u0020 |
| 40 | +Second | 600 \u0020 |
| 41 | +Third | 700 \u0020 |
| 42 | +Fourth | 800 `, |
| 43 | + LOG_LEVELS.info, |
| 44 | + ]); |
| 45 | + } finally { |
| 46 | + global.nativeLoggingHook = originalNativeLoggingHook; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it('should print the passed dictionary as a table', () => { |
| 51 | + const originalNativeLoggingHook = global.nativeLoggingHook; |
| 52 | + const logFn = (global.nativeLoggingHook = jest.fn()); |
| 53 | + |
| 54 | + // TODO: replace with `beforeEach` when supported. |
| 55 | + try { |
| 56 | + console.table({ |
| 57 | + first: {name: 'First', value: 500}, |
| 58 | + second: {name: 'Second', value: 600}, |
| 59 | + third: {name: 'Third', value: 700}, |
| 60 | + fourth: {name: 'Fourth', value: 800, extraValue: true}, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(logFn).toHaveBeenCalledTimes(1); |
| 64 | + expect(logFn.mock.lastCall).toEqual([ |
| 65 | + ` |
| 66 | +(index) | name | value |
| 67 | +--------|--------|------ |
| 68 | +first | First | 500 \u0020 |
| 69 | +second | Second | 600 \u0020 |
| 70 | +third | Third | 700 \u0020 |
| 71 | +fourth | Fourth | 800 `, |
| 72 | + LOG_LEVELS.info, |
| 73 | + ]); |
| 74 | + } finally { |
| 75 | + global.nativeLoggingHook = originalNativeLoggingHook; |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it('should print an empty string for empty arrays', () => { |
| 80 | + const originalNativeLoggingHook = global.nativeLoggingHook; |
| 81 | + const logFn = (global.nativeLoggingHook = jest.fn()); |
| 82 | + |
| 83 | + // TODO: replace with `beforeEach` when supported. |
| 84 | + try { |
| 85 | + console.table([]); |
| 86 | + |
| 87 | + expect(logFn).toHaveBeenCalledTimes(1); |
| 88 | + expect(logFn.mock.lastCall).toEqual([``, LOG_LEVELS.info]); |
| 89 | + } finally { |
| 90 | + global.nativeLoggingHook = originalNativeLoggingHook; |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('should print an empty string for empty dictionaries', () => { |
| 95 | + const originalNativeLoggingHook = global.nativeLoggingHook; |
| 96 | + const logFn = (global.nativeLoggingHook = jest.fn()); |
| 97 | + |
| 98 | + // TODO: replace with `beforeEach` when supported. |
| 99 | + try { |
| 100 | + console.table({}); |
| 101 | + |
| 102 | + expect(logFn).toHaveBeenCalledTimes(1); |
| 103 | + expect(logFn.mock.lastCall).toEqual([``, LOG_LEVELS.info]); |
| 104 | + } finally { |
| 105 | + global.nativeLoggingHook = originalNativeLoggingHook; |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + // This test is currently failing |
| 110 | + it.skip('should print an indices table for an array of empty objects', () => { |
| 111 | + const originalNativeLoggingHook = global.nativeLoggingHook; |
| 112 | + const logFn = (global.nativeLoggingHook = jest.fn()); |
| 113 | + |
| 114 | + // TODO: replace with `beforeEach` when supported. |
| 115 | + try { |
| 116 | + console.table([{}, {}, {}, {}]); |
| 117 | + |
| 118 | + expect(logFn).toHaveBeenCalledTimes(1); |
| 119 | + expect(logFn.mock.lastCall).toEqual([ |
| 120 | + ` |
| 121 | +(index) |
| 122 | +------- |
| 123 | +0 \u0020 |
| 124 | +1 \u0020 |
| 125 | +2 \u0020 |
| 126 | +3 `, |
| 127 | + LOG_LEVELS.info, |
| 128 | + ]); |
| 129 | + } finally { |
| 130 | + global.nativeLoggingHook = originalNativeLoggingHook; |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it('should print an indices table for a dictionary of empty objects', () => { |
| 135 | + const originalNativeLoggingHook = global.nativeLoggingHook; |
| 136 | + const logFn = (global.nativeLoggingHook = jest.fn()); |
| 137 | + |
| 138 | + // TODO: replace with `beforeEach` when supported. |
| 139 | + try { |
| 140 | + console.table({ |
| 141 | + first: {}, |
| 142 | + second: {}, |
| 143 | + third: {}, |
| 144 | + fourth: {}, |
| 145 | + }); |
| 146 | + |
| 147 | + expect(logFn).toHaveBeenCalledTimes(1); |
| 148 | + expect(logFn.mock.lastCall).toEqual([ |
| 149 | + ` |
| 150 | +(index) |
| 151 | +------- |
| 152 | +first \u0020 |
| 153 | +second\u0020 |
| 154 | +third \u0020 |
| 155 | +fourth `, |
| 156 | + LOG_LEVELS.info, |
| 157 | + ]); |
| 158 | + } finally { |
| 159 | + global.nativeLoggingHook = originalNativeLoggingHook; |
| 160 | + } |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments