1- import { getGroupLabel , getComponentName , print } from './utils'
2- import { _PrintConfig , _PrintTypes } from './types'
1+ import { getGroupLabel , getComponentName , print , getPrinter } from './utils'
2+ import { _PrintConfig , _PrintTypes , Printer } from './types'
33
44describe ( 'utils' , ( ) => {
55 describe ( 'getGroupLabel' , ( ) => {
@@ -22,6 +22,28 @@ describe('utils', () => {
2222 } )
2323 } )
2424
25+ describe ( 'getPrinter' , ( ) => {
26+ it ( 'returns printer for existing printer method' , ( ) => {
27+ const printer : Printer = { log : jest . fn ( ) }
28+ expect ( getPrinter ( printer , 'log' ) ) . toBe ( printer . log )
29+ } )
30+
31+ it ( 'returns console for non-existing printer method' , ( ) => {
32+ const printer : Printer = { log : jest . fn ( ) }
33+ expect ( getPrinter ( printer , 'warn' ) ) . toBe ( console . warn )
34+ } )
35+
36+ it ( 'returns console for empty printer' , ( ) => {
37+ const printer : Printer = { }
38+ expect ( getPrinter ( printer , 'log' ) ) . toBe ( console . log )
39+ } )
40+
41+ it ( 'returns console for empty printer method' , ( ) => {
42+ const printer : Printer = { log : undefined }
43+ expect ( getPrinter ( printer , 'log' ) ) . toBe ( console . log )
44+ } )
45+ } )
46+
2547 describe ( 'print' , ( ) => {
2648 const consoleLog = jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => null )
2749 const consoleGroup = jest
@@ -104,5 +126,38 @@ describe('utils', () => {
104126 expect ( consoleLog ) . toHaveBeenCalledWith ( ' A Label: Test Value' )
105127 expect ( consoleGroupEnd ) . toHaveBeenCalled ( )
106128 } )
129+
130+ it ( 'prints with custom printer' , ( ) => {
131+ const printer : Printer = { log : jest . fn ( ) }
132+ const printPropsWithPrinter : _PrintConfig < string > = {
133+ ...printProps ,
134+ printer,
135+ }
136+ const printerLog = jest
137+ . spyOn ( printer , 'log' )
138+ . mockImplementation ( ( ) => 'Some logs' )
139+
140+ print ( printPropsWithPrinter )
141+
142+ expect ( consoleLog ) . not . toHaveBeenCalled ( )
143+
144+ expect ( consoleGroup ) . toHaveBeenCalled ( )
145+ expect ( printerLog ) . toHaveBeenCalled ( )
146+ expect ( consoleGroupEnd ) . toHaveBeenCalled ( )
147+ } )
148+
149+ it ( 'prints with custom empty printer' , ( ) => {
150+ const printer : Printer = { }
151+ const printPropsWithPrinter : _PrintConfig < string > = {
152+ ...printProps ,
153+ printer,
154+ }
155+
156+ print ( printPropsWithPrinter )
157+
158+ expect ( consoleGroup ) . toHaveBeenCalled ( )
159+ expect ( consoleLog ) . toHaveBeenCalled ( )
160+ expect ( consoleGroupEnd ) . toHaveBeenCalled ( )
161+ } )
107162 } )
108163} )
0 commit comments