Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit dca4f38

Browse files
committed
add render app to tests
1 parent 675f793 commit dca4f38

File tree

33 files changed

+55325
-674
lines changed

33 files changed

+55325
-674
lines changed

2-Authorization-I/1-call-graph/SPA/package-lock.json

+1,441-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
{
2-
"name": "ms-identity-react-c2s1",
3-
"version": "0.1.0",
4-
"description": "A React single-page application using MSAL React to call the Microsoft Graph API",
5-
"dependencies": {
6-
"@azure/msal-browser": "^2.31.0",
7-
"@azure/msal-react": "^1.4.10",
8-
"@microsoft/microsoft-graph-client": "^3.0.2",
9-
"bootstrap": "^5.1.3",
10-
"jquery": "^3.5.1",
11-
"popper.js": "^1.16.1",
12-
"react": "^18.1.0",
13-
"react-bootstrap": "^2.4.0",
14-
"react-dom": "^18.1.0",
15-
"react-icons": "^4.4.0",
16-
"react-router-dom": "^6.3.0"
17-
},
18-
"devDependencies": {
19-
"react-scripts": "^5.0.1"
20-
},
21-
"overrides": {
22-
"autoprefixer": "10.4.5"
23-
},
24-
"scripts": {
25-
"start": "react-scripts start",
26-
"build": "react-scripts build",
27-
"test": "react-scripts test",
28-
"eject": "react-scripts eject"
29-
},
30-
"browserslist": {
31-
"production": [
32-
">0.2%",
33-
"not dead",
34-
"not op_mini all"
35-
],
36-
"development": [
37-
"last 1 chrome version",
38-
"last 1 firefox version",
39-
"last 1 safari version"
40-
]
41-
}
2+
"name": "ms-identity-react-c2s1",
3+
"version": "0.1.0",
4+
"description": "A React single-page application using MSAL React to call the Microsoft Graph API",
5+
"dependencies": {
6+
"@azure/msal-browser": "^2.31.0",
7+
"@azure/msal-react": "^1.4.10",
8+
"@microsoft/microsoft-graph-client": "^3.0.2",
9+
"bootstrap": "^5.1.3",
10+
"jquery": "^3.5.1",
11+
"popper.js": "^1.16.1",
12+
"react": "^18.1.0",
13+
"react-bootstrap": "^2.4.0",
14+
"react-dom": "^18.1.0",
15+
"react-icons": "^4.4.0",
16+
"react-router-dom": "^6.3.0"
17+
},
18+
"devDependencies": {
19+
"@testing-library/jest-dom": "^5.16.5",
20+
"@testing-library/react": "^13.4.0",
21+
"react-scripts": "^5.0.1"
22+
},
23+
"overrides": {
24+
"autoprefixer": "10.4.5"
25+
},
26+
"scripts": {
27+
"start": "react-scripts start",
28+
"build": "react-scripts build",
29+
"test": "react-scripts test",
30+
"eject": "react-scripts eject"
31+
},
32+
"browserslist": {
33+
"production": [
34+
">0.2%",
35+
"not dead",
36+
"not op_mini all"
37+
],
38+
"development": [
39+
"last 1 chrome version",
40+
"last 1 firefox version",
41+
"last 1 safari version"
42+
]
43+
}
4244
}

2-Authorization-I/1-call-graph/SPA/src/App.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Pages = () => {
2525
* PublicClientApplication instance via context as well as all hooks and components provided by msal-react. For more, visit:
2626
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-react/docs/getting-started.md
2727
*/
28-
export const App = ({ instance }) => {
28+
const App = ({ instance }) => {
2929
return (
3030
<MsalProvider instance={instance}>
3131
<PageLayout>
@@ -34,3 +34,5 @@ export const App = ({ instance }) => {
3434
</MsalProvider>
3535
);
3636
};
37+
38+
export default App;

2-Authorization-I/1-call-graph/SPA/src/authConfig.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { LogLevel } from "@azure/msal-browser";
7-
7+
import { PublicClientApplication } from "@azure/msal-browser";
88
/**
99
* Configuration object to be passed to MSAL instance on creation.
1010
* For a full list of MSAL.js configuration parameters, visit:
@@ -75,3 +75,10 @@ export const protectedResources = {
7575
scopes: ['Contacts.Read'],
7676
},
7777
};
78+
79+
/**
80+
* MSAL should be instantiated outside of the component tree to prevent it from being re-instantiated on re-renders.
81+
* For more, visit: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-react/docs/getting-started.md
82+
*/
83+
global.crypto = require('crypto');
84+
export const msalInstance = new PublicClientApplication(msalConfig);

2-Authorization-I/1-call-graph/SPA/src/fetch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License.
44
*/
55

6-
import { msalInstance } from './index';
6+
import { msalInstance } from './authConfig';
77
import { msalConfig } from '../src/authConfig';
88
import { addClaimsToStorage } from './utils/storageUtils';
99
import { parseChallenges } from './utils/claimUtils';

2-Authorization-I/1-call-graph/SPA/src/index.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
import React from 'react';
22
import { createRoot } from 'react-dom/client';
3-
import { PublicClientApplication, EventType } from '@azure/msal-browser';
3+
import { EventType } from '@azure/msal-browser';
44
import { BrowserRouter } from 'react-router-dom';
55

6-
import { App } from './App.jsx';
7-
import { msalConfig } from './authConfig';
6+
import App from './App.jsx';
7+
import { msalInstance } from './authConfig';
88

99
import 'bootstrap/dist/css/bootstrap.min.css';
1010
import './styles/index.css';
1111

1212
const container = document.getElementById('root');
1313
const root = createRoot(container);
1414

15-
/**
16-
* MSAL should be instantiated outside of the component tree to prevent it from being re-instantiated on re-renders.
17-
* For more, visit: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-react/docs/getting-started.md
18-
*/
19-
export const msalInstance = new PublicClientApplication(msalConfig);
20-
2115
// Account selection logic is app dependent. Adjust as needed for different use cases.
2216
if (!msalInstance.getActiveAccount() && msalInstance.getAllAccounts().length > 0) {
2317
msalInstance.setActiveAccount(msalInstance.getAllAccounts()[0]);

2-Authorization-I/1-call-graph/SPA/src/sample.test.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { PublicClientApplication } from "@azure/msal-browser";
1+
import '@testing-library/jest-dom';
2+
import { PublicClientApplication } from '@azure/msal-browser';
3+
import { render, screen, waitFor } from '@testing-library/react';
4+
import { BrowserRouter } from 'react-router-dom';
5+
import App from './App';
26

37
describe('Sanitize configuration object', () => {
48
beforeAll(() => {
@@ -21,7 +25,7 @@ describe('Sanitize configuration object', () => {
2125

2226
it('should not contain tenant id', () => {
2327
const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
24-
expect(regexGuid.test(msalConfig.auth.authority.split(".com/")[1])).toBe(false);
28+
expect(regexGuid.test(msalConfig.auth.authority.split('.com/')[1])).toBe(false);
2529
});
2630

2731
it('should define a redirect uri', () => {
@@ -30,15 +34,29 @@ describe('Sanitize configuration object', () => {
3034
});
3135

3236
describe('Ensure that the msal instantiates', () => {
33-
beforeAll(() => {
34-
global.crypto = require('crypto');
37+
let handleRedirectSpy;
38+
let pca;
39+
beforeEach(() => {
3540
global.msalConfig = require('./authConfig.js').msalConfig;
36-
global.msalInstance = new PublicClientApplication(msalConfig);
41+
pca = new PublicClientApplication(msalConfig);
42+
handleRedirectSpy = jest.spyOn(pca, 'handleRedirectPromise');
43+
3744
});
3845

3946
it('should instantiate msal', () => {
40-
expect(msalInstance).toBeDefined();
41-
expect(msalInstance).toBeInstanceOf(PublicClientApplication);
42-
47+
expect(pca).toBeDefined();
48+
expect(pca).toBeInstanceOf(PublicClientApplication);
4349
});
44-
});
50+
51+
it('should render the app without crashing', async () => {
52+
render(
53+
<BrowserRouter>
54+
<App instance={pca} />
55+
</BrowserRouter>
56+
);
57+
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1));
58+
expect(
59+
await screen.findByText('Welcome to the Microsoft Authentication Library For React Tutorial')
60+
).toBeInTheDocument();
61+
});
62+
});

0 commit comments

Comments
 (0)