@@ -4,6 +4,7 @@ import { useEffect, useState } from 'react'
44
55describe ( 'useLog' , ( ) => {
66 const consoleLog = jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
7+ const consoleGroup = jest . spyOn ( console , 'group' ) . mockImplementation ( ( ) => { } )
78
89 beforeEach ( ( ) => {
910 jest . useFakeTimers ( )
@@ -23,9 +24,16 @@ describe('useLog', () => {
2324 expect ( result . current . log ) . toBeTruthy ( )
2425
2526 renderHook ( ( ) => result . current . log ( 'Test' ) )
26- expect ( consoleLog ) . toBeCalledWith ( 'On mount: Test' )
27- expect ( consoleLog ) . toBeCalledWith ( 'On change: Test' )
28- expect ( consoleLog ) . toBeCalledTimes ( 2 )
27+ expect ( consoleLog ) . toBeCalledWith ( ' On mount: Test' )
28+ expect ( consoleLog ) . toBeCalledWith (
29+ 'Previous value: %cTest' ,
30+ 'color: SlateGray; font-weight: thin;' ,
31+ )
32+ expect ( consoleLog ) . toBeCalledWith (
33+ ' Current value: %cTest' ,
34+ 'color: green; font-weight: bold;' ,
35+ )
36+ expect ( consoleLog ) . toBeCalledTimes ( 3 )
2937 } )
3038
3139 it ( 'renders hook with changes' , async ( ) => {
@@ -48,33 +56,111 @@ describe('useLog', () => {
4856 } , [ ] )
4957 } )
5058
51- // set initial values
52- expect ( consoleLog ) . toBeCalledWith ( 'On mount: null' )
53- expect ( consoleLog ) . toBeCalledWith ( 'On change: null' )
54-
55- expect ( consoleLog ) . toBeCalledWith ( 'On change: onMount' )
56-
57- expect ( consoleLog ) . toBeCalledTimes ( 3 )
59+ /*
60+ * Set Initial Values
61+ */
62+ expect ( consoleGroup ) . toBeCalledWith (
63+ `Mount in %c<TestComponent /> %c@ ${ new Date ( ) . toLocaleTimeString ( ) } ` ,
64+ 'color: DodgerBlue' ,
65+ 'color: SlateGray; font-weight: thin;' ,
66+ )
67+ expect ( consoleLog ) . toBeCalledWith ( ' On mount: null' )
68+
69+ expect ( consoleGroup ) . toBeCalledWith (
70+ `Change in %c<TestComponent /> %c@ ${ new Date ( ) . toLocaleTimeString ( ) } ` ,
71+ 'color: DodgerBlue' ,
72+ 'color: SlateGray; font-weight: thin;' ,
73+ )
74+ expect ( consoleLog ) . toBeCalledWith (
75+ 'Previous value: %cnull' ,
76+ 'color: SlateGray; font-weight: thin;' ,
77+ )
78+ expect ( consoleLog ) . toBeCalledWith (
79+ ' Current value: %cnull' ,
80+ 'color: green; font-weight: bold;' ,
81+ )
82+ expect ( consoleGroup ) . toBeCalledWith (
83+ `Change in %c<TestComponent /> %c@ ${ new Date ( ) . toLocaleTimeString ( ) } ` ,
84+ 'color: DodgerBlue' ,
85+ 'color: SlateGray; font-weight: thin;' ,
86+ )
87+ expect ( consoleLog ) . toBeCalledWith (
88+ 'Previous value: %cnull' ,
89+ 'color: SlateGray; font-weight: thin;' ,
90+ )
91+ expect ( consoleLog ) . toBeCalledWith (
92+ ' Current value: %cnull' ,
93+ 'color: green; font-weight: bold;' ,
94+ )
95+ expect ( consoleLog ) . toBeCalledTimes ( 5 )
96+ expect ( consoleGroup ) . toBeCalledTimes ( 3 )
5897
98+ /*
99+ * Check first change
100+ */
59101 await act ( ( ) => {
60102 jest . advanceTimersByTime ( 1000 )
61103 logRerender ( )
62104 } )
63- expect ( consoleLog ) . toBeCalledWith ( 'On change: onChange 1s' )
64- expect ( consoleLog ) . toBeCalledTimes ( 4 )
105+ expect ( consoleGroup ) . toBeCalledWith (
106+ `Change in %c<TestComponent /> %c@ ${ new Date ( ) . toLocaleTimeString ( ) } ` ,
107+ 'color: DodgerBlue' ,
108+ 'color: SlateGray; font-weight: thin;' ,
109+ )
110+ expect ( consoleLog ) . toBeCalledWith (
111+ 'Previous value: %cnull' ,
112+ 'color: SlateGray; font-weight: thin;' ,
113+ )
114+ expect ( consoleLog ) . toBeCalledWith (
115+ ' Current value: %conChange 1s' ,
116+ 'color: green; font-weight: bold;' ,
117+ )
118+ expect ( consoleLog ) . toBeCalledTimes ( 7 )
119+ expect ( consoleGroup ) . toBeCalledTimes ( 4 )
65120
121+ /*
122+ * Check second change
123+ */
66124 await act ( ( ) => {
67125 jest . advanceTimersByTime ( 1000 )
68126 logRerender ( )
69127 } )
70- expect ( consoleLog ) . toBeCalledWith ( 'On change: onChange 2s' )
71- expect ( consoleLog ) . toBeCalledTimes ( 5 )
72-
128+ expect ( consoleGroup ) . toBeCalledWith (
129+ `Change in %c<TestComponent /> %c@ ${ new Date ( ) . toLocaleTimeString ( ) } ` ,
130+ 'color: DodgerBlue' ,
131+ 'color: SlateGray; font-weight: thin;' ,
132+ )
133+ expect ( consoleLog ) . toBeCalledWith (
134+ 'Previous value: %conChange 1s' ,
135+ 'color: SlateGray; font-weight: thin;' ,
136+ )
137+ expect ( consoleLog ) . toBeCalledWith (
138+ ' Current value: %conChange 2s' ,
139+ 'color: green; font-weight: bold;' ,
140+ )
141+ expect ( consoleLog ) . toBeCalledTimes ( 9 )
142+ expect ( consoleGroup ) . toBeCalledTimes ( 5 )
143+
144+ /*
145+ * Check unmount change
146+ */
73147 await act ( ( ) => {
74148 logUnmount ( )
75149 } )
76- expect ( consoleLog ) . toBeCalledWith ( 'On unmount: null' )
77- expect ( consoleLog ) . toBeCalledWith ( 'Before unmount: onChange 2s' )
78- expect ( consoleLog ) . toBeCalledTimes ( 7 )
150+ expect ( consoleGroup ) . toBeCalledWith (
151+ `Unmount in %c<TestComponent /> %c@ ${ new Date ( ) . toLocaleTimeString ( ) } ` ,
152+ 'color: DodgerBlue' ,
153+ 'color: SlateGray; font-weight: thin;' ,
154+ )
155+ expect ( consoleLog ) . toBeCalledWith (
156+ 'Previous value: %conChange 2s' ,
157+ 'color: SlateGray; font-weight: thin;' ,
158+ )
159+ expect ( consoleLog ) . toBeCalledWith (
160+ ' Current value: %cnull' ,
161+ 'color: green; font-weight: bold;' ,
162+ )
163+ expect ( consoleLog ) . toBeCalledTimes ( 11 )
164+ expect ( consoleGroup ) . toBeCalledTimes ( 6 )
79165 } )
80166} )
0 commit comments