Skip to content

Commit 73b276f

Browse files
committed
Add custom print function
1 parent 7f1ed91 commit 73b276f

File tree

5 files changed

+91
-4
lines changed

5 files changed

+91
-4
lines changed

src/index.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,28 @@ describe('useLog', () => {
344344
// first call, first parameter (group label) should be modified
345345
expect(consoleGroup.mock.calls[0][0]).toBe('Mount TestComponent')
346346
})
347+
348+
it('renders log with custom print function', () => {
349+
const render = jest.fn()
350+
const anotherRender = jest.fn()
351+
const printerSpy = jest.spyOn(utils, 'print')
352+
353+
renderHook(() => {
354+
const { log } = useLog({ render })
355+
log('Test', { render: anotherRender })
356+
})
357+
358+
expect(render).not.toHaveBeenCalled()
359+
expect(printerSpy).not.toHaveBeenCalled()
360+
expect(anotherRender).toHaveBeenCalledWith({
361+
componentName: 'TestComponent',
362+
flags: {
363+
isCollapsed: false,
364+
isGrouped: true,
365+
},
366+
prevValue: undefined,
367+
type: 'Mount',
368+
value: 'Test',
369+
})
370+
})
347371
})

src/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
_PrintConfig,
1616
Printer,
1717
} from './types'
18-
import { getComponentName, print } from './utils'
18+
import { getComponentName, getRenderFunctionProps, print } from './utils'
1919
import {
2020
ALLOWED_NODE_ENVS,
2121
CSS_CHANGE,
@@ -49,6 +49,7 @@ export function useLog({
4949
printer = console as Printer,
5050
logLevel = DEFAULT_LOG_LEVEL,
5151
groupLabelRenderer,
52+
render,
5253
}: UseLogConfig = {}): UseLogReturn {
5354
const componentName = getComponentName()
5455

@@ -98,22 +99,30 @@ export function useLog({
9899
function logHooks(): void {
99100
const isUnmounting = useRef(false)
100101

102+
const printFunc = (printProps: _PrintConfig<T>): void =>
103+
(props?.render ?? render ?? print)(
104+
getRenderFunctionProps(
105+
printProps,
106+
Boolean(props?.render ?? render),
107+
),
108+
)
109+
101110
useEffect(function setIsUnmounting() {
102111
return function setIsUnmountingOnMount() {
103112
isUnmounting.current = true
104113
}
105114
}, [])
106115

107116
useEffect(function onMount() {
108-
print({
117+
printFunc({
109118
type: ComponentLifecycleLabels.Mount,
110119
...printProps,
111120
})
112121

113122
prevValueRef.current = value
114123

115124
return function onUnmount() {
116-
print({
125+
printFunc({
117126
type: ComponentLifecycleLabels.Unmount,
118127
prevValue: prevValueRef.current,
119128
...printProps,
@@ -123,7 +132,7 @@ export function useLog({
123132

124133
useEffect(
125134
function onChange() {
126-
print({
135+
printFunc({
127136
type: ComponentLifecycleLabels.Change,
128137
prevValue: prevValueRef.current,
129138
...printProps,

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export type UseLogConfig = {
4242
/** Contains custom implementation of console */
4343
printer?: Printer | Console
4444
logLevel?: LogLevels
45+
/** Custom function which will be used for rendering the result, provided with useful data */
46+
render?: <T>(props: RenderProps<T>) => void
4547
} & (
4648
| {
4749
/** Enable grouping for logs */
@@ -71,6 +73,14 @@ export interface UseLogReturn {
7173
log: <T>(value: T, props?: LogConfig) => void
7274
}
7375

76+
/** Describes input parameters for custom printer function */
77+
export type RenderProps<T> = Pick<
78+
_PrintConfig<T>,
79+
'value' | 'prevValue' | 'type' | 'componentName'
80+
> & {
81+
flags?: Pick<_PrintFlags, 'isGrouped' | 'isCollapsed'>
82+
}
83+
7484
/**
7585
* Describes configuration object of the inner print function
7686
* @internal

src/utils.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,25 @@ describe('utils', () => {
236236
)
237237
})
238238
})
239+
240+
describe('getRenderFunctionProps', () => {
241+
const printProps: _PrintConfig<string> = {
242+
value: 'Test Value',
243+
componentName: 'SomeComponentName',
244+
printer: console,
245+
logLevel: 'warn',
246+
}
247+
248+
it('returns print props', () => {
249+
expect(utils.getRenderFunctionProps(printProps, false)).toHaveProperty(
250+
'logLevel',
251+
)
252+
})
253+
254+
it('returns print props', () => {
255+
expect(utils.getRenderFunctionProps(printProps, true)).not.toHaveProperty(
256+
'logLevel',
257+
)
258+
})
259+
})
239260
})

src/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
_PrintConfig,
55
ComponentLifecycleLabels,
66
_SupportedConsole,
7+
RenderProps,
78
} from './types'
89
import {
910
CURRENT_VALUE_LABEL,
@@ -81,6 +82,28 @@ export function getComponentName(): string {
8182
}
8283
}
8384

85+
export function getRenderFunctionProps<T>(
86+
props: _PrintConfig<T>,
87+
isRender?: boolean,
88+
): _PrintConfig<T> | RenderProps<T> {
89+
if (isRender) {
90+
const renderProps: RenderProps<T> = {
91+
value: props.value,
92+
prevValue: props.prevValue,
93+
type: props.type,
94+
componentName: props.componentName,
95+
flags: {
96+
isGrouped: props.flags?.isGrouped,
97+
isCollapsed: props.flags?.isCollapsed,
98+
},
99+
}
100+
101+
return renderProps
102+
}
103+
104+
return props
105+
}
106+
84107
export function getPrinter(
85108
printer: Printer | Console,
86109
method: keyof _SupportedConsole,

0 commit comments

Comments
 (0)