@@ -4,6 +4,7 @@ import { useEffect, useState } from 'react'
4
4
5
5
describe ( 'useLog' , ( ) => {
6
6
const consoleLog = jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
7
+ const consoleGroup = jest . spyOn ( console , 'group' ) . mockImplementation ( ( ) => { } )
7
8
8
9
beforeEach ( ( ) => {
9
10
jest . useFakeTimers ( )
@@ -23,9 +24,16 @@ describe('useLog', () => {
23
24
expect ( result . current . log ) . toBeTruthy ( )
24
25
25
26
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 )
29
37
} )
30
38
31
39
it ( 'renders hook with changes' , async ( ) => {
@@ -48,33 +56,111 @@ describe('useLog', () => {
48
56
} , [ ] )
49
57
} )
50
58
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 )
58
97
98
+ /*
99
+ * Check first change
100
+ */
59
101
await act ( ( ) => {
60
102
jest . advanceTimersByTime ( 1000 )
61
103
logRerender ( )
62
104
} )
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 )
65
120
121
+ /*
122
+ * Check second change
123
+ */
66
124
await act ( ( ) => {
67
125
jest . advanceTimersByTime ( 1000 )
68
126
logRerender ( )
69
127
} )
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
+ */
73
147
await act ( ( ) => {
74
148
logUnmount ( )
75
149
} )
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 )
79
165
} )
80
166
} )
0 commit comments