1
1
import { Clerk } from './clerk' ;
2
- import type { AuthConfig , DisplayConfig } from './resources/internal' ;
2
+ import type { DevBrowser } from './devBrowser' ;
3
+ import type { DisplayConfig } from './resources/internal' ;
3
4
import { Client , Environment } from './resources/internal' ;
4
5
5
6
const mockClientFetch = jest . fn ( ) ;
6
7
const mockEnvironmentFetch = jest . fn ( ) ;
7
- const mockUsesUrlBasedSessionSync = jest . fn ( ) ;
8
8
9
9
jest . mock ( './resources/Client' ) ;
10
10
jest . mock ( './resources/Environment' ) ;
11
11
12
12
// Because Jest, don't ask me why...
13
13
jest . mock ( './devBrowser' , ( ) => ( {
14
- createDevBrowser : ( ) => ( {
14
+ createDevBrowser : ( ) : DevBrowser => ( {
15
15
clear : jest . fn ( ) ,
16
16
setup : jest . fn ( ) ,
17
17
getDevBrowserJWT : jest . fn ( ( ) => 'deadbeef' ) ,
@@ -57,7 +57,9 @@ const developmentPublishableKey = 'pk_test_Y2xlcmsuYWJjZWYuMTIzNDUuZGV2LmxjbGNsZ
57
57
const productionPublishableKey = 'pk_live_Y2xlcmsuYWJjZWYuMTIzNDUucHJvZC5sY2xjbGVyay5jb20k' ;
58
58
59
59
describe ( 'Clerk singleton - Redirects' , ( ) => {
60
- let mockNavigate = jest . fn ( ) ;
60
+ const mockNavigate = jest . fn ( ( to : string ) => Promise . resolve ( to ) ) ;
61
+ const mockedLoadOptions = { routerPush : mockNavigate , routerReplace : mockNavigate } ;
62
+
61
63
let mockWindowLocation ;
62
64
let mockHref : jest . Mock ;
63
65
@@ -90,10 +92,10 @@ describe('Clerk singleton - Redirects', () => {
90
92
activeSessions : [ ] ,
91
93
} ) ,
92
94
) ;
93
-
94
- mockNavigate = jest . fn ( ( to : string ) => Promise . resolve ( to ) ) ;
95
95
} ) ;
96
96
97
+ afterEach ( ( ) => mockNavigate . mockReset ( ) ) ;
98
+
97
99
describe ( '.redirectTo(SignUp|SignIn|UserProfile|AfterSignIn|AfterSignUp|CreateOrganization|OrganizationProfile)' , ( ) => {
98
100
let clerkForProductionInstance : Clerk ;
99
101
let clerkForDevelopmentInstance : Clerk ;
@@ -102,25 +104,18 @@ describe('Clerk singleton - Redirects', () => {
102
104
beforeEach ( async ( ) => {
103
105
mockEnvironmentFetch . mockReturnValue (
104
106
Promise . resolve ( {
105
- authConfig : { urlBasedSessionSyncing : true } as AuthConfig ,
106
107
userSettings : mockUserSettings ,
107
108
displayConfig : mockDisplayConfigWithSameOrigin ,
108
109
isProduction : ( ) => false ,
109
110
isDevelopmentOrStaging : ( ) => true ,
110
111
} ) ,
111
112
) ;
112
113
113
- mockUsesUrlBasedSessionSync . mockReturnValue ( true ) ;
114
-
115
114
clerkForProductionInstance = new Clerk ( productionPublishableKey ) ;
116
- await clerkForProductionInstance . load ( {
117
- routerPush : mockNavigate ,
118
- } ) ;
119
-
120
115
clerkForDevelopmentInstance = new Clerk ( developmentPublishableKey ) ;
121
- await clerkForDevelopmentInstance . load ( {
122
- routerPush : mockNavigate ,
123
- } ) ;
116
+
117
+ await clerkForProductionInstance . load ( mockedLoadOptions ) ;
118
+ await clerkForDevelopmentInstance . load ( mockedLoadOptions ) ;
124
119
} ) ;
125
120
126
121
afterEach ( ( ) => {
@@ -129,57 +124,57 @@ describe('Clerk singleton - Redirects', () => {
129
124
130
125
it ( 'redirects to signInUrl' , async ( ) => {
131
126
await clerkForProductionInstance . redirectToSignIn ( { redirectUrl : 'https://www.example.com/' } ) ;
132
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/sign-in#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ) ;
133
-
134
127
await clerkForDevelopmentInstance . redirectToSignIn ( { redirectUrl : 'https://www.example.com/' } ) ;
128
+
129
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/sign-in#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ) ;
135
130
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/sign-in#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ) ;
136
131
} ) ;
137
132
138
133
it ( 'redirects to signUpUrl' , async ( ) => {
139
134
await clerkForProductionInstance . redirectToSignUp ( { redirectUrl : 'https://www.example.com/' } ) ;
140
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/sign-up#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ) ;
141
-
142
135
await clerkForDevelopmentInstance . redirectToSignUp ( { redirectUrl : 'https://www.example.com/' } ) ;
136
+
137
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/sign-up#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ) ;
143
138
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/sign-up#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ) ;
144
139
} ) ;
145
140
146
141
it ( 'redirects to userProfileUrl' , async ( ) => {
147
142
await clerkForProductionInstance . redirectToUserProfile ( ) ;
148
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/user-profile' ) ;
149
-
150
143
await clerkForDevelopmentInstance . redirectToUserProfile ( ) ;
144
+
145
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/user-profile' ) ;
151
146
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/user-profile' ) ;
152
147
} ) ;
153
148
154
149
it ( 'redirects to afterSignUp' , async ( ) => {
155
150
await clerkForProductionInstance . redirectToAfterSignUp ( ) ;
156
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/' ) ;
157
-
158
151
await clerkForDevelopmentInstance . redirectToAfterSignUp ( ) ;
152
+
153
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/' ) ;
159
154
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/' ) ;
160
155
} ) ;
161
156
162
157
it ( 'redirects to afterSignIn' , async ( ) => {
163
158
await clerkForProductionInstance . redirectToAfterSignIn ( ) ;
164
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/' ) ;
165
-
166
159
await clerkForDevelopmentInstance . redirectToAfterSignIn ( ) ;
160
+
161
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/' ) ;
167
162
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/' ) ;
168
163
} ) ;
169
164
170
165
it ( 'redirects to create organization' , async ( ) => {
171
166
await clerkForProductionInstance . redirectToCreateOrganization ( ) ;
172
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/create-organization' ) ;
173
-
174
167
await clerkForDevelopmentInstance . redirectToCreateOrganization ( ) ;
168
+
169
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/create-organization' ) ;
175
170
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/create-organization' ) ;
176
171
} ) ;
177
172
178
173
it ( 'redirects to organization profile' , async ( ) => {
179
174
await clerkForProductionInstance . redirectToOrganizationProfile ( ) ;
180
- expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/organization-profile' ) ;
181
-
182
175
await clerkForDevelopmentInstance . redirectToOrganizationProfile ( ) ;
176
+
177
+ expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 1 , '/organization-profile' ) ;
183
178
expect ( mockNavigate ) . toHaveBeenNthCalledWith ( 2 , '/organization-profile' ) ;
184
179
} ) ;
185
180
} ) ;
@@ -188,87 +183,71 @@ describe('Clerk singleton - Redirects', () => {
188
183
beforeEach ( async ( ) => {
189
184
mockEnvironmentFetch . mockReturnValue (
190
185
Promise . resolve ( {
191
- authConfig : { urlBasedSessionSyncing : true } as AuthConfig ,
192
186
userSettings : mockUserSettings ,
193
187
displayConfig : mockDisplayConfigWithDifferentOrigin ,
194
188
isProduction : ( ) => false ,
195
189
isDevelopmentOrStaging : ( ) => true ,
196
190
} ) ,
197
191
) ;
198
192
199
- mockUsesUrlBasedSessionSync . mockReturnValue ( true ) ;
200
-
201
193
clerkForProductionInstance = new Clerk ( productionPublishableKey ) ;
202
- await clerkForProductionInstance . load ( {
203
- routerPush : mockNavigate ,
204
- } ) ;
205
-
206
194
clerkForDevelopmentInstance = new Clerk ( developmentPublishableKey ) ;
207
- await clerkForDevelopmentInstance . load ( {
208
- routerPush : mockNavigate ,
209
- } ) ;
195
+
196
+ await clerkForProductionInstance . load ( mockedLoadOptions ) ;
197
+ await clerkForDevelopmentInstance . load ( mockedLoadOptions ) ;
210
198
} ) ;
211
199
212
200
afterEach ( ( ) => {
213
201
mockEnvironmentFetch . mockRestore ( ) ;
214
202
} ) ;
215
203
204
+ const host = 'http://another-test.host' ;
205
+
216
206
it ( 'redirects to signInUrl' , async ( ) => {
217
207
await clerkForProductionInstance . redirectToSignIn ( { redirectUrl : 'https://www.example.com/' } ) ;
218
- expect ( mockHref ) . toHaveBeenNthCalledWith (
219
- 1 ,
220
- 'http://another-test.host/sign-in#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ,
221
- ) ;
222
-
223
208
await clerkForDevelopmentInstance . redirectToSignIn ( { redirectUrl : 'https://www.example.com/' } ) ;
209
+
210
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , `${ host } /sign-in#/?redirect_url=https%3A%2F%2Fwww.example.com%2F` ) ;
211
+
224
212
expect ( mockHref ) . toHaveBeenNthCalledWith (
225
213
2 ,
226
- 'http://another-test. host/sign-in#/?redirect_url=https%3A%2F%2Fwww.example.com%2F__clerk_db_jwt[deadbeef]' ,
214
+ ` ${ host } /sign-in?__clerk_db_jwt=deadbeef #/?redirect_url=https%3A%2F%2Fwww.example.com%2F` ,
227
215
) ;
228
216
} ) ;
229
217
230
218
it ( 'redirects to signUpUrl' , async ( ) => {
231
219
await clerkForProductionInstance . redirectToSignUp ( { redirectUrl : 'https://www.example.com/' } ) ;
232
- expect ( mockHref ) . toHaveBeenNthCalledWith (
233
- 1 ,
234
- 'http://another-test.host/sign-up#/?redirect_url=https%3A%2F%2Fwww.example.com%2F' ,
235
- ) ;
236
-
237
220
await clerkForDevelopmentInstance . redirectToSignUp ( { redirectUrl : 'https://www.example.com/' } ) ;
221
+
222
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , `${ host } /sign-up#/?redirect_url=https%3A%2F%2Fwww.example.com%2F` ) ;
238
223
expect ( mockHref ) . toHaveBeenNthCalledWith (
239
224
2 ,
240
- 'http://another-test. host/sign-up#/?redirect_url=https%3A%2F%2Fwww.example.com%2F__clerk_db_jwt[deadbeef]' ,
225
+ ` ${ host } /sign-up?__clerk_db_jwt=deadbeef #/?redirect_url=https%3A%2F%2Fwww.example.com%2F` ,
241
226
) ;
242
227
} ) ;
243
228
244
229
it ( 'redirects to userProfileUrl' , async ( ) => {
245
230
await clerkForProductionInstance . redirectToUserProfile ( ) ;
246
- expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , 'http://another-test.host/user-profile' ) ;
247
-
248
231
await clerkForDevelopmentInstance . redirectToUserProfile ( ) ;
249
- expect ( mockHref ) . toHaveBeenNthCalledWith ( 2 , 'http://another-test.host/user-profile#__clerk_db_jwt[deadbeef]' ) ;
232
+
233
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , `${ host } /user-profile` ) ;
234
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 2 , `${ host } /user-profile?__clerk_db_jwt=deadbeef` ) ;
250
235
} ) ;
251
236
252
237
it ( 'redirects to create organization' , async ( ) => {
253
238
await clerkForProductionInstance . redirectToCreateOrganization ( ) ;
254
- expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , 'http://another-test.host/create-organization' ) ;
255
-
256
239
await clerkForDevelopmentInstance . redirectToCreateOrganization ( ) ;
257
- expect ( mockHref ) . toHaveBeenNthCalledWith (
258
- 2 ,
259
- 'http://another-test.host/create-organization#__clerk_db_jwt[deadbeef]' ,
260
- ) ;
240
+
241
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , `${ host } /create-organization` ) ;
242
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 2 , `${ host } /create-organization?__clerk_db_jwt=deadbeef` ) ;
261
243
} ) ;
262
244
263
245
it ( 'redirects to organization profile' , async ( ) => {
264
246
await clerkForProductionInstance . redirectToOrganizationProfile ( ) ;
265
- expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , 'http://another-test.host/organization-profile' ) ;
266
-
267
247
await clerkForDevelopmentInstance . redirectToOrganizationProfile ( ) ;
268
- expect ( mockHref ) . toHaveBeenNthCalledWith (
269
- 2 ,
270
- 'http://another-test.host/organization-profile#__clerk_db_jwt[deadbeef]' ,
271
- ) ;
248
+
249
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , `${ host } /organization-profile` ) ;
250
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 2 , `${ host } /organization-profile?__clerk_db_jwt=deadbeef` ) ;
272
251
} ) ;
273
252
} ) ;
274
253
} ) ;
@@ -278,10 +257,8 @@ describe('Clerk singleton - Redirects', () => {
278
257
let clerkForDevelopmentInstance : Clerk ;
279
258
280
259
beforeEach ( async ( ) => {
281
- mockUsesUrlBasedSessionSync . mockReturnValue ( true ) ;
282
260
mockEnvironmentFetch . mockReturnValue (
283
261
Promise . resolve ( {
284
- authConfig : { urlBasedSessionSyncing : true } as AuthConfig ,
285
262
userSettings : mockUserSettings ,
286
263
displayConfig : mockDisplayConfigWithDifferentOrigin ,
287
264
isProduction : ( ) => false ,
@@ -290,22 +267,20 @@ describe('Clerk singleton - Redirects', () => {
290
267
) ;
291
268
292
269
clerkForProductionInstance = new Clerk ( productionPublishableKey ) ;
293
- await clerkForProductionInstance . load ( {
294
- routerPush : mockNavigate ,
295
- } ) ;
296
-
297
270
clerkForDevelopmentInstance = new Clerk ( developmentPublishableKey ) ;
298
- await clerkForDevelopmentInstance . load ( {
299
- routerPush : mockNavigate ,
300
- } ) ;
271
+
272
+ await clerkForProductionInstance . load ( mockedLoadOptions ) ;
273
+ await clerkForDevelopmentInstance . load ( mockedLoadOptions ) ;
301
274
} ) ;
302
275
276
+ const host = 'https://app.example.com' ;
277
+
303
278
it ( 'redirects to the provided url with __clerk_db_jwt in the url' , async ( ) => {
304
- await clerkForProductionInstance . redirectWithAuth ( 'https://app.example.com' ) ;
305
- expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , 'https://app.example.com/' ) ;
279
+ await clerkForProductionInstance . redirectWithAuth ( host ) ;
280
+ await clerkForDevelopmentInstance . redirectWithAuth ( host ) ;
306
281
307
- await clerkForDevelopmentInstance . redirectWithAuth ( 'https://app.example.com' ) ;
308
- expect ( mockHref ) . toHaveBeenNthCalledWith ( 2 , 'https://app.example.com/# __clerk_db_jwt[ deadbeef]' ) ;
282
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 1 , ` ${ host } /` ) ;
283
+ expect ( mockHref ) . toHaveBeenNthCalledWith ( 2 , ` ${ host } /? __clerk_db_jwt= deadbeef` ) ;
309
284
} ) ;
310
285
} ) ;
311
286
} ) ;
0 commit comments