@@ -19,9 +19,14 @@ jest.mock('next/router', () => {
19
19
} ;
20
20
} ) ;
21
21
22
- type Table < I = string , O = string > = Array < { in : I ; out : O } > ;
23
-
24
22
describe ( 'client' , ( ) => {
23
+ beforeEach ( ( ) => {
24
+ readyCalled = false ;
25
+ if ( Router . router ) {
26
+ Router . router . changeState ( 'pushState' , '/[user]/posts/[id]' , '/abhi/posts/123' , { } ) ;
27
+ }
28
+ } ) ;
29
+
25
30
describe ( 'nextRouterInstrumentation' , ( ) => {
26
31
it ( 'waits for Router.ready()' , ( ) => {
27
32
const mockStartTransaction = jest . fn ( ) ;
@@ -49,54 +54,83 @@ describe('client', () => {
49
54
expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 0 ) ;
50
55
} ) ;
51
56
52
- it ( 'creates navigation transactions' , ( ) => {
53
- const mockStartTransaction = jest . fn ( ) ;
54
- nextRouterInstrumentation ( mockStartTransaction , false ) ;
55
- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 0 ) ;
56
-
57
- const table : Table < Array < string | unknown > , Record < string , unknown > > = [
58
- {
59
- in : [ 'pushState' , '/posts/[id]' , '/posts/32' , { } ] ,
60
- out : {
57
+ describe ( 'navigation transactions' , ( ) => {
58
+ // [name, in, out]
59
+ const table : Array < [ string , [ string , string , string , Record < string , unknown > ] , Record < string , unknown > ] > = [
60
+ [
61
+ 'creates parameterized transaction' ,
62
+ [ 'pushState' , '/posts/[id]' , '/posts/32' , { } ] ,
63
+ {
61
64
name : '/posts/[id]' ,
62
65
op : 'navigation' ,
63
66
tags : {
64
- from : '/posts/[id]' ,
67
+ from : '/[user]/ posts/[id]' ,
65
68
method : 'pushState' ,
66
69
'routing.instrumentation' : 'next-router' ,
67
70
} ,
68
71
} ,
69
- } ,
70
- {
71
- in : [ 'replaceState' , '/posts/[id]?name=cat' , '/posts/32?name=cat' , { } ] ,
72
- out : {
72
+ ] ,
73
+ [
74
+ 'strips query parameters' ,
75
+ [ 'replaceState' , '/posts/[id]?name=cat' , '/posts/32?name=cat' , { } ] ,
76
+ {
73
77
name : '/posts/[id]' ,
74
78
op : 'navigation' ,
75
79
tags : {
76
- from : '/posts/[id]' ,
80
+ from : '/[user]/ posts/[id]' ,
77
81
method : 'replaceState' ,
78
82
'routing.instrumentation' : 'next-router' ,
79
83
} ,
80
84
} ,
81
- } ,
82
- {
83
- in : [ 'pushState' , '/about' , '/about' , { } ] ,
84
- out : {
85
+ ] ,
86
+ [
87
+ 'creates regular transactions' ,
88
+ [ 'pushState' , '/about' , '/about' , { } ] ,
89
+ {
85
90
name : '/about' ,
86
91
op : 'navigation' ,
87
92
tags : {
88
- from : '/about ' ,
93
+ from : '/[user]/posts/[id] ' ,
89
94
method : 'pushState' ,
90
95
'routing.instrumentation' : 'next-router' ,
91
96
} ,
92
97
} ,
93
- } ,
98
+ ] ,
94
99
] ;
95
100
96
- table . forEach ( test => {
97
- // @ts -ignore changeState can be called with array spread
98
- Router . router ?. changeState ( ...test . in ) ;
99
- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( test . out ) ;
101
+ it . each ( table ) ( '%s' , ( ...test ) => {
102
+ const mockStartTransaction = jest . fn ( ) ;
103
+ nextRouterInstrumentation ( mockStartTransaction , false ) ;
104
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 0 ) ;
105
+
106
+ // @ts -ignore we can spread into test
107
+ Router . router ?. changeState ( ...test [ 1 ] ) ;
108
+ expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( test [ 2 ] ) ;
109
+ } ) ;
110
+ } ) ;
111
+
112
+ it ( 'does not create navigation transaction with the same name' , ( ) => {
113
+ const mockStartTransaction = jest . fn ( ) ;
114
+ nextRouterInstrumentation ( mockStartTransaction , false ) ;
115
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 0 ) ;
116
+
117
+ Router . router ?. changeState ( 'pushState' , '/login' , '/login' , { } ) ;
118
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
119
+ expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
120
+ name : '/login' ,
121
+ op : 'navigation' ,
122
+ tags : { from : '/[user]/posts/[id]' , method : 'pushState' , 'routing.instrumentation' : 'next-router' } ,
123
+ } ) ;
124
+
125
+ Router . router ?. changeState ( 'pushState' , '/login' , '/login' , { } ) ;
126
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
127
+
128
+ Router . router ?. changeState ( 'pushState' , '/posts/[id]' , '/posts/123' , { } ) ;
129
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
130
+ expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
131
+ name : '/posts/[id]' ,
132
+ op : 'navigation' ,
133
+ tags : { from : '/login' , method : 'pushState' , 'routing.instrumentation' : 'next-router' } ,
100
134
} ) ;
101
135
} ) ;
102
136
} ) ;
0 commit comments