Skip to content

Commit af3c86d

Browse files
Allow overriding of getAccessTokenForOrg in test provider (#70)
1 parent dfeab0d commit af3c86d

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "git",
66
"url": "https://github.com/PropelAuth/react"
77
},
8-
"version": "2.0.24",
8+
"version": "2.0.25",
99
"license": "MIT",
1010
"keywords": [
1111
"auth",

src/AuthContextForTesting.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
AccessHelper,
33
AccessHelperWithOrg,
4+
AccessTokenForActiveOrg,
45
AuthenticationInfo,
56
OrgHelper,
67
OrgIdToOrgMemberInfo,
@@ -10,14 +11,15 @@ import {
1011
User,
1112
UserClass,
1213
} from "@propelauth/javascript"
13-
import React from "react"
14+
import React, { useCallback } from "react"
1415
import { AuthContext, InternalAuthState } from "./AuthContext"
1516

1617
// User information that we will hard code within the AuthProvider
1718
export type UserInformationForTesting = {
1819
user: User
1920
orgMemberInfos: OrgMemberInfo[]
2021
accessToken?: string
22+
getAccessTokenForOrg?: (orgId: string) => Promise<AccessTokenForActiveOrg>
2123
}
2224

2325
export type AuthProviderForTestingProps = {
@@ -39,6 +41,19 @@ export const AuthProviderForTesting = ({
3941
}: AuthProviderForTestingProps) => {
4042
const authInfo = getAuthInfoForTesting(userInformation)
4143
const activeOrgFnWithDefault = activeOrgFn ? activeOrgFn : () => null
44+
const getAccessTokenForOrg = useCallback(
45+
(orgId: string) => {
46+
if (userInformation?.getAccessTokenForOrg) {
47+
return userInformation.getAccessTokenForOrg(orgId)
48+
}
49+
return Promise.resolve({
50+
error: undefined,
51+
accessToken: "ACCESS_TOKEN",
52+
})
53+
},
54+
[userInformation?.getAccessTokenForOrg]
55+
)
56+
4257
const contextValue: InternalAuthState = {
4358
loading: !!loading,
4459
authInfo,
@@ -58,12 +73,8 @@ export const AuthProviderForTesting = ({
5873
activeOrgFn: activeOrgFnWithDefault,
5974
refreshAuthInfo: () => Promise.resolve(),
6075
tokens: {
61-
getAccessTokenForOrg: () =>
62-
Promise.resolve({
63-
error: undefined,
64-
accessToken: "ACCESS_TOKEN",
65-
}),
66-
getAccessToken: () => Promise.resolve("ACCESS_TOKEN"),
76+
getAccessTokenForOrg: getAccessTokenForOrg,
77+
getAccessToken: () => Promise.resolve(userInformation?.accessToken ?? "ACCESS_TOKEN"),
6778
},
6879
}
6980

0 commit comments

Comments
 (0)