Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): ensure scalprum instance receives configuration updates #141

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/test-app-e2e/src/e2e/test-app/scalprum-api.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe('Scalprum API', () => {
beforeEach(() => {
cy.handleMetaError();
});

it('should display values from scalprum API', () => {
cy.visit('http://localhost:4200/api');
cy.contains('API consumer isBeta: false').should('exist');
});

it('should update isBeta value', () => {
cy.visit('http://localhost:4200/api');
cy.contains('API consumer isBeta: false').should('exist');
cy.contains('Toggle isBeta').click();
cy.contains('API consumer isBeta: true').should('exist');
});
});
18 changes: 13 additions & 5 deletions examples/test-app/src/entry.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import { ScalprumProvider } from '@scalprum/react-core';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import RuntimeErrorRoute from './routes/RuntimeErrorRoute';
Expand All @@ -9,6 +9,7 @@ import SDKModules from './routes/SDKModules';
import NotFoundError from './routes/NotFoundError';
import { AppsConfig } from '@scalprum/core';
import UseModuleLoading from './routes/UseModuleLoading';
import ApiUpdates from './routes/ApiUpdates';

const config: AppsConfig<{ assetsHost?: string }> = {
notFound: {
Expand Down Expand Up @@ -36,6 +37,15 @@ const config: AppsConfig<{ assetsHost?: string }> = {
};

const Entry = () => {
const [isBeta, setIsBeta] = React.useState(false);
const chromeApi = useMemo(
() => ({
foo: 'bar',
isBeta: () => isBeta,
setIsBeta,
}),
[isBeta, setIsBeta],
);
return (
<ScalprumProvider
pluginSDKOptions={{
Expand All @@ -56,10 +66,7 @@ const Entry = () => {
},
}}
api={{
chrome: {
foo: 'bar',
isBeta: () => true,
},
chrome: chromeApi,
}}
config={config}
>
Expand All @@ -72,6 +79,7 @@ const Entry = () => {
<Route path="/legacy" element={<LegacyModules />} />
<Route path="/sdk" element={<SDKModules />} />
<Route path="/use-module" element={<UseModuleLoading />} />
<Route path="/api" element={<ApiUpdates />} />
</Route>
</Routes>
</BrowserRouter>
Expand Down
3 changes: 3 additions & 0 deletions examples/test-app/src/layouts/RootLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function RootLayout() {
<NavLink variant="button" color="text.primary" to="/use-module" sx={{ my: 1, mx: 1.5 }}>
Use module hook
</NavLink>
<NavLink variant="button" color="text.primary" to="/api" sx={{ my: 1, mx: 1.5 }}>
API updates
</NavLink>
</nav>
</Toolbar>
</AppBar>
Expand Down
20 changes: 20 additions & 0 deletions examples/test-app/src/routes/ApiUpdates.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Box, Typography } from '@mui/material';
import { ScalprumComponent } from '@scalprum/react-core';

const ApiUpdates = () => {
return (
<Box>
<Typography variant="h4">API Updates</Typography>
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
<Box>
<ScalprumComponent module="./ApiModule" scope="sdk-plugin" importName="ApiConsumer" />
</Box>
<Box>
<ScalprumComponent module="./ApiModule" scope="sdk-plugin" importName="ApiChanger" />
</Box>
</Box>
</Box>
);
};

export default ApiUpdates;
12 changes: 12 additions & 0 deletions federation-cdn-mock/src/modules/apiModule.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { useScalprum } from '@scalprum/react-core';

export const ApiConsumer = () => {
const { api } = useScalprum();
return <div>API consumer isBeta: {`${api.chrome.isBeta()}`}</div>;
}

export const ApiChanger = () => {
const { api } = useScalprum();
return <div>API changer: <button onClick={() => api.chrome.setIsBeta((prev) => !prev)}>Toggle isBeta</button></div>;
}
1 change: 1 addition & 0 deletions federation-cdn-mock/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const TestSDKPlugin = new DynamicRemotePlugin({
'./ModuleThree': resolve(__dirname, './src/modules/moduleThree.tsx'),
'./ModuleFour': resolve(__dirname, './src/modules/moduleFour.tsx'),
'./SDKComponent': resolve(__dirname, './src/modules/SDKComponent.tsx'),
'./ApiModule': resolve(__dirname, './src/modules/apiModule.tsx'),
},
},
});
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ export const initialize = <T extends Record<string, any> = Record<string, any>>(
pluginStoreOptions?: PluginStoreOptions;
}): Scalprum<T> => {
if (scalprum) {
scalprum.api = api || {};
scalprum.appsConfig = appsConfig;
scalprum.scalprumOptions = {
...scalprum.scalprumOptions,
...options,
};
scalprum.pluginStore.setFeatureFlags(pluginStoreFeatureFlags);
return scalprum as Scalprum<T>;
}
const defaultOptions: ScalprumOptions = {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-core/src/scalprum-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export function ScalprumProvider<T extends Record<string, any> = Record<string,
transformPluginManifest: internalTransformPluginManifest,
},
});
}, []);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
}, [props.api, props.config, props.scalprum]);

return (
<ScalprumContext.Provider
Expand Down