Skip to content

Commit 20b12dc

Browse files
committed
Add customization for group rendering & collapsing of groups
1 parent 21580cb commit 20b12dc

File tree

6 files changed

+115
-12
lines changed

6 files changed

+115
-12
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
},
1111
"scripts": {
1212
"start:demo": "parcel demo/index.html --dist-dir ./build",
13-
"start": "del-cli 'dist/*' && parcel src/index.tsx --dist-dir ./dist",
1413
"build": "del-cli 'dist/*' && parcel build src/index.tsx --dist-dir ./dist && npm run size",
1514
"test": "jest",
1615
"lint": "npx eslint \"./**/*.ts\" --cache --cache-strategy content",

src/index.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ describe('useLog', () => {
88
const consoleGroup = jest
99
.spyOn(console, 'group')
1010
.mockImplementation(() => null)
11+
const consoleGroupCollapsed = jest
12+
.spyOn(console, 'groupCollapsed')
13+
.mockImplementation(() => null)
1114

1215
beforeEach(() => {
1316
jest.useFakeTimers()
@@ -238,4 +241,27 @@ describe('useLog', () => {
238241

239242
expect(consoleLog).not.toBeCalled()
240243
})
244+
245+
it('renders log with disabled groups', () => {
246+
renderHook(() => {
247+
const { log } = useLog({ isGroupingEnabled: true })
248+
log('Test', { isGroupingEnabled: false })
249+
})
250+
251+
expect(consoleGroup).not.toHaveBeenCalled()
252+
})
253+
254+
it('renders log with disabled groups', () => {
255+
renderHook(() => {
256+
const { log } = useLog({ isGroupingEnabled: false })
257+
log('Test', { isGroupingEnabled: true, isGroupCollapsed: true })
258+
})
259+
260+
expect(consoleGroup).not.toHaveBeenCalled()
261+
expect(consoleGroupCollapsed).toHaveBeenCalled()
262+
// first call, first parameter for group name should exist
263+
expect(consoleGroupCollapsed.mock.calls[0][0]).toBe(
264+
`Mount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
265+
)
266+
})
241267
})

src/index.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
*/
88

99
import { useEffect, useRef } from 'react'
10-
import {
11-
ALLOWED_NODE_ENVS,
12-
CSS_CHANGE,
13-
CSS_COMPONENT,
14-
CSS_SUB_VALUE,
15-
} from './constants'
1610
import {
1711
UseLogConfig,
1812
UseLogReturn,
@@ -21,6 +15,12 @@ import {
2115
_PrintConfig,
2216
} from './types'
2317
import { getComponentName, print } from './utils'
18+
import {
19+
ALLOWED_NODE_ENVS,
20+
CSS_CHANGE,
21+
CSS_COMPONENT,
22+
CSS_SUB_VALUE,
23+
} from './constants'
2424

2525
/**
2626
* Provides a function to log through react component lifecycle.
@@ -42,6 +42,8 @@ export function useLog({
4242
subValueCSS = CSS_SUB_VALUE,
4343
} = {},
4444
environments = ALLOWED_NODE_ENVS,
45+
isGroupingEnabled = true,
46+
isGroupCollapsed = false,
4547
}: UseLogConfig = {}): UseLogReturn {
4648
const componentName = getComponentName()
4749

@@ -63,7 +65,7 @@ export function useLog({
6365
const prevValueRef = useRef<T>()
6466
const printProps: Pick<
6567
_PrintConfig<T>,
66-
'value' | 'styles' | 'componentName'
68+
'value' | 'styles' | 'componentName' | 'flags'
6769
> = {
6870
value: clonedValue,
6971
styles: {
@@ -72,6 +74,10 @@ export function useLog({
7274
changeCSS: props?.styles?.changeCSS ?? changeCSS,
7375
},
7476
componentName,
77+
flags: {
78+
isGrouped: props?.isGroupingEnabled ?? isGroupingEnabled,
79+
isCollapsed: props?.isGroupCollapsed ?? isGroupCollapsed,
80+
},
7581
}
7682

7783
if (environments.includes(process.env.NODE_ENV ?? 'production')) {

src/types.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,25 @@ export interface Styles {
3535
}
3636

3737
/** Describes configuration object at component level */
38-
export interface UseLogConfig {
38+
export type UseLogConfig = {
3939
/** Contains styles object with different CSS inline styles used in logging */
4040
styles?: Styles
4141
/** Contains array of environments of `process.env.NODE_ENV` in which logging will be allowed */
4242
environments?: string[]
43-
}
43+
} & (
44+
| {
45+
/** Enable grouping for logs */
46+
isGroupingEnabled?: boolean
47+
/** Render groups collapsed */
48+
isGroupCollapsed?: boolean
49+
}
50+
| {
51+
/** Enable grouping for logs */
52+
isGroupingEnabled?: false
53+
/** Render groups collapsed */
54+
isGroupCollapsed?: never
55+
}
56+
)
4457

4558
/** Describes configuration object at call level, can be used to override configuration */
4659
export type LogConfig = UseLogConfig
@@ -65,8 +78,23 @@ export interface _PrintConfig<T> {
6578
type?: _PrintTypes
6679
styles?: Styles
6780
componentName: string
81+
flags?: _PrintFlags
6882
}
6983

84+
/**
85+
* Describes possible flags for internal print configuration
86+
* @internal
87+
*/
88+
export type _PrintFlags =
89+
| {
90+
isGrouped?: boolean
91+
isCollapsed?: boolean
92+
}
93+
| {
94+
isGrouped?: false
95+
isCollapsed?: never
96+
}
97+
7098
/**
7199
* Label types of print groups
72100
* @internal

src/utils.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ describe('utils', () => {
2727
const consoleGroup = jest
2828
.spyOn(console, 'group')
2929
.mockImplementation(() => null)
30+
const consoleGroupCollapsed = jest
31+
.spyOn(console, 'groupCollapsed')
32+
.mockImplementation(() => null)
3033
const consoleGroupEnd = jest
3134
.spyOn(console, 'groupEnd')
3235
.mockImplementation(() => null)
@@ -69,5 +72,37 @@ describe('utils', () => {
6972
expect(consoleLog).toHaveBeenCalledTimes(2)
7073
expect(consoleGroupEnd).toHaveBeenCalled()
7174
})
75+
76+
it('does not print group per config', () => {
77+
print({
78+
...printProps,
79+
flags: {
80+
isGrouped: false,
81+
},
82+
})
83+
84+
expect(consoleGroup).not.toHaveBeenCalled()
85+
expect(consoleLog).toHaveBeenCalledWith(' A Label: Test Value')
86+
expect(consoleGroupEnd).not.toHaveBeenCalled()
87+
})
88+
89+
it('prints collapsed group per config', () => {
90+
print({
91+
...printProps,
92+
flags: {
93+
isGrouped: true,
94+
isCollapsed: true,
95+
},
96+
})
97+
98+
expect(consoleGroup).not.toHaveBeenCalled()
99+
expect(consoleGroupCollapsed).toHaveBeenCalledWith(
100+
`Change in %c<SomeComponentName /> %c@ ${new Date().toLocaleTimeString()}`,
101+
undefined,
102+
undefined,
103+
)
104+
expect(consoleLog).toHaveBeenCalledWith(' A Label: Test Value')
105+
expect(consoleGroupEnd).toHaveBeenCalled()
106+
})
72107
})
73108
})

src/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@ export function print<T>({
3535
label,
3636
prevValue,
3737
componentName,
38+
flags = {
39+
isCollapsed: false,
40+
isGrouped: true,
41+
},
3842
type = _PrintTypes.Change,
3943
group = getGroupLabel(type, componentName),
4044
styles: { componentCSS, subValueCSS, changeCSS } = {},
4145
}: _PrintConfig<T>): void {
42-
console.group(group, componentCSS, subValueCSS)
46+
flags.isGrouped &&
47+
console[flags.isCollapsed ? 'groupCollapsed' : 'group'](
48+
group,
49+
componentCSS,
50+
subValueCSS,
51+
)
4352

4453
if (!('prevValue' in arguments[0])) {
4554
console.log(`${label.padStart(14, ' ')}: ${String(value)}`)
@@ -51,5 +60,5 @@ export function print<T>({
5160
console.log(` Current value: %c${String(value)}`, changeCSS)
5261
}
5362

54-
console.groupEnd()
63+
flags.isGrouped && console.groupEnd()
5564
}

0 commit comments

Comments
 (0)