-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into claims_bugfix
- Loading branch information
Showing
8 changed files
with
138 additions
and
23 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@azure-msal-browser-7b8679dc-1a5c-44b1-8f0a-bb52821145a9.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Add getAccount API to IPublicClientApplication (#7019)", | ||
"packageName": "@azure/msal-browser", | ||
"email": "dasau@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@azure-msal-react-5355b5e3-0c1c-4e23-ae9d-a6d96ef4285d.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Fix useIsAuthenticated returning incorrect value during useEffect update #7057", | ||
"packageName": "@azure/msal-react", | ||
"email": "kade@hatchedlabs.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from "react"; | ||
import { render, screen, waitFor, act } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import { Configuration, PublicClientApplication } from "@azure/msal-browser"; | ||
import { TEST_CONFIG, testAccount } from "../TestConstants"; | ||
import { MsalProvider, useIsAuthenticated, withMsal } from "../../src/index"; | ||
|
||
describe("useIsAuthenticated tests", () => { | ||
let pca: PublicClientApplication; | ||
const msalConfig: Configuration = { | ||
auth: { | ||
clientId: TEST_CONFIG.MSAL_CLIENT_ID, | ||
}, | ||
system: { | ||
allowNativeBroker: false, | ||
}, | ||
}; | ||
|
||
let handleRedirectSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
pca = new PublicClientApplication(msalConfig); | ||
handleRedirectSpy = jest.spyOn(pca, "handleRedirectPromise"); | ||
jest.spyOn(pca, "getAllAccounts").mockImplementation(() => []); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("useAuthenticated always returns true if user has an account", async () => { | ||
const invalidAuthStateCallback = jest.fn(); | ||
|
||
const testComponent = ({ ...props }) => { | ||
const isAuth = useIsAuthenticated(); | ||
const accounts = props.msalContext.accounts; | ||
|
||
if (accounts.length > 0 && !isAuth) { | ||
invalidAuthStateCallback(); | ||
} | ||
|
||
return ( | ||
<> | ||
<p>This component has been wrapped by msal</p> | ||
{accounts.length === 0 && <p>No accounts</p>} | ||
{accounts.length > 0 && <p>Has accounts</p>} | ||
{isAuth && <p>Is authed</p>} | ||
{!isAuth && <p>Not authed</p>} | ||
</> | ||
); | ||
}; | ||
|
||
const WrappedComponent = withMsal(testComponent); | ||
const { rerender } = render( | ||
<MsalProvider instance={pca}> | ||
<WrappedComponent /> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
|
||
expect(await screen.findByText("No accounts")).toBeInTheDocument(); | ||
expect(await screen.findByText("Not authed")).toBeInTheDocument(); | ||
|
||
const pcaWithAccounts = new PublicClientApplication(msalConfig); | ||
jest.spyOn(pcaWithAccounts, "getAllAccounts").mockImplementation(() => [ | ||
testAccount, | ||
]); | ||
|
||
await act(async () => | ||
rerender( | ||
<MsalProvider instance={pcaWithAccounts}> | ||
<WrappedComponent /> | ||
</MsalProvider> | ||
) | ||
); | ||
|
||
expect(await screen.findByText("Has accounts")).toBeInTheDocument(); | ||
expect(await screen.findByText("Is authed")).toBeInTheDocument(); | ||
expect(invalidAuthStateCallback).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |