@appshell/react 0.1.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +183 -0
  2. package/LICENSE +21 -0
  3. package/README.md +117 -0
  4. package/__tests__/FederatedComponent.spec.tsx +53 -0
  5. package/__tests__/LoadingError.spec.tsx +14 -0
  6. package/__tests__/ManifestContext.spec.tsx +36 -0
  7. package/__tests__/ReactHost.spec.tsx +137 -0
  8. package/__tests__/__snapshots__/FederatedComponent.spec.tsx.snap +9 -0
  9. package/__tests__/__snapshots__/LoadingError.spec.tsx.snap +88 -0
  10. package/__tests__/__snapshots__/ReactHost.spec.tsx.snap +13 -0
  11. package/__tests__/fixtures/Manifest.ts +16 -0
  12. package/__tests__/fixtures/TestComponent.tsx +5 -0
  13. package/__tests__/fixtures/appshell.index.ts +5 -0
  14. package/__tests__/jsonResource.spec.ts +27 -0
  15. package/__tests__/utils/waitForResource.ts +15 -0
  16. package/dist/main.js +1 -0
  17. package/dist/types/config/src/axios.d.ts +3 -0
  18. package/dist/types/config/src/axios.d.ts.map +1 -0
  19. package/dist/types/config/src/configmap.d.ts +7 -0
  20. package/dist/types/config/src/configmap.d.ts.map +1 -0
  21. package/dist/types/config/src/generate.env.d.ts +3 -0
  22. package/dist/types/config/src/generate.env.d.ts.map +1 -0
  23. package/dist/types/config/src/generate.index.d.ts +4 -0
  24. package/dist/types/config/src/generate.index.d.ts.map +1 -0
  25. package/dist/types/config/src/generate.manifest.d.ts +9 -0
  26. package/dist/types/config/src/generate.manifest.d.ts.map +1 -0
  27. package/dist/types/config/src/generate.metadata.d.ts +4 -0
  28. package/dist/types/config/src/generate.metadata.d.ts.map +1 -0
  29. package/dist/types/config/src/index.d.ts +13 -0
  30. package/dist/types/config/src/index.d.ts.map +1 -0
  31. package/dist/types/config/src/mappers/appshell.config.d.ts +3 -0
  32. package/dist/types/config/src/mappers/appshell.config.d.ts.map +1 -0
  33. package/dist/types/config/src/register.d.ts +4 -0
  34. package/dist/types/config/src/register.d.ts.map +1 -0
  35. package/dist/types/config/src/types.d.ts +213 -0
  36. package/dist/types/config/src/types.d.ts.map +1 -0
  37. package/dist/types/config/src/utils/copy.d.ts +11 -0
  38. package/dist/types/config/src/utils/copy.d.ts.map +1 -0
  39. package/dist/types/config/src/utils/index.d.ts +7 -0
  40. package/dist/types/config/src/utils/index.d.ts.map +1 -0
  41. package/dist/types/config/src/utils/isValidUrl.d.ts +3 -0
  42. package/dist/types/config/src/utils/isValidUrl.d.ts.map +1 -0
  43. package/dist/types/config/src/utils/list.d.ts +10 -0
  44. package/dist/types/config/src/utils/list.d.ts.map +1 -0
  45. package/dist/types/config/src/utils/load.d.ts +14 -0
  46. package/dist/types/config/src/utils/load.d.ts.map +1 -0
  47. package/dist/types/config/src/utils/loadJson.d.ts +3 -0
  48. package/dist/types/config/src/utils/loadJson.d.ts.map +1 -0
  49. package/dist/types/config/src/utils/merge.d.ts +4 -0
  50. package/dist/types/config/src/utils/merge.d.ts.map +1 -0
  51. package/dist/types/config/src/validators/appshell.config.d.ts +4 -0
  52. package/dist/types/config/src/validators/appshell.config.d.ts.map +1 -0
  53. package/dist/types/config/src/validators/appshell.index.d.ts +4 -0
  54. package/dist/types/config/src/validators/appshell.index.d.ts.map +1 -0
  55. package/dist/types/config/src/validators/appshell.manifest.d.ts +4 -0
  56. package/dist/types/config/src/validators/appshell.manifest.d.ts.map +1 -0
  57. package/dist/types/config/src/validators/appshell.metadata.d.ts +4 -0
  58. package/dist/types/config/src/validators/appshell.metadata.d.ts.map +1 -0
  59. package/dist/types/config/src/validators/index.d.ts +6 -0
  60. package/dist/types/config/src/validators/index.d.ts.map +1 -0
  61. package/dist/types/config/src/validators/merge.manifests.d.ts +4 -0
  62. package/dist/types/config/src/validators/merge.manifests.d.ts.map +1 -0
  63. package/dist/types/loader/src/fetchDynamicScript.d.ts +3 -0
  64. package/dist/types/loader/src/fetchDynamicScript.d.ts.map +1 -0
  65. package/dist/types/loader/src/index.d.ts +4 -0
  66. package/dist/types/loader/src/index.d.ts.map +1 -0
  67. package/dist/types/loader/src/loadFederatedComponent.d.ts +14 -0
  68. package/dist/types/loader/src/loadFederatedComponent.d.ts.map +1 -0
  69. package/dist/types/loader/src/remoteLoader.d.ts +4 -0
  70. package/dist/types/loader/src/remoteLoader.d.ts.map +1 -0
  71. package/dist/types/loader/src/types.d.ts +11 -0
  72. package/dist/types/loader/src/types.d.ts.map +1 -0
  73. package/dist/types/react/src/components/FederatedComponent.d.ts +20 -0
  74. package/dist/types/react/src/components/FederatedComponent.d.ts.map +1 -0
  75. package/dist/types/react/src/components/LoadingError.d.ts +8 -0
  76. package/dist/types/react/src/components/LoadingError.d.ts.map +1 -0
  77. package/dist/types/react/src/components/ReactHost.d.ts +10 -0
  78. package/dist/types/react/src/components/ReactHost.d.ts.map +1 -0
  79. package/dist/types/react/src/contexts/ManifestContext.d.ts +12 -0
  80. package/dist/types/react/src/contexts/ManifestContext.d.ts.map +1 -0
  81. package/dist/types/react/src/contexts/MetadataContext.d.ts +11 -0
  82. package/dist/types/react/src/contexts/MetadataContext.d.ts.map +1 -0
  83. package/dist/types/react/src/contexts/RegistryContext.d.ts +11 -0
  84. package/dist/types/react/src/contexts/RegistryContext.d.ts.map +1 -0
  85. package/dist/types/react/src/hooks/useManifest.d.ts +4 -0
  86. package/dist/types/react/src/hooks/useManifest.d.ts.map +1 -0
  87. package/dist/types/react/src/hooks/useMetadata.d.ts +4 -0
  88. package/dist/types/react/src/hooks/useMetadata.d.ts.map +1 -0
  89. package/dist/types/react/src/hooks/useRegistry.d.ts +4 -0
  90. package/dist/types/react/src/hooks/useRegistry.d.ts.map +1 -0
  91. package/dist/types/react/src/index.d.ts +10 -0
  92. package/dist/types/react/src/index.d.ts.map +1 -0
  93. package/dist/types/react/src/resources/jsonResource.d.ts +5 -0
  94. package/dist/types/react/src/resources/jsonResource.d.ts.map +1 -0
  95. package/dist/types/react/src/types.d.ts +4 -0
  96. package/dist/types/react/src/types.d.ts.map +1 -0
  97. package/package.json +32 -0
  98. package/src/components/FederatedComponent.tsx +73 -0
  99. package/src/components/LoadingError.tsx +15 -0
  100. package/src/components/ReactHost.tsx +69 -0
  101. package/src/contexts/ManifestContext.tsx +22 -0
  102. package/src/contexts/MetadataContext.tsx +17 -0
  103. package/src/contexts/RegistryContext.tsx +17 -0
  104. package/src/hooks/useManifest.ts +7 -0
  105. package/src/hooks/useMetadata.ts +6 -0
  106. package/src/hooks/useRegistry.ts +6 -0
  107. package/src/index.ts +9 -0
  108. package/src/resources/jsonResource.ts +35 -0
  109. package/src/types.ts +3 -0
  110. package/tsconfig.json +7 -0
  111. package/webpack.config.js +42 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,183 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 0.1.0-alpha.6 (2023-07-10)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * rename @appshell/react-federated-component package to @appshell/react ([0b74498](https://github.com/navaris/appshell/commit/0b74498bf17ba73db4d936c8a012af1d3b111a28))
12
+
13
+
14
+
15
+
16
+
17
+ # [0.1.0-alpha.5](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.1.0-alpha.4...@appshell/react-federated-component@0.1.0-alpha.5) (2023-07-10)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * registry projection, enable env overwrite, refactor docker workflow, consolidate env names ([33a28fe](https://github.com/navaris/appshell/commit/33a28fe76b58e05c5b6b6b33d4b402e52bb29e70))
23
+
24
+
25
+
26
+
27
+
28
+ # [0.1.0-alpha.4](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.1.0-alpha.3...@appshell/react-federated-component@0.1.0-alpha.4) (2023-07-06)
29
+
30
+
31
+ ### Bug Fixes
32
+
33
+ * metadata urls ([efcce1c](https://github.com/navaris/appshell/commit/efcce1ce4cb1745afa83661cae1414a73f837483))
34
+
35
+
36
+
37
+
38
+
39
+ # [0.1.0-alpha.3](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.1.0-alpha.2...@appshell/react-federated-component@0.1.0-alpha.3) (2023-07-06)
40
+
41
+
42
+ ### Bug Fixes
43
+
44
+ * support metadata generation ([4df202f](https://github.com/navaris/appshell/commit/4df202f0fd3b9ca6c660975b75eb0ac9b60225c2))
45
+
46
+
47
+
48
+
49
+
50
+ # [0.1.0-alpha.2](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.1.0-alpha.1...@appshell/react-federated-component@0.1.0-alpha.2) (2023-07-06)
51
+
52
+
53
+ ### Bug Fixes
54
+
55
+ * lint ([a4861ab](https://github.com/navaris/appshell/commit/a4861abfe82d1e0ab815aaa62cdf7907229d0136))
56
+
57
+
58
+ ### Features
59
+
60
+ * appshell start, cleanup dev workflow ([74fd2e5](https://github.com/navaris/appshell/commit/74fd2e5a5acd2415482268175c7f3f16cd7c93ec))
61
+
62
+
63
+
64
+
65
+
66
+ # [0.1.0-alpha.1](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.3...@appshell/react-federated-component@0.1.0-alpha.1) (2023-07-05)
67
+
68
+
69
+ ### Features
70
+
71
+ * foundation refactor wip ([339b930](https://github.com/navaris/appshell/commit/339b9306accaa9aba1712f3a0dee6c4cab7ed273))
72
+
73
+
74
+
75
+
76
+
77
+ # [0.1.0-alpha.0](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.3...@appshell/react-federated-component@0.1.0-alpha.0) (2023-07-05)
78
+
79
+
80
+ ### Features
81
+
82
+ * registry index ([8e6ee0a](https://github.com/navaris/appshell/commit/8e6ee0a6a377584efa2ee702168025f46108b8c5))
83
+
84
+
85
+
86
+
87
+
88
+ ## [0.0.3](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.3-alpha.2...@appshell/react-federated-component@0.0.3) (2023-06-29)
89
+
90
+ **Note:** Version bump only for package @appshell/react-federated-component
91
+
92
+
93
+
94
+
95
+
96
+ ## [0.0.3-alpha.2](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.2...@appshell/react-federated-component@0.0.3-alpha.2) (2023-06-29)
97
+
98
+ **Note:** Version bump only for package @appshell/react-federated-component
99
+
100
+
101
+
102
+
103
+
104
+
105
+ ## [0.0.3-alpha.1](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.3-alpha.0...@appshell/react-federated-component@0.0.3-alpha.1) (2023-06-29)
106
+
107
+
108
+ ### Bug Fixes
109
+
110
+ * update npm package visibility ([1ef4119](https://github.com/navaris/appshell/commit/1ef411903dd038dfc781e8ce0700811e5460c903))
111
+
112
+
113
+
114
+
115
+
116
+ ## [0.0.3-alpha.0](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.2-alpha.1...@appshell/react-federated-component@0.0.3-alpha.0) (2023-06-29)
117
+
118
+ **Note:** Version bump only for package @appshell/react-federated-component
119
+
120
+
121
+
122
+
123
+
124
+ ## [0.0.2](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.2-alpha.1...@appshell/react-federated-component@0.0.2) (2023-06-29)
125
+
126
+ **Note:** Version bump only for package @appshell/react-federated-component
127
+
128
+
129
+
130
+
131
+
132
+ ## [0.0.2-alpha.1](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.2-alpha.0...@appshell/react-federated-component@0.0.2-alpha.1) (2023-06-24)
133
+
134
+
135
+ ### Bug Fixes
136
+
137
+ * add useEffect dependencies for federated component ([616039e](https://github.com/navaris/appshell/commit/616039ebb3f46f34db0b82acfb6ee5438a6b4731))
138
+
139
+
140
+
141
+
142
+
143
+ ## [0.0.2-alpha.0](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.1...@appshell/react-federated-component@0.0.2-alpha.0) (2023-06-15)
144
+
145
+
146
+ ### Bug Fixes
147
+
148
+ * hot module replacement support, example updates ([a67d230](https://github.com/navaris/appshell/commit/a67d2303b40f5911373225cc0b4ccf9b67c33d11))
149
+
150
+
151
+
152
+
153
+
154
+ ## 0.0.1 (2023-06-15)
155
+
156
+ **Note:** Version bump only for package @appshell/react-federated-component
157
+
158
+
159
+
160
+
161
+
162
+ ## [0.0.1-alpha.2](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.1-alpha.1...@appshell/react-federated-component@0.0.1-alpha.2) (2023-06-15)
163
+
164
+
165
+ ### Bug Fixes
166
+
167
+ * hot module replacement support, example updates ([a67d230](https://github.com/navaris/appshell/commit/a67d2303b40f5911373225cc0b4ccf9b67c33d11))
168
+
169
+
170
+
171
+
172
+
173
+ ## [0.0.1-alpha.1](https://github.com/navaris/appshell/compare/@appshell/react-federated-component@0.0.1-alpha.0...@appshell/react-federated-component@0.0.1-alpha.1) (2023-03-19)
174
+
175
+ **Note:** Version bump only for package @appshell/react-federated-component
176
+
177
+
178
+
179
+
180
+
181
+ ## 0.0.1-alpha.0 (2023-03-19)
182
+
183
+ **Note:** Version bump only for package @appshell/react-federated-component
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Navaris Technologies, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,117 @@
1
+ <div align="center">
2
+ <a href="https://github.com/navaris/appshell">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://github.com/navaris/appshell/blob/main/assets/branding/appshell-logo-white_2x.png">
5
+ <img alt="appshell" src="https://github.com/navaris/appshell/blob/main/assets/branding/appshell-logo_2x.png">
6
+ </picture>
7
+ </a>
8
+ </div>
9
+
10
+ [![Appshell CI](https://github.com/navaris/appshell/actions/workflows/pipeline.yml/badge.svg)](https://github.com/navaris/appshell/actions/workflows/pipeline.yml)
11
+
12
+ # @appshell/react
13
+
14
+ React utilites for building micro-frontends with Webpack Module federation and Appshell.
15
+
16
+ Working examples can be found [here](https://github.com/navaris/appshell/tree/main/examples).
17
+
18
+ ## Getting Started
19
+
20
+ To begin, you'll need to install `@appshell/react`:
21
+
22
+ ```console
23
+ npm install @appshell/react --save-dev
24
+ ```
25
+
26
+ or
27
+
28
+ ```console
29
+ yarn add -D @appshell/react
30
+ ```
31
+
32
+ or
33
+
34
+ ```console
35
+ pnpm add -D @appshell/react
36
+ ```
37
+
38
+ ## FederatedComponent
39
+
40
+ React component that dynamically loads federated components.
41
+
42
+ ```tsx
43
+ import { FederatedComponent, ManifestProvider } from '@appshell/react';
44
+
45
+ <App>
46
+ <ManifestProvider manifest={manifest}>
47
+ <FederatedComponent remote="PingModule/Ping">
48
+ <FederatedComponent remote="PongModule/Pong">
49
+ </ManifestProvider>
50
+ </App>
51
+ ```
52
+
53
+ ## useRegistry
54
+
55
+ For access to the registry index.
56
+
57
+ ```tsx
58
+ import { RegistryProvider, useRegistry } from '@appshell/react';
59
+
60
+ const MyComponent = () => {
61
+ const index = useRegistry();
62
+
63
+ ...
64
+ }
65
+
66
+ <RegistryProvider index={index}>
67
+ <MyComponent />
68
+ </RegistryProvider>
69
+ ```
70
+
71
+ ## useMetadata
72
+
73
+ For access to the registry metadata.
74
+
75
+ ```tsx
76
+ import { MetadataProvider, useMetadata } from '@appshell/react';
77
+
78
+ const MyComponent = () => {
79
+ const metadata = useMetadata();
80
+
81
+ ...
82
+ }
83
+
84
+ <MetadataProvider metadata={metadata}>
85
+ <MyComponent />
86
+ </MetadataProvider>
87
+ ```
88
+
89
+ ## useManifest
90
+
91
+ For access to the manifest.
92
+
93
+ ```tsx
94
+ import { ManifestProvider, useManifest } from '@appshell/react';
95
+
96
+ const MyComponent = () => {
97
+ const manifest = useManifest();
98
+
99
+ ...
100
+ }
101
+
102
+ <ManifestProvider manifest={manifest}>
103
+ <MyComponent />
104
+ </ManifestProvider>
105
+ ```
106
+
107
+ **Where does the registry come from?**
108
+
109
+ Currently, the registry is a shared directory or mounted volume. It does support calling an http endpoint, but it has not yet been implemented.
110
+
111
+ > See [@appshell/cli](https://www.npmjs.com/package/@appshell/cli)
112
+
113
+ Each micro-frontend registers its manifest with the registry
114
+
115
+ ```bash
116
+ appshell register --manifest dist/appshell.manifest.json
117
+ ```
@@ -0,0 +1,53 @@
1
+ /** @jest-environment jsdom */
2
+ import * as remoteLoader from '@appshell/loader';
3
+ import '@testing-library/jest-dom';
4
+ import { act, render, screen } from '@testing-library/react';
5
+ import React from 'react';
6
+ import FederatedComponent from '../src/components/FederatedComponent';
7
+ import * as useRegistry from '../src/hooks/useRegistry';
8
+ import index from './fixtures/appshell.index';
9
+ import manifest from './fixtures/Manifest';
10
+ import TestComponent from './fixtures/TestComponent';
11
+
12
+ jest.mock('../src/hooks/useRegistry');
13
+
14
+ const TestFallback = () => <div>loading...</div>;
15
+
16
+ describe('FederatedComponent', () => {
17
+ it('should match snapshot', async () => {
18
+ jest.spyOn(useRegistry, 'default').mockReturnValue(index);
19
+ jest.spyOn(remoteLoader, 'default').mockReturnValueOnce(async () => [TestComponent, manifest]);
20
+
21
+ const { container, findByText } = await act(() =>
22
+ render(<FederatedComponent remote="TestModule/TestComponent" />),
23
+ );
24
+ const view = await findByText(/test component/i);
25
+
26
+ expect(view).toBeInTheDocument();
27
+ expect(container).toMatchSnapshot();
28
+ });
29
+
30
+ it('should render fallback while resource is pending', async () => {
31
+ jest.spyOn(useRegistry, 'default').mockReturnValue(index);
32
+ jest.spyOn(remoteLoader, 'default').mockReturnValueOnce(() => [null, null]);
33
+
34
+ await act(() =>
35
+ render(<FederatedComponent remote="TestModule/TestComponent" fallback={<TestFallback />} />),
36
+ );
37
+
38
+ await expect(screen.getByText(/loading/i)).toBeInTheDocument();
39
+ });
40
+
41
+ it('should render error when resource fails', async () => {
42
+ jest.spyOn(useRegistry, 'default').mockReturnValue(index);
43
+ jest
44
+ .spyOn(remoteLoader, 'default')
45
+ .mockImplementationOnce(() => new Error('Failed to get resource'));
46
+
47
+ await act(() =>
48
+ render(<FederatedComponent remote="TestModule/TestComponent" fallback={<TestFallback />} />),
49
+ );
50
+
51
+ await expect(screen.getByText(/Error loading federated component/i)).toBeInTheDocument();
52
+ });
53
+ });
@@ -0,0 +1,14 @@
1
+ /** @jest-environment jsdom */
2
+ import { render } from '@testing-library/react';
3
+ import React from 'react';
4
+ import LoadingError from '../src/components/LoadingError';
5
+
6
+ describe('LoadingError', () => {
7
+ it('should match snapshot', () => {
8
+ const view = render(
9
+ <LoadingError remote="TestModule/TestComponent" reason="Could not find component" />,
10
+ );
11
+
12
+ expect(view).toMatchSnapshot();
13
+ });
14
+ });
@@ -0,0 +1,36 @@
1
+ /** @jest-environment jsdom */
2
+ import '@testing-library/jest-dom';
3
+ import { render, screen } from '@testing-library/react';
4
+ import React from 'react';
5
+ import { ManifestProvider } from '../src/contexts/ManifestContext';
6
+ import useManifest from '../src/hooks/useManifest';
7
+ import manifest from './fixtures/Manifest';
8
+ import TestComponent from './fixtures/TestComponent';
9
+
10
+ const ComponentUsingManifest = () => {
11
+ const res = useManifest();
12
+
13
+ return <div>{JSON.stringify(res.remotes)}</div>;
14
+ };
15
+
16
+ describe('ManifestProvider', () => {
17
+ it('should render children', async () => {
18
+ render(
19
+ <ManifestProvider manifest={manifest}>
20
+ <TestComponent />
21
+ </ManifestProvider>,
22
+ );
23
+
24
+ await expect(screen.getByText(/test component/i)).toBeInTheDocument();
25
+ });
26
+
27
+ it('should make the manifest context available', async () => {
28
+ render(
29
+ <ManifestProvider manifest={manifest}>
30
+ <ComponentUsingManifest />
31
+ </ManifestProvider>,
32
+ );
33
+
34
+ await expect(screen.getByText(/TestModule\/TestComponent/i)).toBeInTheDocument();
35
+ });
36
+ });
@@ -0,0 +1,137 @@
1
+ /** @jest-environment jsdom */
2
+ import '@testing-library/jest-dom';
3
+ import { act, render, screen } from '@testing-library/react';
4
+ import fetch, { enableFetchMocks } from 'jest-fetch-mock';
5
+ import React from 'react';
6
+ import * as FederatedComponent from '../src/components/FederatedComponent';
7
+ import ReactHost from '../src/components/ReactHost';
8
+
9
+ enableFetchMocks();
10
+
11
+ jest.mock('@appshell/core', () => ({
12
+ APPSHELL_ENV: {
13
+ APPSHELL_REGISTRY: 'appshell_registry',
14
+ APPSHELL_PUBLIC_URL: 'http://test.com',
15
+ APPSHELL_ROOT: 'TestModule/TestComponent',
16
+ APPSHELL_ROOT_PROPS: '{"foo":"bar"}',
17
+ },
18
+ }));
19
+
20
+ jest.mock('../src/components/FederatedComponent', () => ({
21
+ __esModule: true,
22
+ default: () => <div>test component</div>,
23
+ }));
24
+
25
+ describe('RenderHost', () => {
26
+ const indexUrl = 'http://test.com/appshell.index.json';
27
+ const metadataUrl = 'http://test.com/appshell.metadata.json';
28
+ const remote = 'TestModule/TestComponent';
29
+
30
+ let useState: jest.SpyInstance;
31
+ const mockUseState = (val) => {
32
+ useState.mockImplementationOnce(() => [val, () => {}]);
33
+ };
34
+
35
+ beforeEach(() => {
36
+ // useEffect = jest.spyOn(React, 'useEffect');
37
+ useState = jest.spyOn(React, 'useState');
38
+ fetch.mockIf(
39
+ (req) =>
40
+ req.url === 'http://test.com/appshell.metadata.json' ||
41
+ req.url === 'http://test.com/appshell.index.json',
42
+ () => Promise.resolve({ ok: true, body: JSON.stringify({ test: 'data' }) }),
43
+ );
44
+ });
45
+
46
+ afterEach(() => {
47
+ useState.mockReset();
48
+ });
49
+
50
+ it('should match snapshot', async () => {
51
+ mockUseState({ index: 'foo' });
52
+ mockUseState({ metadata: 'bar' });
53
+ const { container } = await act(() =>
54
+ render(
55
+ <ReactHost
56
+ indexUrl={indexUrl}
57
+ metadataUrl={metadataUrl}
58
+ remote={remote}
59
+ fallback="Loading"
60
+ />,
61
+ ),
62
+ );
63
+
64
+ expect(container).toMatchSnapshot();
65
+ });
66
+
67
+ it('should render empty when index is pending', async () => {
68
+ mockUseState(undefined);
69
+ mockUseState({ metadata: 'bar' });
70
+ const { container } = render(
71
+ <ReactHost
72
+ indexUrl={indexUrl}
73
+ metadataUrl={metadataUrl}
74
+ remote={remote}
75
+ fallback="Loading"
76
+ />,
77
+ );
78
+
79
+ expect(container).toMatchSnapshot();
80
+ });
81
+
82
+ it('should render empty when metadata is pending', async () => {
83
+ mockUseState({ index: 'foo' });
84
+ mockUseState(undefined);
85
+ const { container } = render(
86
+ <ReactHost
87
+ indexUrl={indexUrl}
88
+ metadataUrl={metadataUrl}
89
+ remote={remote}
90
+ fallback="Loading"
91
+ />,
92
+ );
93
+
94
+ expect(container).toMatchSnapshot();
95
+ });
96
+
97
+ it('should render component when index and metadata are available', async () => {
98
+ mockUseState({ index: 'foo' });
99
+ mockUseState({ metadata: 'bar' });
100
+ await act(() =>
101
+ render(
102
+ <ReactHost
103
+ indexUrl={indexUrl}
104
+ metadataUrl={metadataUrl}
105
+ remote={remote}
106
+ fallback="Loading"
107
+ />,
108
+ ),
109
+ );
110
+
111
+ await expect(screen.getByText(/test component/i)).toBeInTheDocument();
112
+ });
113
+
114
+ it('should pass props to federated component when available', async () => {
115
+ mockUseState({ index: 'foo' });
116
+ mockUseState({ metadata: 'bar' });
117
+ const federatedComponentSpy = jest.spyOn(FederatedComponent, 'default');
118
+ await act(() =>
119
+ render(
120
+ <ReactHost
121
+ indexUrl={indexUrl}
122
+ metadataUrl={metadataUrl}
123
+ remote={remote}
124
+ foo="bar"
125
+ fallback="Loading"
126
+ />,
127
+ ),
128
+ );
129
+
130
+ await expect(screen.getByText(/test component/i)).toBeInTheDocument();
131
+
132
+ expect(federatedComponentSpy).toHaveBeenCalledWith(
133
+ { fallback: 'Loading', foo: 'bar', remote: 'TestModule/TestComponent' },
134
+ {},
135
+ );
136
+ });
137
+ });
@@ -0,0 +1,9 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`FederatedComponent should match snapshot 1`] = `
4
+ <div>
5
+ <div>
6
+ test component
7
+ </div>
8
+ </div>
9
+ `;
@@ -0,0 +1,88 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`LoadingError should match snapshot 1`] = `
4
+ {
5
+ "asFragment": [Function],
6
+ "baseElement": <body>
7
+ <div>
8
+ <div
9
+ style="border: 3px solid red;"
10
+ >
11
+ <h5>
12
+ Error loading federated component 'TestModule/TestComponent.'
13
+ </h5>
14
+ <span
15
+ style="font-size: 1rem;"
16
+ >
17
+ Could not find component
18
+ </span>
19
+ </div>
20
+ </div>
21
+ </body>,
22
+ "container": <div>
23
+ <div
24
+ style="border: 3px solid red;"
25
+ >
26
+ <h5>
27
+ Error loading federated component 'TestModule/TestComponent.'
28
+ </h5>
29
+ <span
30
+ style="font-size: 1rem;"
31
+ >
32
+ Could not find component
33
+ </span>
34
+ </div>
35
+ </div>,
36
+ "debug": [Function],
37
+ "findAllByAltText": [Function],
38
+ "findAllByDisplayValue": [Function],
39
+ "findAllByLabelText": [Function],
40
+ "findAllByPlaceholderText": [Function],
41
+ "findAllByRole": [Function],
42
+ "findAllByTestId": [Function],
43
+ "findAllByText": [Function],
44
+ "findAllByTitle": [Function],
45
+ "findByAltText": [Function],
46
+ "findByDisplayValue": [Function],
47
+ "findByLabelText": [Function],
48
+ "findByPlaceholderText": [Function],
49
+ "findByRole": [Function],
50
+ "findByTestId": [Function],
51
+ "findByText": [Function],
52
+ "findByTitle": [Function],
53
+ "getAllByAltText": [Function],
54
+ "getAllByDisplayValue": [Function],
55
+ "getAllByLabelText": [Function],
56
+ "getAllByPlaceholderText": [Function],
57
+ "getAllByRole": [Function],
58
+ "getAllByTestId": [Function],
59
+ "getAllByText": [Function],
60
+ "getAllByTitle": [Function],
61
+ "getByAltText": [Function],
62
+ "getByDisplayValue": [Function],
63
+ "getByLabelText": [Function],
64
+ "getByPlaceholderText": [Function],
65
+ "getByRole": [Function],
66
+ "getByTestId": [Function],
67
+ "getByText": [Function],
68
+ "getByTitle": [Function],
69
+ "queryAllByAltText": [Function],
70
+ "queryAllByDisplayValue": [Function],
71
+ "queryAllByLabelText": [Function],
72
+ "queryAllByPlaceholderText": [Function],
73
+ "queryAllByRole": [Function],
74
+ "queryAllByTestId": [Function],
75
+ "queryAllByText": [Function],
76
+ "queryAllByTitle": [Function],
77
+ "queryByAltText": [Function],
78
+ "queryByDisplayValue": [Function],
79
+ "queryByLabelText": [Function],
80
+ "queryByPlaceholderText": [Function],
81
+ "queryByRole": [Function],
82
+ "queryByTestId": [Function],
83
+ "queryByText": [Function],
84
+ "queryByTitle": [Function],
85
+ "rerender": [Function],
86
+ "unmount": [Function],
87
+ }
88
+ `;
@@ -0,0 +1,13 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`RenderHost should match snapshot 1`] = `
4
+ <div>
5
+ <div>
6
+ test component
7
+ </div>
8
+ </div>
9
+ `;
10
+
11
+ exports[`RenderHost should render empty when index is pending 1`] = `<div />`;
12
+
13
+ exports[`RenderHost should render empty when metadata is pending 1`] = `<div />`;
@@ -0,0 +1,16 @@
1
+ import { AppshellManifest } from '@appshell/config';
2
+
3
+ export default {
4
+ remotes: {
5
+ 'TestModule/TestComponent': {
6
+ id: 'test-component',
7
+ scope: 'TestModule',
8
+ module: './TestComponent',
9
+ manifestUrl: 'http://test.com/appshell.manifest.json',
10
+ remoteEntryUrl: 'http://test.com/remoteEntry.js',
11
+ metadata: {},
12
+ },
13
+ },
14
+ modules: {},
15
+ environment: {},
16
+ } as AppshellManifest;