Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit e2b98ab

Browse files
committed
cross import demo
unit tests supported
1 parent 01c79f1 commit e2b98ab

13 files changed

+423
-12
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
},
66
moduleNameMapper: {
77
"\\.(css)$": "identity-obj-proxy",
8+
"\\.svg$": "<rootDir>/__mocks__/fileMock.js",
89
},
910
setupFilesAfterEnv: [
1011
"../node_modules/@testing-library/jest-dom/dist/index.js",

package-lock.json

+269-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"eslint-config-prettier": "^6.7.0",
3131
"eslint-config-react-important-stuff": "^2.0.0",
3232
"eslint-plugin-prettier": "^3.1.1",
33+
"file-loader": "^6.2.0",
3334
"identity-obj-proxy": "^3.0.0",
3435
"jest": "^25.2.7",
3536
"jest-cli": "^25.2.7",
@@ -47,6 +48,7 @@
4748
"@reach/router": "^1.3.4",
4849
"express": "^4.17.1",
4950
"react": "^16.12.0",
50-
"react-dom": "^16.12.0"
51+
"react-dom": "^16.12.0",
52+
"react-use": "^15.3.4"
5153
}
5254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
login: () => {},
3+
logout: () => {},
4+
setAppMenu: () => {},
5+
getAuthUserTokens: () => new Promise(() => {}),
6+
getAuthUserProfile: () => new Promise(() => {}),
7+
};

src/__mocks__/fileMock.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "";

src/assets/images/home-green.svg

+3
Loading

src/assets/images/home.svg

+3
Loading

src/assets/images/react-green.svg

+7
Loading

src/assets/images/react-grey.svg

+7
Loading

src/components/AuthDemo/index.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from "react";
2+
import { useAsync } from "react-use";
3+
import {
4+
login,
5+
logout,
6+
getAuthUserTokens,
7+
getAuthUserProfile,
8+
} from "@topcoder/micro-frontends-navbar-app";
9+
10+
const Profile = ({ authUserProfile }) => (
11+
<>
12+
<h3>User Profile</h3>
13+
{authUserProfile.loading ? (
14+
<div>User Profile Loading...</div>
15+
) : authUserProfile.error ? (
16+
<div>Profile Loading Error: {authUserProfile.error.message}</div>
17+
) : (
18+
<ul>
19+
<li>
20+
<strong>Handle: </strong> {authUserProfile.value.handle}
21+
</li>
22+
<li>
23+
<strong>First Name: </strong> {authUserProfile.value.firstName}
24+
</li>
25+
<li>
26+
<strong>Last Name: </strong> {authUserProfile.value.lastName}
27+
</li>
28+
</ul>
29+
)}
30+
</>
31+
);
32+
33+
const AuthDemo = () => {
34+
// see how the `useAsync` hook works here https://github.com/streamich/react-use/blob/master/docs/useAsync.md
35+
const authUserTokens = useAsync(getAuthUserTokens);
36+
const authUserProfile = useAsync(getAuthUserProfile);
37+
38+
return (
39+
<>
40+
<h2>Authentication</h2>
41+
{authUserTokens.loading ? (
42+
<div>Authentication...</div>
43+
) : authUserTokens.error ? (
44+
<div>Authentication Error: {authUserTokens.error.message}</div>
45+
) : (
46+
<div>
47+
{authUserTokens.value.tokenV3 ? (
48+
<div>
49+
User is logged-in <button onClick={logout}>Logout</button>
50+
<Profile authUserProfile={authUserProfile} />
51+
</div>
52+
) : (
53+
<>
54+
User is logged-out <button onClick={login}>Login</button>
55+
</>
56+
)}
57+
</div>
58+
)}
59+
</>
60+
);
61+
};
62+
63+
export default AuthDemo;

src/constants/appMenu.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* React app side menu structure
3+
*/
4+
import reactIcon from "../assets/images/react-grey.svg";
5+
import reactActiveIcon from "../assets/images/react-green.svg";
6+
import homeIcon from "../assets/images/home.svg";
7+
import homeActiveIcon from "../assets/images/home-green.svg";
8+
9+
const appMenu = [
10+
{
11+
title: "React App",
12+
path: "/micro-frontends-react-route",
13+
icon: reactIcon,
14+
activeIcon: reactActiveIcon,
15+
},
16+
{
17+
title: "Home",
18+
path: "/micro-frontends-react-route/home",
19+
icon: homeIcon,
20+
activeIcon: homeActiveIcon,
21+
},
22+
];
23+
24+
export default appMenu;

src/root.component.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22
import { Link } from "@reach/router";
3+
import { setAppMenu } from "@topcoder/micro-frontends-navbar-app";
4+
import appMenu from "./constants/appMenu";
5+
import AuthDemo from "./components/AuthDemo";
6+
7+
export default function Root() {
8+
useEffect(() => {
9+
// when app starts it should set its side menu structure
10+
setAppMenu("/micro-frontends-react-route", appMenu);
11+
}, []);
312

4-
export default function Root(props) {
513
return (
614
<div style={{ textAlign: "center" }}>
715
<h1>React child app example</h1>
@@ -19,8 +27,12 @@ export default function Root(props) {
1927
</svg>
2028

2129
<div>
22-
<Link to="/micro-frontends-angular-route">Link to Angular child app</Link>
30+
<Link to="/micro-frontends-angular-route">
31+
Link to Angular child app
32+
</Link>
2333
</div>
34+
35+
<AuthDemo />
2436
</div>
2537
);
2638
}

webpack.config.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global __dirname */
2+
13
const webpackMerge = require("webpack-merge");
24
const singleSpaDefaults = require("webpack-config-single-spa-react");
35

@@ -8,7 +10,24 @@ module.exports = (webpackConfigEnv) => {
810
webpackConfigEnv,
911
});
1012

13+
// modify the webpack config however you'd like to by adding to this object
1114
return webpackMerge.smart(defaultConfig, {
12-
// modify the webpack config however you'd like to by adding to this object
15+
// we have to list here all the microapps which we would like to use in imports
16+
// so webpack doesn't tries to import them
17+
externals: {
18+
"@topcoder/micro-frontends-navbar-app":
19+
"@topcoder/micro-frontends-navbar-app",
20+
},
21+
module: {
22+
rules: [
23+
{
24+
test: /\.svg$/,
25+
exclude: /node_modules/,
26+
use: {
27+
loader: require.resolve("file-loader", { paths: [__dirname] }),
28+
},
29+
},
30+
],
31+
},
1332
});
1433
};

0 commit comments

Comments
 (0)